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
38
39
40
41
42
43
44
45 public class KeyValueType implements Cloneable
46 {
47
48
49 private String key;
50
51
52 private String value;
53
54
55 private String type;
56
57
58 public KeyValueType()
59 {
60 super();
61 }
62
63
64
65
66
67
68
69
70 public final String getKey()
71 {
72 return this.key;
73 }
74
75
76
77
78
79
80
81
82 public final void setKey( final String k )
83 {
84 this.key = k;
85 }
86
87
88
89
90
91
92
93
94 public final String getValue()
95 {
96 return this.value;
97 }
98
99
100
101
102
103
104
105
106 public final void setValue( final String v )
107 {
108 this.value = v;
109 }
110
111
112
113
114
115
116
117
118 public final String getType()
119 {
120 return this.type;
121 }
122
123
124
125
126
127
128
129
130 public final void setType( final String t )
131 {
132 this.type = t;
133 }
134
135
136
137
138
139
140
141
142
143
144
145 public Object getObject() throws InstantiationException
146 {
147 Class<?> javaClass = null;
148 Object o = this.getValue();
149
150 try
151 {
152 if ( o != null )
153 {
154 if ( this.getType() != null && !String.class.getName().equals( this.getType() ) )
155 {
156 javaClass = Class.forName( this.getType() );
157
158 try
159 {
160 o = javaClass.getConstructor( String.class ).newInstance( o );
161 }
162 catch ( final NoSuchMethodException e )
163 {
164 final Method valueOf = javaClass.getMethod( "valueOf", String.class );
165
166 if ( Modifier.isStatic( valueOf.getModifiers() )
167 && valueOf.getReturnType().equals( javaClass ) )
168 {
169 o = valueOf.invoke( null, o );
170 }
171 else
172 {
173 throw (InstantiationException) new InstantiationException(
174 Messages.getMessage( "noSuchMethodCreatingObject", this.getType(), this.getValue(),
175 javaClass.getSimpleName() ) ).initCause( e );
176
177 }
178 }
179 }
180 }
181 else if ( this.getType() != null )
182 {
183 o = Class.forName( this.getType() ).newInstance();
184 }
185
186 return o;
187 }
188 catch ( final ClassNotFoundException e )
189 {
190 throw (InstantiationException) new InstantiationException(
191 Messages.getMessage( "failedCreatingObject", this.getType() ) ).initCause( e );
192
193 }
194 catch ( final NoSuchMethodException e )
195 {
196 throw (InstantiationException) new InstantiationException(
197 Messages.getMessage( "noSuchMethodCreatingObject", this.getType(), this.getValue(),
198 javaClass.getSimpleName() ) ).initCause( e );
199
200 }
201 catch ( final IllegalAccessException e )
202 {
203 throw (InstantiationException) new InstantiationException(
204 Messages.getMessage( "failedCreatingObject", this.getType() ) ).initCause( e );
205
206 }
207 catch ( final InvocationTargetException e )
208 {
209 throw (InstantiationException) new InstantiationException(
210 Messages.getMessage( "failedCreatingObject", this.getType() ) ).initCause( e );
211
212 }
213 }
214
215
216
217
218
219
220 @Override
221 public KeyValueType clone()
222 {
223 try
224 {
225 return (KeyValueType) super.clone();
226 }
227 catch ( final CloneNotSupportedException e )
228 {
229 throw new AssertionError( e );
230 }
231 }
232
233
234
235
236
237
238 @Override
239 public String toString()
240 {
241 return ToStringBuilder.reflectionToString( this );
242 }
243
244 }