View Javadoc
1   package org.apache.maven.surefire.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Constructor;
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  
26  /**
27   * @author Kristian Rosenvold
28   */
29  public final 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      private ReflectionUtils()
36      {
37          throw new IllegalStateException( "no instantiable constructor" );
38      }
39  
40      public static Method getMethod( Object instance, String methodName, Class<?>... parameters )
41      {
42          return getMethod( instance.getClass(), methodName, parameters );
43      }
44  
45      public static Method getMethod( Class<?> clazz, String methodName, Class<?>... parameters )
46      {
47          try
48          {
49              return clazz.getMethod( methodName, parameters );
50          }
51          catch ( NoSuchMethodException e )
52          {
53              throw new RuntimeException( "When finding method " + methodName, e );
54          }
55      }
56  
57      public static Method tryGetMethod( Class<?> clazz, String methodName, Class<?>... parameters )
58      {
59          try
60          {
61              return clazz.getMethod( methodName, parameters );
62          }
63          catch ( NoSuchMethodException e )
64          {
65              return null;
66          }
67      }
68  
69      public static Object invokeGetter( Object instance, String methodName )
70      {
71          final Method method = getMethod( instance, methodName, NO_ARGS );
72          return invokeMethodWithArray( instance, method, NO_ARGS_VALUES );
73      }
74  
75      public static Constructor getConstructor( Class<?> clazz, Class<?>... arguments )
76      {
77          try
78          {
79              return clazz.getConstructor( arguments );
80          }
81          catch ( NoSuchMethodException e )
82          {
83              throw new SurefireReflectionException( e );
84          }
85      }
86  
87      public static Object newInstance( Constructor constructor, Object... params )
88      {
89          try
90          {
91              return constructor.newInstance( params );
92          }
93          catch ( InvocationTargetException e )
94          {
95              throw new SurefireReflectionException( e );
96          }
97          catch ( InstantiationException e )
98          {
99              throw new SurefireReflectionException( e );
100         }
101         catch ( IllegalAccessException e )
102         {
103             throw new SurefireReflectionException( e );
104         }
105     }
106 
107     public static <T> T instantiate( ClassLoader classLoader, String classname, Class<T> returnType )
108     {
109         try
110         {
111             Class<?> clazz = loadClass( classLoader, classname );
112             return returnType.cast( clazz.newInstance() );
113         }
114         catch ( InstantiationException e )
115         {
116             throw new SurefireReflectionException( e );
117         }
118         catch ( IllegalAccessException e )
119         {
120             throw new SurefireReflectionException( e );
121         }
122     }
123 
124     public static Object instantiateOneArg( ClassLoader classLoader, String className, Class<?> param1Class,
125                                             Object param1 )
126     {
127         try
128         {
129             Class<?> aClass = loadClass( classLoader, className );
130             Constructor constructor = getConstructor( aClass, param1Class );
131             return constructor.newInstance( param1 );
132         }
133         catch ( InvocationTargetException e )
134         {
135             throw new SurefireReflectionException( e.getTargetException() );
136         }
137         catch ( InstantiationException e )
138         {
139             throw new SurefireReflectionException( e );
140         }
141         catch ( IllegalAccessException e )
142         {
143             throw new SurefireReflectionException( e );
144         }
145     }
146 
147     public static Object instantiateTwoArgs( ClassLoader classLoader, String className, Class<?> param1Class,
148                                              Object param1, Class param2Class, Object param2 )
149     {
150         try
151         {
152             Class<?> aClass = loadClass( classLoader, className );
153             Constructor constructor = getConstructor( aClass, param1Class, param2Class );
154             return constructor.newInstance( param1, param2 );
155         }
156         catch ( InvocationTargetException e )
157         {
158             throw new SurefireReflectionException( e.getTargetException() );
159         }
160         catch ( InstantiationException e )
161         {
162             throw new SurefireReflectionException( e );
163         }
164         catch ( IllegalAccessException e )
165         {
166             throw new SurefireReflectionException( e );
167         }
168     }
169 
170     public static void invokeSetter( Object o, String name, Class<?> value1clazz, Object value )
171     {
172         Method setter = getMethod( o, name, value1clazz );
173         invokeSetter( o, setter, value );
174     }
175 
176     public static Object invokeSetter( Object target, Method method, Object value )
177     {
178         return invokeMethodWithArray( target, method, value );
179     }
180 
181     public static Object invokeMethodWithArray( Object target, Method method, Object... args )
182     {
183         try
184         {
185             return method.invoke( target, args );
186         }
187         catch ( IllegalAccessException e )
188         {
189             throw new SurefireReflectionException( e );
190         }
191         catch ( InvocationTargetException e )
192         {
193             throw new SurefireReflectionException( e.getTargetException() );
194         }
195     }
196 
197     public static Object invokeMethodWithArray2( Object target, Method method, Object... args )
198         throws InvocationTargetException
199     {
200         try
201         {
202             return method.invoke( target, args );
203         }
204         catch ( IllegalAccessException e )
205         {
206             throw new SurefireReflectionException( e );
207         }
208     }
209 
210     public static Object instantiateObject( String className, Class[] types, Object[] params, ClassLoader classLoader )
211     {
212         Class<?> clazz = loadClass( classLoader, className );
213         final Constructor constructor = getConstructor( clazz, types );
214         return newInstance( constructor, params );
215     }
216 
217     @SuppressWarnings( "checkstyle:emptyblock" )
218     public static Class<?> tryLoadClass( ClassLoader classLoader, String className )
219     {
220         try
221         {
222             return classLoader.loadClass( className );
223         }
224         catch ( NoClassDefFoundError ignore )
225         {
226         }
227         catch ( ClassNotFoundException ignore )
228         {
229         }
230         return null;
231     }
232 
233     public static Class<?> loadClass( ClassLoader classLoader, String className )
234     {
235         try
236         {
237             return classLoader.loadClass( className );
238         }
239         catch ( NoClassDefFoundError e )
240         {
241             throw new SurefireReflectionException( e );
242         }
243         catch ( ClassNotFoundException e )
244         {
245             throw new SurefireReflectionException( e );
246         }
247     }
248 }