Skip to content

Correct AutoConfigurationSorter for source-style nested class name #44276

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
Expand Down Expand Up @@ -66,13 +67,17 @@ List<String> getInPriorityOrder(Collection<String> classNames) {
// Then sort by order
AutoConfigurationClasses classes = new AutoConfigurationClasses(this.metadataReaderFactory,
this.autoConfigurationMetadata, alphabeticallyOrderedClassNames);
List<String> orderedClassNames = new ArrayList<>(classNames);
Collections.sort(orderedClassNames);
orderedClassNames.sort((o1, o2) -> {
int i1 = classes.get(o1).getOrder();
int i2 = classes.get(o2).getOrder();
return Integer.compare(i1, i2);
});
List<String> orderedClassNames = classNames.stream()
// collect the changed className (e.g. for source style nested class name)
.map(classes::getAutoConfigurationClassName)
// sort by alphabetical order
.sorted()
// then by order
.sorted((o1, o2) -> {
int i1 = classes.get(o1).getOrder();
int i2 = classes.get(o2).getOrder();
return Integer.compare(i1, i2);
}).collect(Collectors.toList());
// Then respect @AutoConfigureBefore @AutoConfigureAfter
orderedClassNames = sortByAnnotation(classes, orderedClassNames);
return orderedClassNames;
Expand Down Expand Up @@ -135,6 +140,10 @@ private void addToClasses(MetadataReaderFactory metadataReaderFactory,
boolean available = autoConfigurationClass.isAvailable();
if (required || available) {
this.classes.put(className, autoConfigurationClass);
// collect the changed className (e.g. for source style nested class name)
if (!className.equals(autoConfigurationClass.getClassName())) {
this.classes.put(autoConfigurationClass.getClassName(), autoConfigurationClass);
}
}
if (available) {
addToClasses(metadataReaderFactory, autoConfigurationMetadata,
Expand All @@ -150,10 +159,16 @@ AutoConfigurationClass get(String className) {
return this.classes.get(className);
}

String getAutoConfigurationClassName(String className) {
AutoConfigurationClass autoConfigurationClass = this.classes.get(className);
return null == autoConfigurationClass ? className : autoConfigurationClass.getClassName();
}

Set<String> getClassesRequestedAfter(String className) {
Set<String> classesRequestedAfter = new LinkedHashSet<>(get(className).getAfter());
this.classes.forEach((name, autoConfigurationClass) -> {
if (autoConfigurationClass.getBefore().contains(className)) {
if (autoConfigurationClass.getCorrectedBeforeClassNames(this::getAutoConfigurationClassName)
.contains(className)) {
classesRequestedAfter.add(name);
}
});
Expand All @@ -164,7 +179,7 @@ Set<String> getClassesRequestedAfter(String className) {

private class AutoConfigurationClass {

private final String className;
private String className;

private final MetadataReaderFactory metadataReaderFactory;

Expand All @@ -176,13 +191,20 @@ private class AutoConfigurationClass {

private volatile Set<String> after;

private volatile Set<String> correctedBeforeClassNames;

AutoConfigurationClass(String className, MetadataReaderFactory metadataReaderFactory,
AutoConfigurationMetadata autoConfigurationMetadata) {
this.className = className;
this.metadataReaderFactory = metadataReaderFactory;
this.autoConfigurationMetadata = autoConfigurationMetadata;
}

public String getClassName() {
// should be called after getAnnotationMetadata method for correcting className
return this.className;
}

boolean isAvailable() {
try {
if (!wasProcessed()) {
Expand Down Expand Up @@ -227,6 +249,14 @@ private Set<String> applyReplacements(Set<String> values) {
return replaced;
}

Set<String> getCorrectedBeforeClassNames(UnaryOperator<String> correctionMapper) {
if (null == this.correctedBeforeClassNames) {
this.correctedBeforeClassNames = getBefore().stream().map(correctionMapper)
.collect(Collectors.toCollection(LinkedHashSet::new));
}
return this.correctedBeforeClassNames;
}

private int getOrder() {
if (wasProcessed()) {
return this.autoConfigurationMetadata.getInteger(this.className, "AutoConfigureOrder",
Expand Down Expand Up @@ -259,6 +289,8 @@ private AnnotationMetadata getAnnotationMetadata() {
try {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(this.className);
this.annotationMetadata = metadataReader.getAnnotationMetadata();
// correct the class name (e.g. for source style nested class name)
this.className = this.annotationMetadata.getClassName();
}
catch (IOException ex) {
throw new IllegalStateException("Unable to read meta-data for class " + this.className, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ void byBeforeAnnotationThenOrderAnnotation() {
assertThat(actual).containsExactly(oa1, oa2, oa3, oa4, oa);
}

@Test
void byAutoConfigureAfterWithNestedClassNameInSourceStyle() {
// For B, replace the last $ with .
String srcStyleB = B.substring(0, B.lastIndexOf('$')) + '.' + B.substring(B.lastIndexOf('$') + 1);
List<String> actual = getInPriorityOrder(A, srcStyleB, C);
assertThat(actual).containsExactly(C, B, A);
}

private List<String> getInPriorityOrder(String... classNames) {
return this.sorter.getInPriorityOrder(Arrays.asList(classNames));
}
Expand Down