-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inspector.java
188 lines (165 loc) · 4.84 KB
/
Inspector.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class Inspector
{
Object obj = null;
Class<?> objClass = null;
boolean recursive = false;
LinkedList<Field> fields = new LinkedList<Field>();
public void inspect(Object obj, boolean recursive) throws IllegalArgumentException, IllegalAccessException
{
this.obj = obj;
this.recursive = recursive;
objClass = obj.getClass();
System.out.println("Name = " + getDeclaringClass());
System.out.println("SuperClass = " + getSuper());
// Theses methods print out everything and return what's included//
// for the purposes of this program I will just be calling them to print the output //
getInterfaces();
getMethods();
getConstructors();
System.out.println("Fields:");
getFields(objClass, obj);
}
public Method[] getMethods()
{
printMethods(objClass.getMethods());
return objClass.getMethods();
}
private LinkedList getFields(Class<?> objClass, Object obj) throws IllegalArgumentException, IllegalAccessException
{
Field[] subFields;
subFields = objClass.getDeclaredFields();
for (int x =0; x < subFields.length; x++)
{
subFields[x].setAccessible(true);
fields.add(subFields[x]);
System.out.print(" " + subFields[x].getName());
System.out.print(" with type " + subFields[x].getType() );
System.out.print(" modifiers " + Modifier.toString(subFields[x].getModifiers()));
if(subFields[x].getType().isPrimitive())
{
System.out.print(" with value " + subFields[x].get(obj));
}
else
{
Object cur = subFields[x].get(obj);
if (cur == null) System.out.print(" with null value");
else if(cur instanceof Iterable || cur.getClass().isArray())
{
try
{
@SuppressWarnings("rawtypes")
List list = Arrays.asList(cur);
for (Object y : list)
{
System.out.print(" with Value of " + y + " ");
}
}
catch (Exception e)
{
System.out.println("");
}
}
else
{
if (recursive == false)
{
System.out.print(" with value " + cur);
}
else
{
System.out.print("\n<Containing>\n");
getFields(cur.getClass(), cur);
System.out.print("<Containing\\>\n");
}
}
}
System.out.println();
}
if (objClass.getSuperclass()!= null)getFields(objClass.getSuperclass(), obj );
return fields;
}
/* private void fieldsInvolved(Field field)
{
boolean iterable = false;
System.out.println();
Class<?>[] interfaces = field.getClass().getInterfaces();
}*/
public Constructor[] getConstructors()
{
Constructor[] constructors = objClass.getConstructors();
for(int x = 0; x < constructors.length; x++ )
{
System.out.println("Constructor " + x + " : ");
Parameter[] parameters = constructors[x].getParameters();
if (parameters.length >= 1)
{
System.out.print(" Parameters: ");
for (int y = 0; y < parameters.length; y++)
{
System.out.print(parameters[y].getType());
if (y != parameters.length - 1) System.out.print(", ");
System.out.println();
}
}
else System.out.println(" No Parameters");
System.out.println(" Modifiers: " + Modifier.toString(constructors[x].getModifiers()));
}
return constructors;
}
public String getDeclaringClass()
{
return obj.getClass().getName();
}
public String getSuper()
{
return objClass.getSuperclass().getName();
}
public Class<?>[] getInterfaces()
{
Class<?>[] interfaces = objClass.getInterfaces();
if(interfaces.length >0)
{
System.out.print("Interfaces = ");
for (int x = 0; x < interfaces.length; x++)
{
System.out.print(interfaces[x].getName());
if(x != interfaces.length) System.out.print(", ");
}
System.out.println();
}
else System.out.println("No Interfaces");
return interfaces;
}
private void printMethods(Method[] methods)
{
for (Method x : methods)
{
System.out.println("Method " + x.getName());
System.out.println(" With Exceptions: " + x.getExceptionTypes().getClass().getName());
printParameters(x);
System.out.println(" Return Type: " + x.getReturnType());
System.out.println(" Modifiers: " + Modifier.toString(x.getModifiers()));
}
}
private void printParameters(Method x)
{
Parameter[] parameters = x.getParameters();
if (parameters.length >= 1)
{
System.out.print(" Parameters: ");
for (int y = 0; y < parameters.length; y++)
{
System.out.print(parameters[y].getType());
if (y != parameters.length - 1) System.out.print(", ");
}
System.out.println();
}
else System.out.println(" No Parameters");
}
}