11package me .wavelength .betterreflection ;
22
3+ import java .io .File ;
34import java .lang .reflect .Array ;
45import java .lang .reflect .Constructor ;
56import java .lang .reflect .Field ;
67import java .lang .reflect .InvocationTargetException ;
78import java .lang .reflect .Method ;
9+ import java .security .ProtectionDomain ;
810import java .util .HashMap ;
911import java .util .Map ;
12+ import java .util .jar .JarFile ;
1013
1114public class BetterReflectionClass {
1215
@@ -26,6 +29,7 @@ public class BetterReflectionClass {
2629 * @since 0.6
2730 */
2831 private final String typeName ;
32+ private final String packageName ;
2933
3034 private Class <?> clasz ;
3135
@@ -36,14 +40,52 @@ public class BetterReflectionClass {
3640 private final Method [] declaredMethods ;
3741 private final Method [] methods ;
3842
43+ /**
44+ * @since 0.7
45+ */
46+ private final ProtectionDomain protectionDomain ;
47+
3948 /**
4049 * @since 0.6
4150 */
4251 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 ;
4378
4479 public BetterReflectionClass (String className ) throws ClassNotFoundException {
4580 this (Class .forName (className ));
4681 }
82+
83+ /**
84+ * @since 0.7
85+ */
86+ public BetterReflectionClass (BetterReflectionClass clasz ) throws ClassNotFoundException {
87+ this (clasz .getClasz ());
88+ }
4789
4890 public BetterReflectionClass (Class <?> clasz ) {
4991 this .clasz = clasz ;
@@ -52,6 +94,7 @@ public BetterReflectionClass(Class<?> clasz) {
5294 this .simpleName = clasz .getSimpleName ();
5395 this .canonicalName = clasz .getCanonicalName ();
5496 this .typeName = clasz .getTypeName ();
97+ this .packageName = name .contains ("." ) ? name .substring (0 , name .lastIndexOf ("." )) : "" ;
5598
5699 this .declaredFields = clasz .getDeclaredFields ();
57100 this .fields = clasz .getFields ();
@@ -60,6 +103,8 @@ public BetterReflectionClass(Class<?> clasz) {
60103 this .declaredMethods = clasz .getDeclaredMethods ();
61104 this .methods = clasz .getMethods ();
62105 this .superClass = clasz .getSuperclass ();
106+
107+ this .protectionDomain = clasz .getProtectionDomain ();
63108 }
64109
65110 /**
@@ -75,6 +120,18 @@ public static BetterReflectionClass forName(String name) {
75120 }
76121 }
77122
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+
78135 public Class <?> getClasz () {
79136 return clasz ;
80137 }
@@ -190,6 +247,10 @@ public Method[] getMethods() {
190247 return methods ;
191248 }
192249
250+ public ProtectionDomain getProtectionDomain () {
251+ return protectionDomain ;
252+ }
253+
193254 public Object newInstance () throws InstantiationException , IllegalAccessException {
194255 return clasz .newInstance ();
195256 }
@@ -206,7 +267,6 @@ public void invokeMethods(Map<String, Object[]> methods, Object instance) throws
206267 Object parameter = parameters [i ];
207268 if (!(parameter instanceof ReflectionParameter ))
208269 continue ;
209-
210270 ReflectionParameter reflectionParameter = (ReflectionParameter ) parameter ;
211271 if (reflectionParameter .getType ().isPrimitive ()) {
212272 primitives .put (i , reflectionParameter .getType ());
@@ -230,6 +290,10 @@ public Class<?> getArrayClass() {
230290 return Array .newInstance (clasz , 0 ).getClass ();
231291 }
232292
293+ public BetterReflectionClass getBetterReflectionArrayClass () {
294+ return new BetterReflectionClass (Array .newInstance (clasz , 0 ).getClass ());
295+ }
296+
233297 /**
234298 * @param condition the condition to be met to return an array of the class
235299 * @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
255319 public Object invokeMethod (Object instance , String methodName , Object ... parameters ) throws InvocationTargetException , IllegalAccessException {
256320 return getMethod (methodName , BetterReflectionUtils .getTypes (parameters )).invoke (instance , parameters );
257321 }
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+ }
258337
259338 /**
260339 * @since 0.6
@@ -263,6 +342,43 @@ public Class<?> getSuperclass() {
263342 return superClass ;
264343 }
265344
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+
266382 public boolean isAssignableFrom (Class <?> clasz ) {
267383 return this .clasz .isAssignableFrom (clasz );
268384 }
@@ -271,4 +387,41 @@ public boolean isAssignableFrom(BetterReflectionClass clasz) {
271387 return isAssignableFrom (clasz .getClasz ());
272388 }
273389
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+
274427}
0 commit comments