|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.nativex.substitutions.data; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.LinkedHashSet; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.function.Function; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import com.oracle.svm.core.annotate.Substitute; |
| 26 | +import com.oracle.svm.core.annotate.TargetClass; |
| 27 | +import org.springframework.beans.factory.BeanDefinitionStoreException; |
| 28 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 29 | +import org.springframework.context.annotation.ScannedGenericBeanDefinition; |
| 30 | +import org.springframework.context.index.CandidateComponentsIndex; |
| 31 | +import org.springframework.context.index.CandidateComponentsIndexLoader; |
| 32 | +import org.springframework.core.type.classreading.MetadataReader; |
| 33 | +import org.springframework.data.repository.config.ImplementationDetectionConfiguration; |
| 34 | +import org.springframework.nativex.substitutions.OnlyIfPresent; |
| 35 | +import org.springframework.stereotype.Component; |
| 36 | + |
| 37 | + |
| 38 | +@TargetClass(className = "org.springframework.data.repository.config.CustomRepositoryImplementationDetector", onlyWith = {OnlyIfPresent.class}) |
| 39 | +public final class Target_CustomRepositoryImplementationDetector { |
| 40 | + |
| 41 | + @Substitute |
| 42 | + private Set<BeanDefinition> findCandidateBeanDefinitions(ImplementationDetectionConfiguration config) { |
| 43 | + |
| 44 | + /* Using the index instead of ClassPathScanningCandidateComponentProvider with pattern. |
| 45 | + * Not sure why components are not found via the index as it should be configured on |
| 46 | + * `setResourceLoader` within ClassPathScanningCandidateComponentProvider. |
| 47 | + * |
| 48 | + * ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false, environment); |
| 49 | + * |
| 50 | + * provider.setResourceLoader(resourceLoader); |
| 51 | + * provider.setResourcePattern(String.format(CUSTOM_IMPLEMENTATION_RESOURCE_PATTERN, postfix)); |
| 52 | + * provider.setMetadataReaderFactory(config.getMetadataReaderFactory()); |
| 53 | + * provider.addIncludeFilter((reader, factory) -> true); |
| 54 | + */ |
| 55 | + CandidateComponentsIndex index = CandidateComponentsIndexLoader.loadIndex(config.getClass().getClassLoader()); |
| 56 | + |
| 57 | + return config.getBasePackages().stream() |
| 58 | + |
| 59 | + .flatMap(new Function<String, Stream<? extends BeanDefinition>>() { // see oracle/graal#2479 |
| 60 | + |
| 61 | + @Override |
| 62 | + public Stream<? extends BeanDefinition> apply(String basePackage) { |
| 63 | + |
| 64 | + Set<String> candidateTypes = index.getCandidateTypes(basePackage, Component.class.getName()); |
| 65 | + if (candidateTypes.isEmpty()) { |
| 66 | + return Stream.empty(); |
| 67 | + } |
| 68 | + |
| 69 | + Set<BeanDefinition> beanDefinitions = new LinkedHashSet<>(); |
| 70 | + for (String candidate : candidateTypes) { |
| 71 | + if (candidate.endsWith(config.getImplementationPostfix())) { |
| 72 | + |
| 73 | + try { |
| 74 | + |
| 75 | + MetadataReader metadataReader = config.getMetadataReaderFactory().getMetadataReader(candidate); |
| 76 | + ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader); |
| 77 | + sbd.setResource(metadataReader.getResource()); |
| 78 | + beanDefinitions.add(sbd); |
| 79 | + } catch (IOException ex) { |
| 80 | + throw new BeanDefinitionStoreException(String.format("Failure while reading metadata for %s.", candidate), ex); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return beanDefinitions.stream(); |
| 86 | + } |
| 87 | + } |
| 88 | + ).collect(Collectors.toSet()); |
| 89 | + } |
| 90 | +} |
0 commit comments