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