1
1
package me .wavelength .betterreflection ;
2
2
3
+ import java .io .File ;
3
4
import java .lang .reflect .Array ;
4
5
import java .lang .reflect .Constructor ;
5
6
import java .lang .reflect .Field ;
6
7
import java .lang .reflect .InvocationTargetException ;
7
8
import java .lang .reflect .Method ;
9
+ import java .security .ProtectionDomain ;
8
10
import java .util .HashMap ;
9
11
import java .util .Map ;
12
+ import java .util .jar .JarFile ;
10
13
11
14
public class BetterReflectionClass {
12
15
@@ -26,6 +29,7 @@ public class BetterReflectionClass {
26
29
* @since 0.6
27
30
*/
28
31
private final String typeName ;
32
+ private final String packageName ;
29
33
30
34
private Class <?> clasz ;
31
35
@@ -36,14 +40,52 @@ public class BetterReflectionClass {
36
40
private final Method [] declaredMethods ;
37
41
private final Method [] methods ;
38
42
43
+ /**
44
+ * @since 0.7
45
+ */
46
+ private final ProtectionDomain protectionDomain ;
47
+
39
48
/**
40
49
* @since 0.6
41
50
*/
42
51
private final Class <?> superClass ;
52
+ /**
53
+ * @since 0.7
54
+ */
55
+ private BetterReflectionClass betterReflectionSuperClass ;
56
+
57
+ /**
58
+ * Null until {@link #isRunningFromJar()} is invoked, then a mirror of
59
+ * {@link BetterReflectionUtils#isRunningFromJar(BetterReflectionClass)}
60
+ *
61
+ * @since 0.7
62
+ */
63
+ private Boolean runningFromJar ;
64
+ /**
65
+ * Null until {@link #getJar()} is invoked, then a mirror of
66
+ * {@link BetterReflectionUtils#getCurrentJar(BetterReflectionClass)}
67
+ *
68
+ * @since 0.7
69
+ */
70
+ private JarFile jar ;
71
+ /**
72
+ * Null until {@link #getJarFile()} is invoked, then a mirror of
73
+ * {@link BetterReflectionUtils#getCurrentJarFile(BetterReflectionClass)}
74
+ *
75
+ * @since 0.7
76
+ */
77
+ private File jarFile ;
43
78
44
79
public BetterReflectionClass (String className ) throws ClassNotFoundException {
45
80
this (Class .forName (className ));
46
81
}
82
+
83
+ /**
84
+ * @since 0.7
85
+ */
86
+ public BetterReflectionClass (BetterReflectionClass clasz ) throws ClassNotFoundException {
87
+ this (clasz .getClasz ());
88
+ }
47
89
48
90
public BetterReflectionClass (Class <?> clasz ) {
49
91
this .clasz = clasz ;
@@ -52,6 +94,7 @@ public BetterReflectionClass(Class<?> clasz) {
52
94
this .simpleName = clasz .getSimpleName ();
53
95
this .canonicalName = clasz .getCanonicalName ();
54
96
this .typeName = clasz .getTypeName ();
97
+ this .packageName = name .contains ("." ) ? name .substring (0 , name .lastIndexOf ("." )) : "" ;
55
98
56
99
this .declaredFields = clasz .getDeclaredFields ();
57
100
this .fields = clasz .getFields ();
@@ -60,6 +103,8 @@ public BetterReflectionClass(Class<?> clasz) {
60
103
this .declaredMethods = clasz .getDeclaredMethods ();
61
104
this .methods = clasz .getMethods ();
62
105
this .superClass = clasz .getSuperclass ();
106
+
107
+ this .protectionDomain = clasz .getProtectionDomain ();
63
108
}
64
109
65
110
/**
@@ -75,6 +120,18 @@ public static BetterReflectionClass forName(String name) {
75
120
}
76
121
}
77
122
123
+ /**
124
+ * @return a BetterReflectionClass wrapping the class' Array. Null if the class
125
+ * is not found.
126
+ * @since 0.7
127
+ */
128
+ public static BetterReflectionClass forNameAsArray (String name ) {
129
+ BetterReflectionClass clasz = forName (name );
130
+ if (clasz == null )
131
+ return null ;
132
+ return clasz .getBetterReflectionArrayClass ();
133
+ }
134
+
78
135
public Class <?> getClasz () {
79
136
return clasz ;
80
137
}
@@ -190,6 +247,10 @@ public Method[] getMethods() {
190
247
return methods ;
191
248
}
192
249
250
+ public ProtectionDomain getProtectionDomain () {
251
+ return protectionDomain ;
252
+ }
253
+
193
254
public Object newInstance () throws InstantiationException , IllegalAccessException {
194
255
return clasz .newInstance ();
195
256
}
@@ -206,7 +267,6 @@ public void invokeMethods(Map<String, Object[]> methods, Object instance) throws
206
267
Object parameter = parameters [i ];
207
268
if (!(parameter instanceof ReflectionParameter ))
208
269
continue ;
209
-
210
270
ReflectionParameter reflectionParameter = (ReflectionParameter ) parameter ;
211
271
if (reflectionParameter .getType ().isPrimitive ()) {
212
272
primitives .put (i , reflectionParameter .getType ());
@@ -230,6 +290,10 @@ public Class<?> getArrayClass() {
230
290
return Array .newInstance (clasz , 0 ).getClass ();
231
291
}
232
292
293
+ public BetterReflectionClass getBetterReflectionArrayClass () {
294
+ return new BetterReflectionClass (Array .newInstance (clasz , 0 ).getClass ());
295
+ }
296
+
233
297
/**
234
298
* @param condition the condition to be met to return an array of the class
235
299
* @return an array of {@link #clasz} if condition is true, {@link #clasz}
@@ -255,6 +319,21 @@ public Object getDeclaredFieldValue(Object instance, String fieldName) throws Il
255
319
public Object invokeMethod (Object instance , String methodName , Object ... parameters ) throws InvocationTargetException , IllegalAccessException {
256
320
return getMethod (methodName , BetterReflectionUtils .getTypes (parameters )).invoke (instance , parameters );
257
321
}
322
+
323
+ public Object invokeDeclaredMethod (Object instance , String methodName , Object ... parameters ) throws InvocationTargetException , IllegalAccessException {
324
+ Method method = getDeclaredMethod (methodName , BetterReflectionUtils .getTypes (parameters ));
325
+ boolean accessible = method .isAccessible ();
326
+ if (!accessible )
327
+ method .setAccessible (true );
328
+ Object result = method .invoke (instance , parameters );
329
+ if (!accessible )
330
+ method .setAccessible (false );
331
+ return result ;
332
+ }
333
+
334
+ public String getPackageName () {
335
+ return packageName ;
336
+ }
258
337
259
338
/**
260
339
* @since 0.6
@@ -263,6 +342,43 @@ public Class<?> getSuperclass() {
263
342
return superClass ;
264
343
}
265
344
345
+ /**
346
+ * @since 0.7
347
+ * @return {@link #getSuperclass()} as {@link BetterReflectionClass}
348
+ */
349
+ public BetterReflectionClass getBetterReflectionSuperClass () {
350
+ if (betterReflectionSuperClass == null )
351
+ betterReflectionSuperClass = new BetterReflectionClass (superClass );
352
+ return betterReflectionSuperClass ;
353
+ }
354
+
355
+ /**
356
+ * @since 0.8
357
+ */
358
+ public boolean isRunningFromJar () {
359
+ if (runningFromJar == null )
360
+ runningFromJar = BetterReflectionUtils .isRunningFromJar (this );
361
+ return runningFromJar ;
362
+ }
363
+
364
+ /**
365
+ * @since 0.8
366
+ */
367
+ public JarFile getJar () {
368
+ if (isRunningFromJar () && jar == null )
369
+ jar = BetterReflectionUtils .getCurrentJar (this );
370
+ return jar ;
371
+ }
372
+
373
+ /**
374
+ * @since 0.8
375
+ */
376
+ public File getJarFile () {
377
+ if (isRunningFromJar () && jarFile == null )
378
+ jarFile = BetterReflectionUtils .getCurrentJarFile (this );
379
+ return jarFile ;
380
+ }
381
+
266
382
public boolean isAssignableFrom (Class <?> clasz ) {
267
383
return this .clasz .isAssignableFrom (clasz );
268
384
}
@@ -271,4 +387,41 @@ public boolean isAssignableFrom(BetterReflectionClass clasz) {
271
387
return isAssignableFrom (clasz .getClasz ());
272
388
}
273
389
390
+ /**
391
+ * Write the all the method headers into stout
392
+ *
393
+ * @since 0.7
394
+ */
395
+ public void dumpMethodHeaders () {
396
+ dumpMethodHeaders (true );
397
+ }
398
+
399
+ /**
400
+ * Write the all the method headers into stout
401
+ *
402
+ * @since 0.7
403
+ */
404
+ public void dumpMethodHeaders (boolean includeModifiers ) {
405
+ dumpMethodHeaders (includeModifiers , true );
406
+ }
407
+
408
+ /**
409
+ * Write the all the method headers into stout
410
+ *
411
+ * @since 0.7
412
+ */
413
+ public void dumpMethodHeaders (boolean includeModifiers , boolean includeReturnType ) {
414
+ dumpMethodHeaders (includeModifiers , includeReturnType , true );
415
+ }
416
+
417
+ /**
418
+ * Write the all the method headers into stout
419
+ *
420
+ * @since 0.7
421
+ */
422
+ public void dumpMethodHeaders (boolean includeModifiers , boolean includeReturnType , boolean includeParameterNames ) {
423
+ for (Method method : getDeclaredMethods ())
424
+ BetterReflectionUtils .dumpMethodHeader (method , includeModifiers , includeReturnType , includeParameterNames );
425
+ }
426
+
274
427
}
0 commit comments