1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.enhance; |
16 | |
|
17 | |
import org.apache.hivemind.Location; |
18 | |
import org.apache.hivemind.service.BodyBuilder; |
19 | |
import org.apache.hivemind.service.ClassFabUtils; |
20 | |
import org.apache.hivemind.service.MethodSignature; |
21 | |
import org.apache.hivemind.util.Defense; |
22 | |
import org.apache.tapestry.coerce.ValueConverter; |
23 | |
import org.apache.tapestry.services.ComponentPropertySource; |
24 | |
import org.apache.tapestry.spec.InjectSpecification; |
25 | |
|
26 | |
import java.lang.reflect.Modifier; |
27 | |
import java.util.HashMap; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class InjectMetaWorker implements InjectEnhancementWorker |
39 | |
{ |
40 | |
static final String SOURCE_NAME = "_$componentPropertySource"; |
41 | |
|
42 | |
private ComponentPropertySource _source; |
43 | |
|
44 | |
private ValueConverter _valueConverter; |
45 | |
|
46 | 0 | private Map _primitiveParser = new HashMap(); |
47 | |
{ |
48 | 0 | _primitiveParser.put(short.class, "java.lang.Short.parseShort"); |
49 | 0 | _primitiveParser.put(int.class, "java.lang.Integer.parseInt"); |
50 | 0 | _primitiveParser.put(long.class, "java.lang.Long.parseLong"); |
51 | 0 | _primitiveParser.put(double.class, "java.lang.Double.parseDouble"); |
52 | 0 | _primitiveParser.put(float.class, "java.lang.Float.parseFloat"); |
53 | 0 | } |
54 | |
|
55 | |
public void performEnhancement(EnhancementOperation op, InjectSpecification spec) |
56 | |
{ |
57 | 0 | String propertyName = spec.getProperty(); |
58 | 0 | String metaKey = spec.getObject(); |
59 | |
|
60 | 0 | injectMetaValue(op, propertyName, metaKey, spec.getLocation()); |
61 | 0 | } |
62 | |
|
63 | |
public void injectMetaValue(EnhancementOperation op, String propertyName, String metaKey, |
64 | |
Location location) |
65 | |
{ |
66 | 0 | Defense.notNull(op, "op"); |
67 | 0 | Defense.notNull(propertyName, "propertyName"); |
68 | 0 | Defense.notNull(metaKey, "metaKey"); |
69 | |
|
70 | 0 | Class propertyType = op.getPropertyType(propertyName); |
71 | |
|
72 | |
|
73 | |
|
74 | 0 | if (propertyType == null) { |
75 | |
|
76 | 0 | propertyType = Object.class; |
77 | |
} |
78 | |
|
79 | 0 | op.claimReadonlyProperty(propertyName); |
80 | |
|
81 | 0 | String sourceName = op.addInjectedField(SOURCE_NAME, ComponentPropertySource.class, _source); |
82 | |
|
83 | 0 | MethodSignature sig = new MethodSignature(propertyType, op.getAccessorMethodName(propertyName), null, null); |
84 | |
|
85 | 0 | String parser = (String) _primitiveParser.get(propertyType); |
86 | |
|
87 | 0 | if (parser != null) |
88 | |
{ |
89 | 0 | addPrimitive(op, metaKey, propertyName, sig, sourceName, parser, location); |
90 | 0 | return; |
91 | 0 | } else if (propertyType == boolean.class) |
92 | |
{ |
93 | 0 | addBoolean(op, metaKey, propertyName, sig, sourceName, location); |
94 | 0 | return; |
95 | |
} |
96 | |
|
97 | 0 | if (propertyType == char.class) |
98 | |
{ |
99 | 0 | addCharacterPrimitive(op, metaKey, propertyName, sig, sourceName, location); |
100 | 0 | return; |
101 | |
} |
102 | |
|
103 | 0 | addObject(op, metaKey, propertyName, propertyType, sig, sourceName, location); |
104 | 0 | } |
105 | |
|
106 | |
private void addPrimitive(EnhancementOperation op, String metaKey, String propertyName, |
107 | |
MethodSignature sig, String sourceName, String parser, Location location) |
108 | |
{ |
109 | 0 | BodyBuilder builder = new BodyBuilder(); |
110 | 0 | builder.begin(); |
111 | 0 | builder.addln("java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");", |
112 | |
sourceName, |
113 | |
metaKey); |
114 | 0 | builder.addln("return {0}(meta);", parser); |
115 | 0 | builder.end(); |
116 | |
|
117 | 0 | op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location); |
118 | 0 | } |
119 | |
|
120 | |
private void addBoolean(EnhancementOperation op, String metaKey, String propertyName, |
121 | |
MethodSignature sig, String sourceName, Location location) |
122 | |
{ |
123 | 0 | BodyBuilder builder = new BodyBuilder(); |
124 | 0 | builder.begin(); |
125 | 0 | builder.addln( |
126 | |
"java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");", |
127 | |
sourceName, |
128 | |
metaKey); |
129 | 0 | builder.addln("return java.lang.Boolean.valueOf(meta).booleanValue();"); |
130 | 0 | builder.end(); |
131 | |
|
132 | 0 | op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location); |
133 | 0 | } |
134 | |
|
135 | |
private void addCharacterPrimitive(EnhancementOperation op, String metaKey, |
136 | |
String propertyName, MethodSignature sig, String sourceName, Location location) |
137 | |
{ |
138 | 0 | BodyBuilder builder = new BodyBuilder(); |
139 | 0 | builder.begin(); |
140 | 0 | builder.addln( |
141 | |
"java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");", |
142 | |
sourceName, |
143 | |
metaKey); |
144 | 0 | builder.addln("return meta.charAt(0);"); |
145 | 0 | builder.end(); |
146 | |
|
147 | 0 | op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location); |
148 | 0 | } |
149 | |
|
150 | |
private void addObject(EnhancementOperation op, String metaKey, String propertyName, |
151 | |
Class propertyType, MethodSignature sig, String sourceName, Location location) |
152 | |
{ |
153 | 0 | String valueConverterName = op.addInjectedField("_$valueConverter", ValueConverter.class, _valueConverter); |
154 | |
|
155 | 0 | String classRef = op.getClassReference(propertyType); |
156 | |
|
157 | 0 | BodyBuilder builder = new BodyBuilder(); |
158 | 0 | builder.begin(); |
159 | 0 | builder.addln("java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");", |
160 | |
sourceName, |
161 | |
metaKey); |
162 | 0 | builder.addln("return ({0}) {1}.coerceValue(meta, {2});", ClassFabUtils |
163 | |
.getJavaClassName(propertyType), valueConverterName, classRef); |
164 | 0 | builder.end(); |
165 | |
|
166 | 0 | op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location); |
167 | 0 | } |
168 | |
|
169 | |
public void setSource(ComponentPropertySource source) |
170 | |
{ |
171 | 0 | _source = source; |
172 | 0 | } |
173 | |
|
174 | |
public void setValueConverter(ValueConverter valueConverter) |
175 | |
{ |
176 | 0 | _valueConverter = valueConverter; |
177 | 0 | } |
178 | |
} |