1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.jomc.sdk.model;
34
35 import java.lang.reflect.InvocationTargetException;
36 import java.text.MessageFormat;
37 import java.util.Locale;
38 import java.util.ResourceBundle;
39 import org.jomc.model.PropertyException;
40
41
42
43
44
45
46
47 abstract class ReflectionHelper
48 {
49
50 private static String GET_JAVA_VALUE = "getJavaValue";
51
52 ReflectionHelper()
53 {
54 super();
55 }
56
57 static java.lang.Object getJavaValue( final ClassLoader classLoader, final Object o ) throws PropertyException
58 {
59 if ( o != null )
60 {
61 try
62 {
63 return o.getClass().getMethod( GET_JAVA_VALUE, ClassLoader.class ).invoke( o, classLoader );
64 }
65 catch ( final IllegalAccessException e )
66 {
67 throw new PropertyException( getMessage(
68 "methodAccessDenied", GET_JAVA_VALUE, o.getClass().getName() ), e );
69
70 }
71 catch ( final IllegalArgumentException e )
72 {
73 throw new AssertionError( e );
74 }
75 catch ( final InvocationTargetException e )
76 {
77 throw new PropertyException( getMessage(
78 "methodInvocationFailure", GET_JAVA_VALUE, o.getClass().getName() ), e );
79
80 }
81 catch ( final SecurityException e )
82 {
83 throw new PropertyException( getMessage(
84 "methodAccessDenied", GET_JAVA_VALUE, o.getClass().getName() ), e );
85
86 }
87 catch ( final NoSuchMethodException e )
88 {
89
90 return o;
91 }
92 }
93
94 return o;
95 }
96
97 static <T> T getJavaValue( final ClassLoader classLoader, final Object o, final Class<T> returnType )
98 throws PropertyException
99 {
100 final Object javaValue = getJavaValue( classLoader, o );
101
102 if ( javaValue != null && !returnType.isAssignableFrom( javaValue.getClass() ) )
103 {
104 throw new PropertyException( getMessage(
105 "illegalMethodInvocationResult", GET_JAVA_VALUE, o.getClass().getName(), javaValue.getClass().getName(),
106 returnType.getName() ) );
107
108 }
109
110 return (T) javaValue;
111 }
112
113 static <T> T getJavaValue( final Class<T> type, final String value ) throws PropertyException
114 {
115 if ( value != null )
116 {
117 try
118 {
119 if ( type == Character.class )
120 {
121 if ( value.length() != 1 )
122 {
123 throw new PropertyException( getMessage( "illegalValue", value, Character.class.getName() ) );
124 }
125
126 return type.getConstructor( new Class[]
127 {
128 char.class
129 } ).newInstance( new java.lang.Object[]
130 {
131 value.charAt( 0 )
132 } );
133
134 }
135 else if ( type == String.class )
136 {
137 return (T) value;
138 }
139 else
140 {
141 return type.getConstructor( new Class[]
142 {
143 String.class
144 } ).newInstance( value );
145
146 }
147 }
148 catch ( final InstantiationException e )
149 {
150 throw new PropertyException( getMessage( "instantiationException", type.getName() ), e );
151 }
152 catch ( final IllegalAccessException e )
153 {
154 throw new PropertyException( getMessage( "constructorAccessDenied", type.getName() ), e );
155 }
156 catch ( final IllegalArgumentException e )
157 {
158 throw new AssertionError( e );
159 }
160 catch ( final InvocationTargetException e )
161 {
162 throw new PropertyException( getMessage( "constructorInvocationFailure", type.getName() ), e );
163 }
164 catch ( final NoSuchMethodException e )
165 {
166 throw new PropertyException( getMessage( "constructorNotFound", type.getName() ), e );
167 }
168 }
169
170 return null;
171 }
172
173 private static String getMessage( final String key, final java.lang.Object... arguments )
174 {
175 return MessageFormat.format( ResourceBundle.getBundle( ReflectionHelper.class.getName().replace( '.', '/' ),
176 Locale.getDefault() ).getString( key ), arguments );
177
178 }
179
180 }