Skip to content

Exclude local & anonymous classes from type inspection #2746

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
wants to merge 3 commits into from
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.x-2744-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ protected void contribute(AotRepositoryContext repositoryContext, GenerationCont

repositoryContext.getResolvedTypes().stream()
.filter(it -> !RepositoryRegistrationAotContribution.isJavaOrPrimitiveType(it))
.forEach(it -> RepositoryRegistrationAotProcessor.contributeType(it, generationContext));
.forEach(it -> contributeType(it, generationContext));

repositoryContext.getResolvedAnnotations().stream()
.filter(RepositoryRegistrationAotProcessor::isSpringDataManagedAnnotation).map(MergedAnnotation::getType)
.forEach(it -> RepositoryRegistrationAotProcessor.contributeType(it, generationContext));
.forEach(it -> contributeType(it, generationContext));
}

private boolean isRepositoryBean(RegisteredBean bean) {
Expand Down Expand Up @@ -167,7 +167,7 @@ private static boolean isSpringDataManagedAnnotation(@Nullable MergedAnnotation<
|| annotation.getMetaTypes().stream().anyMatch(RepositoryRegistrationAotProcessor::isInSpringDataNamespace));
}

private static void contributeType(Class<?> type, GenerationContext generationContext) {
protected void contributeType(Class<?> type, GenerationContext generationContext) {
TypeContributor.contribute(type, it -> true, generationContext);
}

Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/springframework/data/util/TypeCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ public class TypeCollector {

private static final Log logger = LogFactory.getLog(TypeCollector.class);

static final Set<String> EXCLUDED_DOMAINS = new HashSet<>(Arrays.asList("java", "sun.", "jdk.", "reactor.",
"kotlinx.", "kotlin.", "org.springframework.core.", "org.springframework.data.mapping.",
"org.springframework.data.repository.", "org.springframework.boot.", "org.springframework.core."));
static final Set<String> EXCLUDED_DOMAINS = new HashSet<>(
Arrays.asList("java", "sun.", "jdk.", "reactor.", "kotlinx.", "kotlin.", "org.springframework.core.",
"org.springframework.data.mapping.", "org.springframework.data.repository.", "org.springframework.boot.",
"org.springframework.context.", "org.springframework.beans."));

private final Predicate<Class<?>> excludedDomainsFilter = type -> {
String packageName = type.getPackageName();
String packageName = type.getPackageName() + ".";
return EXCLUDED_DOMAINS.stream().noneMatch(packageName::startsWith);
};

private Predicate<Class<?>> typeFilter = excludedDomainsFilter;
private Predicate<Class<?>> typeFilter = excludedDomainsFilter
.and(it -> !it.isLocalClass() && !it.isAnonymousClass());

private final Predicate<Method> methodFilter = createMethodFilter();

Expand Down Expand Up @@ -125,6 +127,10 @@ Set<Type> visitConstructorsOfType(ResolvableType type) {
}
Set<Type> discoveredTypes = new LinkedHashSet<>();
for (Constructor<?> constructor : type.toClass().getDeclaredConstructors()) {

if (constructor.isSynthetic()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kotlin likes to generate synthetic constructors for data classes. We should make sure to not break stuff. Likely, the field-based inspection is going to catch such cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continue;
}
for (Class<?> signatureType : TypeUtils.resolveTypesInSignature(type.toClass(), constructor)) {
if (typeFilter.test(signatureType)) {
discoveredTypes.add(signatureType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ void includesDeclaredClassesInInspection() {
WithDeclaredClass.SomeEnum.class);
}

@Test // GH-2744
void skipsCoreFrameworkType() {
assertThat(TypeCollector.inspect(org.springframework.core.AliasRegistry.class).list()).isEmpty();
}

}