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 package org.jomc.mojo;
32
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.Modifier;
36 import org.apache.commons.lang.builder.ToStringBuilder;
37 import org.jomc.modlet.ModelContext;
38 import org.jomc.modlet.ModelException;
39
40
41
42
43
44
45
46
47 public class KeyValueType implements Cloneable
48 {
49
50
51
52
53 private String key;
54
55
56
57
58 private String value;
59
60
61
62
63 private String type;
64
65
66
67
68 public KeyValueType()
69 {
70 super();
71 }
72
73
74
75
76
77
78
79
80 public final String getKey()
81 {
82 return this.key;
83 }
84
85
86
87
88
89
90
91
92 public final void setKey( final String k )
93 {
94 this.key = k;
95 }
96
97
98
99
100
101
102
103
104 public final String getValue()
105 {
106 return this.value;
107 }
108
109
110
111
112
113
114
115
116 public final void setValue( final String v )
117 {
118 this.value = v;
119 }
120
121
122
123
124
125
126
127
128 public final String getType()
129 {
130 return this.type;
131 }
132
133
134
135
136
137
138
139
140 public final void setType( final String t )
141 {
142 this.type = t;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157 @Deprecated
158 public Object getObject() throws InstantiationException
159 {
160 Class<?> javaClass = null;
161 Object o = this.getValue();
162
163 try
164 {
165 if ( o != null )
166 {
167 if ( this.getType() != null && !String.class.getName().equals( this.getType() ) )
168 {
169 javaClass = Class.forName( this.getType() );
170
171 try
172 {
173 o = javaClass.getConstructor( String.class ).newInstance( o );
174 }
175 catch ( final NoSuchMethodException e )
176 {
177 final Method valueOf = javaClass.getMethod( "valueOf", String.class );
178
179 if ( Modifier.isStatic( valueOf.getModifiers() )
180 && valueOf.getReturnType().equals( javaClass ) )
181 {
182 o = valueOf.invoke( null, o );
183 }
184 else
185 {
186 throw (InstantiationException) new InstantiationException(
187 Messages.getMessage( "noSuchMethodCreatingObject", this.getType(), this.getValue(),
188 javaClass.getSimpleName() ) ).initCause( e );
189
190 }
191 }
192 }
193 }
194 else if ( this.getType() != null )
195 {
196 o = Class.forName( this.getType() ).newInstance();
197 }
198
199 return o;
200 }
201 catch ( final ClassNotFoundException e )
202 {
203 throw (InstantiationException) new InstantiationException(
204 Messages.getMessage( "classNotFound", this.getType() ) ).initCause( e );
205
206 }
207 catch ( final NoSuchMethodException e )
208 {
209 throw (InstantiationException) new InstantiationException(
210 Messages.getMessage( "noSuchMethodCreatingObject", this.getType(), this.getValue(),
211 javaClass.getSimpleName() ) ).initCause( e );
212
213 }
214 catch ( final IllegalAccessException e )
215 {
216 throw (InstantiationException) new InstantiationException(
217 Messages.getMessage( "failedCreatingObject", this.getType() ) ).initCause( e );
218
219 }
220 catch ( final InvocationTargetException e )
221 {
222 throw (InstantiationException) new InstantiationException(
223 Messages.getMessage( "failedCreatingObject", this.getType() ) ).initCause( e );
224
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 public Object getObject( final ModelContext modelContext ) throws InstantiationException
244 {
245 if ( modelContext == null )
246 {
247 throw new NullPointerException( "modelContext" );
248 }
249
250 Class<?> javaClass = null;
251 Object o = this.getValue();
252
253 try
254 {
255 if ( o != null )
256 {
257 if ( this.getType() != null && !String.class.getName().equals( this.getType() ) )
258 {
259 javaClass = modelContext.findClass( this.getType() );
260
261 if ( javaClass == null )
262 {
263 throw (InstantiationException) new InstantiationException(
264 Messages.getMessage( "classNotFound", this.getType() ) );
265
266 }
267
268 try
269 {
270 o = javaClass.getConstructor( String.class ).newInstance( o );
271 }
272 catch ( final NoSuchMethodException e )
273 {
274 final Method valueOf = javaClass.getMethod( "valueOf", String.class );
275
276 if ( Modifier.isStatic( valueOf.getModifiers() )
277 && valueOf.getReturnType().equals( javaClass ) )
278 {
279 o = valueOf.invoke( null, o );
280 }
281 else
282 {
283 throw (InstantiationException) new InstantiationException(
284 Messages.getMessage( "noSuchMethodCreatingObject", this.getType(), this.getValue(),
285 javaClass.getSimpleName() ) ).initCause( e );
286
287 }
288 }
289 }
290 }
291 else if ( this.getType() != null )
292 {
293 javaClass = modelContext.findClass( this.getType() );
294
295 if ( javaClass == null )
296 {
297 throw (InstantiationException) new InstantiationException(
298 Messages.getMessage( "classNotFound", this.getType() ) );
299
300 }
301
302 o = javaClass.newInstance();
303 }
304
305 return o;
306 }
307 catch ( final ModelException e )
308 {
309 throw (InstantiationException) new InstantiationException(
310 Messages.getMessage( "failedSearchingClass", this.getType() ) ).initCause( e );
311
312 }
313 catch ( final NoSuchMethodException e )
314 {
315 throw (InstantiationException) new InstantiationException(
316 Messages.getMessage( "noSuchMethodCreatingObject", this.getType(), this.getValue(),
317 javaClass.getSimpleName() ) ).initCause( e );
318
319 }
320 catch ( final IllegalAccessException e )
321 {
322 throw (InstantiationException) new InstantiationException(
323 Messages.getMessage( "failedCreatingObject", this.getType() ) ).initCause( e );
324
325 }
326 catch ( final InvocationTargetException e )
327 {
328 throw (InstantiationException) new InstantiationException(
329 Messages.getMessage( "failedCreatingObject", this.getType() ) ).initCause( e );
330
331 }
332 }
333
334
335
336
337
338
339 @Override
340 public KeyValueType clone()
341 {
342 try
343 {
344 return (KeyValueType) super.clone();
345 }
346 catch ( final CloneNotSupportedException e )
347 {
348 throw new AssertionError( e );
349 }
350 }
351
352
353
354
355
356
357 @Override
358 public String toString()
359 {
360 return ToStringBuilder.reflectionToString( this );
361 }
362
363 }