11package com .mendix .mendixnative .util ;
22
3+ import java .lang .reflect .Constructor ;
34import java .lang .reflect .Field ;
5+ import java .lang .reflect .InvocationTargetException ;
6+ import java .lang .reflect .Method ;
47
58public class ReflectionUtils {
69 private static Field findDeclaredField (Class <?> objectClass , String ... fieldNames ) {
@@ -17,17 +20,76 @@ private static Field findDeclaredField(Class<?> objectClass, String... fieldName
1720 throw new RuntimeException (lastException );
1821 }
1922
23+ public static ConstructorWrapper findConstructor (String className , Class <?>... parameterTypes ) {
24+ try {
25+ Constructor constructor = Class .forName (className ).getDeclaredConstructor (parameterTypes );
26+ constructor .setAccessible (true );
27+ return new ConstructorWrapper (constructor );
28+ } catch (ClassNotFoundException | NoSuchMethodException e ) {
29+ throw new RuntimeException (e );
30+ }
31+ }
32+
33+ // TODO: replace this with a lambda after upgrading to Java 8
34+ public static class ConstructorWrapper {
35+ private final Constructor constructor ;
36+
37+ ConstructorWrapper (Constructor constructor ) {
38+ this .constructor = constructor ;
39+ }
40+
41+ public <T > T newInstance (Object ... args ) {
42+ try {
43+ return (T ) constructor .newInstance (args );
44+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException e ) {
45+ throw new RuntimeException (e );
46+ }
47+ }
48+ }
49+
50+ public static MethodWrapper findMethod (Object object , String methodName , Class <?>... parameterTypes ) {
51+ try {
52+ Method method = object .getClass ().getDeclaredMethod (methodName , parameterTypes );
53+ method .setAccessible (true );
54+ return new MethodWrapper (method , object );
55+ } catch (NoSuchMethodException e ) {
56+ throw new RuntimeException (e );
57+ }
58+ }
59+
60+ // TODO: replace this with a lambda after upgrading to Java 8
61+ public static class MethodWrapper {
62+ private final Method method ;
63+ private final Object object ;
64+
65+ MethodWrapper (Method method , Object object ) {
66+ this .method = method ;
67+ this .object = object ;
68+ }
69+
70+ public void invoke (Object ... args ) {
71+ try {
72+ method .invoke (object , args );
73+ } catch (IllegalAccessException | InvocationTargetException e ) {
74+ throw new RuntimeException (e );
75+ }
76+ }
77+ }
78+
79+ public static void setFieldOfSuperclass (Object object , String fieldName , Object value ) {
80+ setFieldOfSuperclass (object , value , fieldName );
81+ }
82+
2083 public static void setFieldOfSuperclass (Object object , Object value , String ... fieldNames ) {
2184 Field field = findDeclaredField (object .getClass ().getSuperclass (), fieldNames );
2285 setField (object , field , value );
2386 }
2487
25- public static < T > T getFieldOfSuperclass (Object object , String ... fieldNames ) {
88+ public static void setField (Object object , String fieldName , Object value ) {
2689 try {
27- Field field = findDeclaredField (object .getClass ().getSuperclass (), fieldNames );
28- field .setAccessible (true );
29- return (T ) field .get (object );
30- } catch (IllegalAccessException | ClassCastException e ) {
90+ Field field = object .getClass ().getDeclaredField (fieldName );
91+ setField (object , field , value );
92+ } catch (NoSuchFieldException e ) {
3193 throw new RuntimeException (e );
3294 }
3395 }
@@ -42,4 +104,29 @@ private static void setField(Object object, Field field, Object value) {
42104 field .setAccessible (false );
43105 }
44106 }
107+
108+ public static <T > T getFieldOfSuperclass (Object object , String fieldName ) {
109+ return getFieldOfSuperclass (object , new String [] { fieldName });
110+ }
111+
112+ public static <T > T getFieldOfSuperclass (Object object , String ... fieldNames ) {
113+ try {
114+ Field field = findDeclaredField (object .getClass ().getSuperclass (), fieldNames );
115+ field .setAccessible (true );
116+ return (T ) field .get (object );
117+ } catch (IllegalAccessException | ClassCastException e ) {
118+ throw new RuntimeException (e );
119+ }
120+ }
121+
122+ public static <T > T getField (Object object , String fieldName ) {
123+ try {
124+ Field field = object .getClass ().getDeclaredField (fieldName );
125+ field .setAccessible (true );
126+ return (T ) field .get (object );
127+ } catch (NoSuchFieldException | IllegalAccessException | ClassCastException e ) {
128+ throw new RuntimeException (e );
129+ }
130+ }
131+
45132}
0 commit comments