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