Coverage Report - joptsimple.internal.Reflection
 
Classes in this File Line Coverage Branch Coverage Complexity
Reflection
94%
30/32
83%
15/18
3.625
 
 1  
 /*
 2  
  Copyright 2004-2008 Paul R. Holser, Jr.  All rights reserved.
 3  
  Licensed under the Academic Free License version 3.0
 4  
  */
 5  
 
 6  
 package joptsimple.internal;
 7  
 
 8  
 import java.lang.reflect.Constructor;
 9  
 import java.lang.reflect.InvocationTargetException;
 10  
 import java.lang.reflect.Member;
 11  
 import java.lang.reflect.Method;
 12  
 import java.lang.reflect.Modifier;
 13  
 
 14  
 /**
 15  
  * <p>Helper methods for reflection.</p>
 16  
  *
 17  
  * @since 2.0
 18  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 19  
  * @version $Id: Reflection.java,v 1.3 2008/06/02 16:35:17 pholser Exp $
 20  
  */
 21  
 public class Reflection {
 22  2
     protected Reflection() {
 23  2
         throw new UnsupportedOperationException();
 24  
     }
 25  
 
 26  
     /**
 27  
      * Finds an appropriate value converter for the given class.
 28  
      * 
 29  
      * @param clazz class to introspect on
 30  
      * @return a converter method or constructor
 31  
      */
 32  
     public static Member findConverter( Class clazz ) {
 33  132
         Member method = findConverterMethod( clazz );
 34  128
         if ( method != null )
 35  84
             return method;
 36  
 
 37  44
         Member constructor = findConverterConstructor( clazz );
 38  44
         if ( constructor != null )
 39  40
             return constructor;
 40  
         
 41  4
         throw new IllegalArgumentException( clazz + " is not a value type" );
 42  
     }
 43  
 
 44  
     private static Method findConverterMethod( Class clazz ) {
 45  
         try {
 46  132
             Method valueOf =
 47  2
                 clazz.getDeclaredMethod( "valueOf", new Class[] { String.class } );
 48  
 
 49  88
             return meetsConverterRequirements( valueOf, clazz ) ? valueOf : null;
 50  
         }
 51  40
         catch ( NoSuchMethodException ignored ) {
 52  40
             return null;
 53  
         }
 54  
     }
 55  
 
 56  
     private static boolean meetsConverterRequirements( Method method,
 57  
         Class expectedReturnType ) {
 58  
 
 59  88
         int modifiers = method.getModifiers();
 60  88
         return Modifier.isPublic( modifiers )
 61  
             && Modifier.isStatic( modifiers )
 62  
             && expectedReturnType.equals( method.getReturnType() );
 63  
     }
 64  
 
 65  
     private static Constructor findConverterConstructor( Class clazz ) {
 66  
         try {
 67  44
             return clazz.getConstructor( new Class[] { String.class } );
 68  
         }
 69  4
         catch ( NoSuchMethodException ignored ) {
 70  4
             return null;
 71  
         }
 72  
     }
 73  
 
 74  
     /**
 75  
      * Invokes the given constructor with the given args.
 76  
      *
 77  
      * @param constructor constructor to invoke
 78  
      * @param args arguments to hand to the constructor
 79  
      * @return the result of invoking the constructor
 80  
      * @throws ReflectionException in lieu of the gaggle of reflection-related exceptions
 81  
      */
 82  
     public static Object instantiate( Constructor constructor, Object[] args ) {
 83  48
         return invokeQuietly( constructor, args );
 84  
     }
 85  
 
 86  
     /**
 87  
      * Invokes the given static method with the given args.
 88  
      *
 89  
      * @param method method to invoke
 90  
      * @param args arguments to hand to the method
 91  
      * @return the result of invoking the method
 92  
      * @throws ReflectionException in lieu of the gaggle of reflection-related exceptions
 93  
      */
 94  
     public static Object invoke( Method method, Object[] args ) {
 95  84
         return invokeQuietly( method, args );
 96  
     }
 97  
 
 98  
     private static Object invokeQuietly( Member member, Object[] args ) {
 99  
         try {
 100  132
             if ( member instanceof Constructor )
 101  48
                 return ((Constructor) member).newInstance( args );
 102  84
             return ((Method) member).invoke( null, args );
 103  
         }
 104  4
         catch ( IllegalArgumentException ex ) {
 105  4
             throw new ReflectionException( ex );
 106  
         }
 107  10
         catch ( InvocationTargetException ex ) {
 108  10
             throw new ReflectionException( ex.getTargetException() );
 109  
         }
 110  0
         catch ( RuntimeException ex ) {
 111  0
             throw ex;
 112  
         }
 113  6
         catch ( Exception ex ) {
 114  6
             throw new ReflectionException( ex );
 115  
         }
 116  
     }
 117  
 }