| 1 | |
|
| 2 | |
|
| 3 | |
|
| 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 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
public class Reflection { |
| 22 | 2 | protected Reflection() { |
| 23 | 2 | throw new UnsupportedOperationException(); |
| 24 | |
} |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public static Object instantiate( Constructor constructor, Object[] args ) { |
| 83 | 48 | return invokeQuietly( constructor, args ); |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 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 | |
} |