@ -868,6 +868,49 @@ class ClassUtilsTests {
@@ -868,6 +868,49 @@ class ClassUtilsTests {
assertPubliclyAccessible ( publiclyAccessibleMethod ) ;
}
@Test // gh-35667
void staticMethodInPublicClass ( ) throws Exception {
Method originalMethod = PublicSuperclass . class . getMethod ( "getCacheKey" ) ;
// Prerequisite: method must be public static for this use case.
assertPublic ( originalMethod ) ;
assertStatic ( originalMethod ) ;
Method publiclyAccessibleMethod = ClassUtils . getPubliclyAccessibleMethodIfPossible ( originalMethod , null ) ;
assertThat ( publiclyAccessibleMethod ) . isSameAs ( originalMethod ) ;
assertPubliclyAccessible ( publiclyAccessibleMethod ) ;
}
@Test // gh-35667
void publicSubclassHidesStaticMethodInPublicSuperclass ( ) throws Exception {
Method originalMethod = PublicSubclass . class . getMethod ( "getCacheKey" ) ;
// Prerequisite: type must be public for this use case.
assertPublic ( originalMethod . getDeclaringClass ( ) ) ;
// Prerequisite: method must be public static for this use case.
assertPublic ( originalMethod ) ;
assertStatic ( originalMethod ) ;
Method publiclyAccessibleMethod = ClassUtils . getPubliclyAccessibleMethodIfPossible ( originalMethod , null ) ;
assertThat ( publiclyAccessibleMethod ) . isSameAs ( originalMethod ) ;
assertPubliclyAccessible ( publiclyAccessibleMethod ) ;
}
@Test // gh-35667
void privateSubclassHidesStaticMethodInPublicSuperclass ( ) throws Exception {
Method originalMethod = PrivateSubclass . class . getMethod ( "getCacheKey" ) ;
// Prerequisite: type must not be public for this use case.
assertNotPublic ( originalMethod . getDeclaringClass ( ) ) ;
// Prerequisite: method must be public static for this use case.
assertPublic ( originalMethod ) ;
assertStatic ( originalMethod ) ;
Method publiclyAccessibleMethod = ClassUtils . getPubliclyAccessibleMethodIfPossible ( originalMethod , null ) ;
assertThat ( publiclyAccessibleMethod ) . isSameAs ( originalMethod ) ;
assertNotPubliclyAccessible ( publiclyAccessibleMethod ) ;
}
}
@ -914,6 +957,10 @@ class ClassUtilsTests {
@@ -914,6 +957,10 @@ class ClassUtilsTests {
return Modifier . isPublic ( member . getModifiers ( ) ) ;
}
private static void assertStatic ( Member member ) {
assertThat ( Modifier . isStatic ( member . getModifiers ( ) ) ) . as ( "%s must be static" , member ) . isTrue ( ) ;
}
@Target ( ElementType . METHOD )
@Retention ( RetentionPolicy . RUNTIME )
@ -1048,8 +1095,27 @@ class ClassUtilsTests {
@@ -1048,8 +1095,27 @@ class ClassUtilsTests {
String greet ( String name ) ;
}
public static class PublicSubclass extends PublicSuperclass {
/ * *
* This method intentionally has the exact same signature as
* { @link PublicSuperclass # getCacheKey ( ) } .
* /
public static String getCacheKey ( ) {
return "child" ;
}
}
private static class PrivateSubclass extends PublicSuperclass implements PublicInterface , PrivateInterface {
/ * *
* This method intentionally has the exact same signature as
* { @link PublicSuperclass # getCacheKey ( ) } .
* /
public static String getCacheKey ( ) {
return "child" ;
}
@Override
public int getNumber ( ) {
return 2 ;