Coverage Report - org.apache.maven.surefire.util.ReflectionUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectionUtils
0%
0/71
N/A
4,125
 
 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  0
 public class ReflectionUtils
 30  
 {
 31  0
     private static final Class[] NO_ARGS = new Class[0];
 32  
 
 33  0
     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  0
         return getMethod( instance.getClass(), methodName, parameters );
 39  
     }
 40  
 
 41  
     public static Method getMethod( Class clazz, String methodName, Class[] parameters )
 42  
     {
 43  
         try
 44  
         {
 45  0
             return clazz.getMethod( methodName, parameters );
 46  
         }
 47  0
         catch ( NoSuchMethodException e )
 48  
         {
 49  0
             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  0
             return clazz.getMethod( methodName, parameters );
 58  
         }
 59  0
         catch ( NoSuchMethodException e )
 60  
         {
 61  0
             return null;
 62  
         }
 63  
     }
 64  
 
 65  
 
 66  
     public static Object invokeGetter( Object instance, String methodName )
 67  
     {
 68  0
         final Method method = getMethod( instance, methodName, NO_ARGS );
 69  0
         return invokeMethodWithArray( instance, method, NO_ARGS_VALUES );
 70  
     }
 71  
 
 72  
     public static Constructor getConstructor( Class clazz, Class[] arguments )
 73  
     {
 74  
         try
 75  
         {
 76  0
             return clazz.getConstructor( arguments );
 77  
         }
 78  0
         catch ( NoSuchMethodException e )
 79  
         {
 80  0
             throw new SurefireReflectionException( e );
 81  
         }
 82  
     }
 83  
 
 84  
     public static Object newInstance( Constructor constructor, Object[] params )
 85  
     {
 86  
         try
 87  
         {
 88  0
             return constructor.newInstance( params );
 89  
         }
 90  0
         catch ( InvocationTargetException e )
 91  
         {
 92  0
             throw new SurefireReflectionException( e );
 93  
         }
 94  0
         catch ( InstantiationException e )
 95  
         {
 96  0
             throw new SurefireReflectionException( e );
 97  
         }
 98  0
         catch ( IllegalAccessException e )
 99  
         {
 100  0
             throw new SurefireReflectionException( e );
 101  
         }
 102  
     }
 103  
 
 104  
     public static Object instantiate( ClassLoader classLoader, String classname )
 105  
     {
 106  
         try
 107  
         {
 108  
 
 109  0
             Class clazz = loadClass( classLoader, classname );
 110  0
             return clazz.newInstance();
 111  
         }
 112  0
         catch ( InstantiationException e )
 113  
         {
 114  0
             throw new SurefireReflectionException( e );
 115  
         }
 116  0
         catch ( IllegalAccessException e )
 117  
         {
 118  0
             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  0
             Class aClass = loadClass( classLoader, className );
 129  0
             Constructor constructor = ReflectionUtils.getConstructor( aClass, new Class[]{ param1Class } );
 130  0
             return constructor.newInstance( param1 );
 131  
         }
 132  0
         catch ( InvocationTargetException e )
 133  
         {
 134  0
             throw new SurefireReflectionException( e.getTargetException() );
 135  
         }
 136  0
         catch ( InstantiationException e )
 137  
         {
 138  0
             throw new SurefireReflectionException( e );
 139  
         }
 140  0
         catch ( IllegalAccessException e )
 141  
         {
 142  0
             throw new SurefireReflectionException( e );
 143  
         }
 144  
     }
 145  
 
 146  
     public static Object instantiateTwoArgs( ClassLoader classLoader, String className, Class param1Class,
 147  
                                              Object param1, Class param2Class, Object param2 )
 148  
     {
 149  
 
 150  
         try
 151  
         {
 152  0
             Class aClass = loadClass( classLoader, className );
 153  0
             Constructor constructor = ReflectionUtils.getConstructor( aClass, new Class[]{ param1Class, param2Class } );
 154  0
             return constructor.newInstance( param1, param2 );
 155  
         }
 156  0
         catch ( InvocationTargetException e )
 157  
         {
 158  0
             throw new SurefireReflectionException( e.getTargetException() );
 159  
         }
 160  0
         catch ( InstantiationException e )
 161  
         {
 162  0
             throw new SurefireReflectionException( e );
 163  
         }
 164  0
         catch ( IllegalAccessException e )
 165  
         {
 166  0
             throw new SurefireReflectionException( e );
 167  
         }
 168  
     }
 169  
 
 170  
 
 171  
     public static void invokeSetter( Object o, String name, Class value1clazz, Object value )
 172  
 
 173  
     {
 174  0
         final Method setter = getMethod( o, name, new Class[]{ value1clazz } );
 175  0
         invokeSetter( o, setter, value );
 176  0
     }
 177  
 
 178  
     public static Object invokeSetter( Object target, Method method, Object value )
 179  
 
 180  
     {
 181  0
         return invokeMethodWithArray( target, method, new Object[]{ value } );
 182  
     }
 183  
 
 184  
     public static Object invokeMethodWithArray( Object target, Method method, Object[] args )
 185  
 
 186  
     {
 187  
         try
 188  
         {
 189  0
             return method.invoke( target, args );
 190  
         }
 191  0
         catch ( IllegalAccessException e )
 192  
         {
 193  0
             throw new SurefireReflectionException( e );
 194  
         }
 195  0
         catch ( InvocationTargetException e )
 196  
         {
 197  0
             throw new SurefireReflectionException( e.getTargetException() );
 198  
         }
 199  
 
 200  
     }
 201  
 
 202  
     public static Object invokeMethodWithArray2( Object target, Method method, Object[] args )
 203  
         throws InvocationTargetException
 204  
 
 205  
     {
 206  
         try
 207  
         {
 208  0
             return method.invoke( target, args );
 209  
         }
 210  0
         catch ( IllegalAccessException e )
 211  
         {
 212  0
             throw new SurefireReflectionException( e );
 213  
         }
 214  
 
 215  
     }
 216  
 
 217  
     public static Object instantiateObject( String className, Class[] types, Object[] params, ClassLoader classLoader )
 218  
     {
 219  0
         Class clazz = loadClass( classLoader, className );
 220  0
         final Constructor constructor = getConstructor( clazz, types );
 221  0
         return newInstance( constructor, params );
 222  
     }
 223  
 
 224  
     public static Class tryLoadClass( ClassLoader classLoader, String className )
 225  
     {
 226  
         try
 227  
         {
 228  0
             return classLoader.loadClass( className );
 229  
         }
 230  0
         catch ( NoClassDefFoundError ignore )
 231  
         {
 232  
         }
 233  0
         catch ( ClassNotFoundException ignore )
 234  
         {
 235  0
         }
 236  0
         return null;
 237  
     }
 238  
 
 239  
     public static Class loadClass( ClassLoader classLoader, String className )
 240  
     {
 241  
         try
 242  
         {
 243  0
             return classLoader.loadClass( className );
 244  
         }
 245  0
         catch ( NoClassDefFoundError e )
 246  
         {
 247  0
             throw new SurefireReflectionException( e );
 248  
         }
 249  0
         catch ( ClassNotFoundException e )
 250  
         {
 251  0
             throw new SurefireReflectionException( e );
 252  
         }
 253  
     }
 254  
 }