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  import java.lang.annotation.Annotation;
12  import java.lang.reflect.AccessibleObject;
13  import java.lang.reflect.GenericArrayType;
14  import java.lang.reflect.Method;
15  import java.lang.reflect.ParameterizedType;
16  import java.lang.reflect.Type;
17  import java.lang.reflect.TypeVariable;
18  import java.lang.reflect.WildcardType;
19  import java.net.URLDecoder;
20  import java.nio.charset.StandardCharsets;
21  import java.security.CodeSource;
22  import java.util.List;
23  
24  /**
25   * Miscellaneous utility constants and methods.
26   */
27  public final class Utilities {
28      @NonNull
29      public static final Object[] NO_ARGS = {};
30      public static final boolean JAVA8;
31      public static final boolean HOTSPOT_VM;
32  
33      static {
34          float javaVersion = Float.parseFloat(System.getProperty("java.specification.version"));
35          JAVA8 = javaVersion >= 1.8F;
36          String vmName = System.getProperty("java.vm.name");
37          HOTSPOT_VM = vmName.contains("HotSpot") || vmName.contains("OpenJDK");
38      }
39  
40      private Utilities() {
41      }
42  
43      public static void ensureThatMemberIsAccessible(@NonNull AccessibleObject classMember) {
44          // noinspection deprecation
45          if (!classMember.isAccessible()) {
46              classMember.setAccessible(true);
47          }
48      }
49  
50      public static Method getAnnotatedMethod(Class<?> cls, Class<? extends Annotation> annotation) {
51          for (Method method : cls.getMethods()) {
52              if (method.getAnnotation(annotation) != null) {
53                  return method;
54              }
55          }
56          return null;
57      }
58  
59      public static Method getAnnotatedDeclaredMethod(Class<?> cls, Class<? extends Annotation> annotation) {
60          for (Method method : cls.getDeclaredMethods()) {
61              if (method.getAnnotation(annotation) != null) {
62                  return method;
63              }
64          }
65          return null;
66      }
67  
68      @NonNull
69      public static Class<?> getClassType(@NonNull Type declaredType) {
70          while (true) {
71              if (declaredType instanceof Class<?>) {
72                  return (Class<?>) declaredType;
73              }
74  
75              if (declaredType instanceof ParameterizedType) {
76                  return (Class<?>) ((ParameterizedType) declaredType).getRawType();
77              }
78  
79              if (declaredType instanceof GenericArrayType) {
80                  declaredType = ((GenericArrayType) declaredType).getGenericComponentType();
81                  continue;
82              }
83  
84              if (declaredType instanceof TypeVariable) {
85                  declaredType = ((TypeVariable<?>) declaredType).getBounds()[0];
86                  continue;
87              }
88  
89              if (declaredType instanceof WildcardType) {
90                  declaredType = ((WildcardType) declaredType).getUpperBounds()[0];
91                  continue;
92              }
93  
94              throw new IllegalArgumentException("Type of unexpected kind: " + declaredType);
95          }
96      }
97  
98      public static boolean containsReference(@NonNull List<?> references, @Nullable Object toBeFound) {
99          for (Object reference : references) {
100             if (reference == toBeFound) {
101                 return true;
102             }
103         }
104 
105         return false;
106     }
107 
108     @NonNull
109     public static String getClassFileLocationPath(@NonNull Class<?> aClass) {
110         CodeSource codeSource = aClass.getProtectionDomain().getCodeSource();
111         return getClassFileLocationPath(codeSource);
112     }
113 
114     @NonNull
115     public static String getClassFileLocationPath(@NonNull CodeSource codeSource) {
116         String locationPath = codeSource.getLocation().getPath();
117         return URLDecoder.decode(locationPath, StandardCharsets.UTF_8);
118     }
119 }