-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ImportSelector#getExclusionFilter does not exclude matching candidates with import selector #27080
Comments
// @return null when exclustionFilter matches className
@Nullable
SourceClass asSourceClass(@Nullable String className, Predicate<String> exclusionFilter) throws IOException {
if (className == null)
return this.objectSourceClass;
}
if (exclusionFilter.test(className) { // 👈👈 should be excluded
return null
}
}
// usage example
private Collection<SourceClass> asSourceClasses(String[] classNames, Predicate<String> filter) throws IOException {
List<SourceClass> annotatedClasses = new ArrayList<>(classNames.length);
for (String className : classNames) {
SourceClass sourceClass = asSourceClass(className, filter);
if (sourceClass != null) { // 👈👈 should exclude sourceClass
annotatedClasses.add(sourceClass);
}
}
return annotatedClasses;
} (just idea) maybe we can explicitly return |
@injae-kim I don't know. I think the first step is to reproduce this scenario in a test here and then investigate. Import selector is quite involved already. |
aha~ I understood. if you don't start to investigate this issue yet, may I investigate this more? I'll check this case and share the result to you within next friday~! |
Thanks for asking. Yes, I am interested as I am busy on other issues at the moment. |
Investigation
private final SourceClass objectSourceClass = new SourceClass(Object.class);
SourceClass asSourceClass(@Nullable Class<?> classType) throws IOException {
if (classType == null || classType.getName().startsWith("java.lang.annotation")) { // 👈
return this.objectSourceClass;
} (based on commit message) I think at first, we just want to skip
After that, we introduced SourceClass asSourceClass(@Nullable String className, Predicate<String> filter) throws IOException {
if (className == null || filter.test(className)) { // 👈
return this.objectSourceClass;
} Lines 73 to 75 in 4f16297
But unlike above comment, we don't bypass when candidates match Proposal @Nullable
SourceClass asSourceClass(@Nullable Class<?> classType, Predicate<String> filter) throws IOException {
if (filter.test(classType.getName())) {
return null; // 👈
} So my proposal is, explicitly return Lines 368 to 370 in 4f16297
But I'm not sure how to test it 😅 maybe above test can cover this change? @snicoll PTAL when you have some times, and feel free to share your opinion and how can we test it. thanks! |
@injae-kim thanks for your help. I went with quite a pragmatic approach of filtering them out for import selectors only. But we might have to revisit this part of the code at some point (cc @jhoeller). |
5331499 oh~ I understood your approach. if you need any help in the future, feel free to mention me ;) |
ImportSelector#getExclusionFilter
is a way to let an import selector apply an exclude to classes that are imported. We use this in Spring Boot to filter out classes that are about to be imported and that have conditions we can run statically eagerly and to avoid loading unnecessary classes.CacheAutoConfiguration
has an import selector that loads the various configuration candidates. That's handled primarily by the following code:spring-framework/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
Lines 581 to 583 in df588e0
The candidates for the import selector are as follows:
The exclusion filter is going to match for a number of them. As a result the
importSourceClasses
collection is the following:Those 7 "configuration classes" are then later on processed. It leads ultimately to a
java.lang.Object
bean to be defined.The reason for this is because of a shortcut when the exclude filter matches:
spring-framework/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
Lines 684 to 686 in df588e0
Perhaps an improvement would be a way to not contribute the
SourceClass
at all so that it's not processed?The text was updated successfully, but these errors were encountered: