Biblioteca Java - Blame information for rev 32
Subversion Repositories:
(root)/Courses and labs samples/ISP/Exemple_ISP_Cluj_2015/ExempleReflexie/src/exemple/reflexie/clase/InspectClass.java
Rev | Author | Line No. | Line |
---|---|---|---|
32 | mihai | 1 | /* |
2 | * To change this license header, choose License Headers in Project Properties. | ||
3 | * To change this template file, choose Tools | Templates | ||
4 | * and open the template in the editor. | ||
5 | */ | ||
6 | package exemple.reflexie.clase; | ||
7 | |||
8 | import exemple.reflexie.app.Car; | ||
9 | import java.lang.reflect.Constructor; | ||
10 | import java.lang.reflect.Field; | ||
11 | import java.lang.reflect.Method; | ||
12 | import java.lang.reflect.Modifier; | ||
13 | |||
14 | /** | ||
15 | * | ||
16 | * @author Mihai Hulea mihai.hulea@aut.utcluj.ro | ||
17 | */ | ||
18 | public class InspectClass { | ||
19 | |||
20 | public static void describeInstance(Object object) { | ||
21 | Class<?> clazz = object.getClass(); | ||
22 | |||
23 | Constructor<?>[] constructors = clazz.getDeclaredConstructors(); | ||
24 | Field[] fields = clazz.getDeclaredFields(); | ||
25 | Method[] methods = clazz.getDeclaredMethods(); | ||
26 | |||
27 | System.out.println("Description for class: " + clazz.getName()); | ||
28 | System.out.println(); | ||
29 | System.out.println("Summary"); | ||
30 | System.out.println("-----------------------------------------"); | ||
31 | System.out.println("Constructors: " + (constructors.length)); | ||
32 | System.out.println("Fields: " + (fields.length)); | ||
33 | System.out.println("Methods: " + (methods.length)); | ||
34 | |||
35 | System.out.println(); | ||
36 | System.out.println(); | ||
37 | System.out.println("Details"); | ||
38 | System.out.println("-----------------------------------------"); | ||
39 | |||
40 | if (constructors.length > 0) { | ||
41 | System.out.println(); | ||
42 | System.out.println("Constructors:"); | ||
43 | for (Constructor<?> constructor : constructors) { | ||
44 | System.out.println(constructor); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | if (fields.length > 0) { | ||
49 | System.out.println(); | ||
50 | System.out.println("Fields:"); | ||
51 | for (Field field : fields) { | ||
52 | System.out.println(field); | ||
53 | } | ||
54 | } | ||
55 | |||
56 | if (methods.length > 0) { | ||
57 | System.out.println(); | ||
58 | System.out.println("Methods:"); | ||
59 | for (Method method : methods) { | ||
60 | System.out.println(method); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | static boolean[] checkModifiers(int modifiers){ | ||
65 | boolean[] b = new boolean[11]; | ||
66 | b[0] = Modifier.isAbstract(modifiers); | ||
67 | b[1] = Modifier.isFinal(modifiers); | ||
68 | b[2] = Modifier.isInterface(modifiers); | ||
69 | b[3] = Modifier.isNative(modifiers); | ||
70 | b[4] = Modifier.isPrivate(modifiers); | ||
71 | b[5] = Modifier.isProtected(modifiers); | ||
72 | b[6] = Modifier.isPublic(modifiers); | ||
73 | b[6] = Modifier.isStatic(modifiers); | ||
74 | b[7] = Modifier.isStrict(modifiers); | ||
75 | b[8] = Modifier.isSynchronized(modifiers); | ||
76 | b[9] = Modifier.isTransient(modifiers); | ||
77 | b[10] = Modifier.isVolatile(modifiers); | ||
78 | return b; | ||
79 | } | ||
80 | |||
81 | public static void main(String[] args) throws Exception{ | ||
82 | Car myCar = new Car(); | ||
83 | |||
84 | // Class c = Car.class; | ||
85 | // describeInstance(myCar); | ||
86 | /* System.out.println("Class name:"+c.getName()); | ||
87 | |||
88 | Class s = c.getSuperclass(); | ||
89 | System.out.println("Superclass name:"+s.getName()); | ||
90 | */ | ||
91 | |||
92 | System.out.println("My car is:"+myCar); | ||
93 | |||
94 | Method privateMethod = Car.class. | ||
95 | getDeclaredMethod("setSpeedLimit", Integer.class); | ||
96 | privateMethod.setAccessible(true); | ||
97 | privateMethod.invoke(myCar, 300); | ||
98 | |||
99 | System.out.println("My car is:"+myCar); | ||
100 | |||
101 | Field privateField = Car.class. | ||
102 | getDeclaredField("maxSpeed"); | ||
103 | privateField.setAccessible(true); | ||
104 | Integer fieldValue = (Integer) privateField.get(myCar); | ||
105 | |||
106 | System.out.println("Max car speed is = " + fieldValue); | ||
107 | |||
108 | Car o = (Car)Car.class.getConstructor().newInstance(); | ||
109 | describeInstance(o); | ||
110 | o.accelerate(); | ||
111 | |||
112 | } | ||
113 | } |