1818
1919import java .lang .reflect .Method ;
2020import java .lang .reflect .Modifier ;
21+ import java .util .ArrayDeque ;
2122import java .util .ArrayList ;
2223import java .util .Collections ;
2324import java .util .Comparator ;
25+ import java .util .Deque ;
2426import java .util .HashMap ;
2527import java .util .HashSet ;
2628import java .util .Iterator ;
@@ -1011,11 +1013,6 @@ public static String getShortCanonicalName(final String canonicalName) {
10111013 /**
10121014 * Gets the class name minus the package name from a {@link Class}.
10131015 *
1014- * <p>
1015- * This method simply gets the name using {@code Class.getName()} and then calls {@link #getShortClassName(String)}. See
1016- * relevant notes there.
1017- * </p>
1018- *
10191016 * @param cls the class to get the short name for.
10201017 * @return the class name without the package name or an empty string. If the class is an inner class then the returned
10211018 * value will contain the outer class or classes separated with {@code .} (dot) character.
@@ -1024,17 +1021,37 @@ public static String getShortClassName(final Class<?> cls) {
10241021 if (cls == null ) {
10251022 return StringUtils .EMPTY ;
10261023 }
1027- return getShortClassName (cls .getName ());
1024+ int dim = 0 ;
1025+ Class <?> c = cls ;
1026+ while (c .isArray ()) {
1027+ dim ++;
1028+ c = c .getComponentType ();
1029+ }
1030+
1031+ final String base ;
1032+ // Preserve legacy behavior for anonymous/local classes (keeps compiler ordinals: $13, $10Named, etc.)
1033+ if (c .isAnonymousClass () || c .isLocalClass ()) {
1034+ base = getShortClassName (c .getName ());
1035+ } else {
1036+ final Deque <String > parts = new ArrayDeque <>();
1037+ Class <?> x = c ;
1038+ while (x != null ) {
1039+ parts .push (x .getSimpleName ());
1040+ x = x .getDeclaringClass ();
1041+ }
1042+ base = String .join (String .valueOf (PACKAGE_SEPARATOR_CHAR ), parts );
1043+ }
1044+
1045+ final StringBuilder sb = new StringBuilder (base );
1046+ for (int i = 0 ; i < dim ; i ++) {
1047+ sb .append ("[]" );
1048+ }
1049+ return sb .toString ();
10281050 }
10291051
10301052 /**
10311053 * Gets the class name of the {@code object} without the package name or names.
10321054 *
1033- * <p>
1034- * The method looks up the class of the object and then converts the name of the class invoking
1035- * {@link #getShortClassName(Class)} (see relevant notes there).
1036- * </p>
1037- *
10381055 * @param object the class to get the short name for, may be {@code null}.
10391056 * @param valueIfNull the value to return if the object is {@code null}.
10401057 * @return the class name of the object without the package name, or {@code valueIfNull} if the argument {@code object}
0 commit comments