MCPcopy Create free account
hub / github.com/Ape1ron/SpringAopInDeserializationDemo1 / Reflections

Class Reflections

src/main/java/Reflections.java:14–193  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12
13
14public class Reflections {
15
16 public static Field getField(Class clazz,String fieldName) throws NoSuchFieldException {
17
18 while (true){
19 Field[] fields = clazz.getDeclaredFields();
20 for(Field field:fields){
21 if(field.getName().equals(fieldName)){
22 return field;
23 }
24 }
25 if(clazz == Object.class){
26 break;
27 }
28 clazz = clazz.getSuperclass();
29 }
30 throw new NoSuchFieldException(fieldName);
31 }
32
33
34 public static Object getFieldValue(Object obj,String filedName) throws NoSuchFieldException, IllegalAccessException {
35 Field field = getField(obj.getClass(),filedName);
36 field.setAccessible(true);
37 return field.get(obj);
38 }
39
40 public static void setFieldValue(Object obj,String filedName,Object value) throws NoSuchFieldException, IllegalAccessException {
41 Field field = getField(obj.getClass(),filedName);
42 field.setAccessible(true);
43 field.set(obj,value);
44 }
45
46 public static Method getMethod(Class clazz,String methodName,Class... paramTypes) throws NoSuchFieldException {
47
48 while (true){
49 Method[] methods = clazz.getDeclaredMethods();
50 for(Method method:methods){
51 if(method.getName().equals(methodName) && Arrays.equals(paramTypes, method.getParameterTypes())){
52 method.setAccessible(true);
53 return method;
54 }
55 }
56 if(clazz == Object.class){
57 break;
58 }
59 clazz = clazz.getSuperclass();
60 }
61 throw new NoSuchFieldException(methodName);
62 }
63
64
65 public static boolean hasField(Class clazz,String fieldName) {
66 try {
67 getField(clazz,fieldName);
68 return true;
69 }catch (Exception e){
70 return false;
71 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected