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