Skip to content

Commit ead80ce

Browse files
philwebbbclozel
authored andcommitted
Return null for getSuperClassName() with package-info classes
Update `ClassFileClassMetadata` to align the behavior of `getSuperClassName()` with other readers in that it returns `null` for `package-info` classes. See gh-34869
1 parent dbaba3d commit ead80ce

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileClassMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ClassFileClassMetadata implements AnnotationMetadata {
7272
this.className = className;
7373
this.accessFlags = accessFlags;
7474
this.enclosingClassName = enclosingClassName;
75-
this.superClassName = superClassName;
75+
this.superClassName = (!className.endsWith(".package-info")) ? superClassName : null;
7676
this.independentInnerClass = independentInnerClass;
7777
this.interfaceNames = interfaceNames;
7878
this.memberClassNames = memberClassNames;

spring-core/src/test/java/org/springframework/core/type/AbstractAnnotationMetadataTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ void getSuperClassNameWhenHasNoSuperClassReturnsNull() {
161161
assertThat(get(TestSubInterface.class).getSuperClassName()).isIn(null, "java.lang.Object");
162162
}
163163

164+
@Test
165+
void getSuperClassNameWhenPackageInfoReturnsNull() throws Exception {
166+
Class<?> packageClass = Class.forName(getClass().getPackageName() + ".package-info");
167+
assertThat(get(packageClass).getSuperClassName()).isNull();
168+
}
169+
164170
@Test
165171
void getInterfaceNamesWhenHasInterfacesReturnsNames() {
166172
assertThat(get(TestSubclass.class).getInterfaceNames()).containsExactly(TestInterface.class.getName());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2002-2025 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+
17+
package org.springframework.core.type.classreading;
18+
19+
import org.springframework.core.type.AbstractAnnotationMetadataTests;
20+
import org.springframework.core.type.AnnotationMetadata;
21+
22+
/**
23+
* Tests for {@link SimpleAnnotationMetadata} and
24+
* {@link SimpleAnnotationMetadataReadingVisitor} on Java < 24,
25+
* and for the ClassFile API variant on Java >= 24.
26+
*
27+
* @author Phillip Webb
28+
*/
29+
class DefaultAnnotationMetadataTests extends AbstractAnnotationMetadataTests {
30+
31+
@Override
32+
protected AnnotationMetadata get(Class<?> source) {
33+
try {
34+
return MetadataReaderFactory.create(source.getClassLoader())
35+
.getMetadataReader(source.getName()).getAnnotationMetadata();
36+
}
37+
catch (Exception ex) {
38+
throw new IllegalStateException(ex);
39+
}
40+
}
41+
42+
}

spring-core/src/test/java/org/springframework/core/type/classreading/SimpleAnnotationMetadataTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
/**
2323
* Tests for {@link SimpleAnnotationMetadata} and
24-
* {@link SimpleAnnotationMetadataReadingVisitor} on Java < 24,
25-
* and for the ClassFile API variant on Java >= 24.
24+
* {@link SimpleAnnotationMetadataReadingVisitor}.
2625
*
2726
* @author Phillip Webb
2827
*/
@@ -31,8 +30,9 @@ class SimpleAnnotationMetadataTests extends AbstractAnnotationMetadataTests {
3130
@Override
3231
protected AnnotationMetadata get(Class<?> source) {
3332
try {
34-
return MetadataReaderFactory.create(source.getClassLoader())
35-
.getMetadataReader(source.getName()).getAnnotationMetadata();
33+
return new SimpleMetadataReaderFactory(
34+
source.getClassLoader()).getMetadataReader(
35+
source.getName()).getAnnotationMetadata();
3636
}
3737
catch (Exception ex) {
3838
throw new IllegalStateException(ex);

0 commit comments

Comments
 (0)