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