-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathClassScanInReflection.java
More file actions
55 lines (46 loc) · 1.33 KB
/
ClassScanInReflection.java
File metadata and controls
55 lines (46 loc) · 1.33 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
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.TypeVariable;
class A
{
int b;
private int c;
static int d;
A(int b, int c ){
this.b = b;
this.c = c;
}
private void print(){
System.out.println("this is print");
}
void show(){
}
}
public class ClassScanInReflection {
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
// TODO Auto-generated method stub
A obj = new A(10,20);
Class cls2 = obj.getClass(); // Data + MetaData
Method method = cls2.getDeclaredMethod("print", null);
method.setAccessible(true);
method.invoke(obj, null);
Class cls = A.class;
Field fields[] = cls.getDeclaredFields();
Method methods [] = cls.getDeclaredMethods();
for(Method m: methods){
TypeVariable t [] = m.getTypeParameters();
Class c = t.getClass();
Method methodA = cls.getDeclaredMethod(m.getName(),c);
methodA.invoke(obj, 10,20);
}
for(Field field :fields){
if(!Modifier.isPrivate(field.getModifiers())){
System.out.println("these fields are not private "+field.getName());
}
}
System.out.println(fields.length);
System.out.println(methods.length);
}
}