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