-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionUtils.java
More file actions
113 lines (85 loc) · 3.19 KB
/
Copy pathReflectionUtils.java
File metadata and controls
113 lines (85 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package ru.cwcode.cwutils.reflection;
import java.io.File;
import java.lang.reflect.*;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ReflectionUtils {
public static <T> T getNewInstance(Class<T> type, Object... parameters) {
try {
Class<?>[] classes = new Class<?>[parameters.length];
for (int i = 0; i < parameters.length; i++) {
classes[i] = parameters[i].getClass();
}
Constructor<T> constructor = type.getDeclaredConstructor(classes);
constructor.setAccessible(true);
return constructor.newInstance(parameters);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
public static <T> T getFieldValue(Object object, String fieldName, Class<T> fieldType) {
try {
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return fieldType.cast(field.get(object));
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
public static Set<Class<?>> getClasses(File jarFile, String packageName) {
return getClasses(jarFile, packageName, name -> true);
}
public static Set<Class<?>> getClasses(File jarFile, String packageName, Predicate<String> filter) {
Set<Class<?>> classes = new HashSet<>();
try {
JarFile file = new JarFile(jarFile);
for (Enumeration<JarEntry> entry = file.entries(); entry.hasMoreElements(); ) {
JarEntry jarEntry = entry.nextElement();
String name = jarEntry.getName().replace("/", ".");
if (name.startsWith(packageName) && name.endsWith(".class") && filter.test(name)) {
classes.add(Class.forName(name.substring(0, name.length() - 6)));
}
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
return classes;
}
public static Set<String> findMatchingResources(File jarFile, Predicate<String> filter) {
Set<String> resources = new HashSet<>();
try {
JarFile file = new JarFile(jarFile);
for (Enumeration<JarEntry> entry = file.entries(); entry.hasMoreElements(); ) {
JarEntry jarEntry = entry.nextElement();
String name = jarEntry.getName();
if (!name.endsWith(".class") && filter.test(name)) {
resources.add(name);
}
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
return resources;
}
public static boolean tryToInvokeStaticMethod(Class<?> classInfo, String methodName) {
try {
Method load;
load = classInfo.getDeclaredMethod(methodName);
if (!Modifier.isStatic(load.getModifiers()) || load.getParameterCount() != 0) {
return false;
}
load.invoke(null);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
return false;
}
return true;
}
}