View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit.internal.util;
7   
8   import edu.umd.cs.findbugs.annotations.NonNull;
9   
10  public final class ClassNaming {
11      private ClassNaming() {
12      }
13  
14      /**
15       * This method was created to work around an issue in the standard {@link Class#isAnonymousClass()} method, which
16       * causes a sibling nested class to be loaded when called on a nested class. If that sibling nested class is not in
17       * the classpath, a <code>ClassNotFoundException</code> would result.
18       * <p>
19       * This method checks only the given class name, never causing any other classes to be loaded.
20       */
21      public static boolean isAnonymousClass(@NonNull Class<?> aClass) {
22          return isAnonymousClass(aClass.getName());
23      }
24  
25      public static boolean isAnonymousClass(@NonNull String className) {
26          int positionJustBefore = className.lastIndexOf('$');
27  
28          if (positionJustBefore <= 0) {
29              return false;
30          }
31  
32          int nextPos = positionJustBefore + 1;
33          int n = className.length();
34  
35          while (nextPos < n) {
36              char c = className.charAt(nextPos);
37              if (c < '0' || c > '9') {
38                  return false;
39              }
40              nextPos++;
41          }
42  
43          return true;
44      }
45  }