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
This commit is contained in:
Phillip Webb
2025-05-07 22:53:47 -07:00
committed by Brian Clozel
parent dbaba3d88a
commit ead80ce2f5
4 changed files with 53 additions and 5 deletions

View File

@@ -72,7 +72,7 @@ class ClassFileClassMetadata implements AnnotationMetadata {
this.className = className;
this.accessFlags = accessFlags;
this.enclosingClassName = enclosingClassName;
this.superClassName = superClassName;
this.superClassName = (!className.endsWith(".package-info")) ? superClassName : null;
this.independentInnerClass = independentInnerClass;
this.interfaceNames = interfaceNames;
this.memberClassNames = memberClassNames;

View File

@@ -161,6 +161,12 @@ public abstract class AbstractAnnotationMetadataTests {
assertThat(get(TestSubInterface.class).getSuperClassName()).isIn(null, "java.lang.Object");
}
@Test
void getSuperClassNameWhenPackageInfoReturnsNull() throws Exception {
Class<?> packageClass = Class.forName(getClass().getPackageName() + ".package-info");
assertThat(get(packageClass).getSuperClassName()).isNull();
}
@Test
void getInterfaceNamesWhenHasInterfacesReturnsNames() {
assertThat(get(TestSubclass.class).getInterfaceNames()).containsExactly(TestInterface.class.getName());

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.type.classreading;
import org.springframework.core.type.AbstractAnnotationMetadataTests;
import org.springframework.core.type.AnnotationMetadata;
/**
* Tests for {@link SimpleAnnotationMetadata} and
* {@link SimpleAnnotationMetadataReadingVisitor} on Java < 24,
* and for the ClassFile API variant on Java >= 24.
*
* @author Phillip Webb
*/
class DefaultAnnotationMetadataTests extends AbstractAnnotationMetadataTests {
@Override
protected AnnotationMetadata get(Class<?> source) {
try {
return MetadataReaderFactory.create(source.getClassLoader())
.getMetadataReader(source.getName()).getAnnotationMetadata();
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}

View File

@@ -21,8 +21,7 @@ import org.springframework.core.type.AnnotationMetadata;
/**
* Tests for {@link SimpleAnnotationMetadata} and
* {@link SimpleAnnotationMetadataReadingVisitor} on Java < 24,
* and for the ClassFile API variant on Java >= 24.
* {@link SimpleAnnotationMetadataReadingVisitor}.
*
* @author Phillip Webb
*/
@@ -31,8 +30,9 @@ class SimpleAnnotationMetadataTests extends AbstractAnnotationMetadataTests {
@Override
protected AnnotationMetadata get(Class<?> source) {
try {
return MetadataReaderFactory.create(source.getClassLoader())
.getMetadataReader(source.getName()).getAnnotationMetadata();
return new SimpleMetadataReaderFactory(
source.getClassLoader()).getMetadataReader(
source.getName()).getAnnotationMetadata();
}
catch (Exception ex) {
throw new IllegalStateException(ex);