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   import edu.umd.cs.findbugs.annotations.Nullable;
10  
11  public final class ObjectMethods {
12      private ObjectMethods() {
13      }
14  
15      @NonNull
16      public static String objectIdentity(@NonNull Object obj) {
17          return obj.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(obj));
18      }
19  
20      @Nullable
21      public static Object evaluateOverride(@NonNull Object obj, @NonNull String methodNameAndDesc,
22              @NonNull Object[] args) {
23          if ("equals(Ljava/lang/Object;)Z".equals(methodNameAndDesc)) {
24              return obj == args[0];
25          }
26          if ("hashCode()I".equals(methodNameAndDesc)) {
27              return System.identityHashCode(obj);
28          }
29          if ("toString()Ljava/lang/String;".equals(methodNameAndDesc)) {
30              return objectIdentity(obj);
31          }
32          if (args.length == 1 && methodNameAndDesc.startsWith("compareTo(L") && methodNameAndDesc.endsWith(";)I")
33                  && obj instanceof Comparable<?>) {
34              Object arg = args[0];
35  
36              if (obj == arg) {
37                  return 0;
38              }
39  
40              return System.identityHashCode(obj) > System.identityHashCode(arg) ? 1 : -1;
41          }
42  
43          return null;
44      }
45  
46      public static boolean isMethodFromObject(@NonNull String name, @NonNull String desc) {
47          return "equals".equals(name) && "(Ljava/lang/Object;)Z".equals(desc)
48                  || "hashCode".equals(name) && "()I".equals(desc)
49                  || "toString".equals(name) && "()Ljava/lang/String;".equals(desc)
50                  || "finalize".equals(name) && "()V".equals(desc);
51      }
52  }