1 package org.apache.maven.surefire.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25
26
27
28
29 public class ReflectionUtils
30 {
31 private static final Class[] NO_ARGS = new Class[0];
32
33 private static final Object[] NO_ARGS_VALUES = new Object[0];
34
35
36 public static Method getMethod( Object instance, String methodName, Class[] parameters )
37 {
38 return getMethod( instance.getClass(), methodName, parameters );
39 }
40
41 public static Method getMethod( Class clazz, String methodName, Class[] parameters )
42 {
43 try
44 {
45 return clazz.getMethod( methodName, parameters );
46 }
47 catch ( NoSuchMethodException e )
48 {
49 throw new NestedRuntimeException( "When finding method " + methodName, e );
50 }
51 }
52
53 public static Method tryGetMethod( Class clazz, String methodName, Class[] parameters )
54 {
55 try
56 {
57 return clazz.getMethod( methodName, parameters );
58 }
59 catch ( NoSuchMethodException e )
60 {
61 return null;
62 }
63 }
64
65
66 public static Object invokeGetter( Object instance, String methodName )
67 {
68 final Method method = getMethod( instance, methodName, NO_ARGS );
69 return invokeMethodWithArray( instance, method, NO_ARGS_VALUES );
70 }
71
72 public static Constructor getConstructor( Class clazz, Class[] arguments )
73 {
74 try
75 {
76 return clazz.getConstructor( arguments );
77 }
78 catch ( NoSuchMethodException e )
79 {
80 throw new SurefireReflectionException( e );
81 }
82 }
83
84 public static Object newInstance( Constructor constructor, Object[] params )
85 {
86 try
87 {
88 return constructor.newInstance( params );
89 }
90 catch ( InvocationTargetException e )
91 {
92 throw new SurefireReflectionException( e );
93 }
94 catch ( InstantiationException e )
95 {
96 throw new SurefireReflectionException( e );
97 }
98 catch ( IllegalAccessException e )
99 {
100 throw new SurefireReflectionException( e );
101 }
102 }
103
104 public static Object instantiate( ClassLoader classLoader, String classname )
105 {
106 try
107 {
108
109 Class clazz = loadClass( classLoader, classname );
110 return clazz.newInstance();
111 }
112 catch ( InstantiationException e )
113 {
114 throw new SurefireReflectionException( e );
115 }
116 catch ( IllegalAccessException e )
117 {
118 throw new SurefireReflectionException( e );
119 }
120 }
121
122 public static Object instantiateOneArg( ClassLoader classLoader, String className, Class param1Class,
123 Object param1 )
124 {
125
126 try
127 {
128 Class aClass = loadClass( classLoader, className );
129 Constructor constructor = ReflectionUtils.getConstructor( aClass, new Class[]{ param1Class } );
130 return constructor.newInstance( new Object[]{ param1 } );
131 }
132 catch ( InvocationTargetException e )
133 {
134 throw new SurefireReflectionException( e );
135 }
136 catch ( InstantiationException e )
137 {
138 throw new SurefireReflectionException( e );
139 }
140 catch ( IllegalAccessException e )
141 {
142 throw new SurefireReflectionException( e );
143 }
144 }
145
146 public static Object invokeSetter( Object o, String name, Class value1clazz, Object value )
147
148 {
149 final Method setter = getMethod( o, name, new Class[]{ value1clazz } );
150 return invokeSetter( o, setter, value );
151 }
152
153 public static Object invokeSetter( Object target, Method method, Object value )
154
155 {
156 return invokeMethodWithArray( target, method, new Object[]{ value } );
157 }
158
159 public static Object invokeMethodWithArray( Object target, Method method, Object[] args )
160
161 {
162 try
163 {
164 return method.invoke( target, args );
165 }
166 catch ( IllegalAccessException e )
167 {
168 throw new SurefireReflectionException( e );
169 }
170 catch ( InvocationTargetException e )
171 {
172 throw new SurefireReflectionException( e );
173 }
174
175 }
176
177 public static Object instantiateObject( String className, Object[] params, ClassLoader classLoader )
178 {
179 try
180 {
181 Class clazz = loadClass( classLoader, className );
182
183 Object object;
184 if ( params != null )
185 {
186 Class[] paramTypes = new Class[params.length];
187
188 for ( int j = 0; j < params.length; j++ )
189 {
190 if ( params[j] == null )
191 {
192 paramTypes[j] = String.class;
193 }
194 else
195 {
196 paramTypes[j] = params[j].getClass();
197 }
198 }
199
200 Constructor constructor = getConstructor( clazz, paramTypes );
201
202 object = newInstance( constructor, params );
203 }
204 else
205 {
206 object = clazz.newInstance();
207 }
208 return object;
209
210 }
211 catch ( IllegalAccessException e )
212 {
213 throw new SurefireReflectionException( e );
214 }
215 catch ( InstantiationException e )
216 {
217 throw new SurefireReflectionException( e );
218 }
219 }
220
221 public static Class tryLoadClass( ClassLoader classLoader, String className )
222 {
223 try
224 {
225 return classLoader.loadClass( className );
226 }
227 catch ( NoClassDefFoundError ignore )
228 {
229 }
230 catch ( ClassNotFoundException ignore )
231 {
232 }
233 return null;
234 }
235
236 public static Class loadClass( ClassLoader classLoader, String className )
237 {
238 try
239 {
240 return classLoader.loadClass( className );
241 }
242 catch ( NoClassDefFoundError e )
243 {
244 throw new SurefireReflectionException( e );
245 }
246 catch ( ClassNotFoundException e )
247 {
248 throw new SurefireReflectionException( e );
249 }
250 }
251 }