01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import java.util.List;
18:
19: import org.apache.tapestry.Block;
20: import org.apache.tapestry.annotations.Id;
21: import org.apache.tapestry.annotations.Inject;
22: import org.apache.tapestry.ioc.internal.util.InternalUtils;
23: import org.apache.tapestry.ioc.util.BodyBuilder;
24: import org.apache.tapestry.model.MutableComponentModel;
25: import org.apache.tapestry.services.ClassTransformation;
26: import org.apache.tapestry.services.ComponentClassTransformWorker;
27: import org.apache.tapestry.services.TransformConstants;
28:
29: /**
30: * Identifies fields of type {@link Block} that have the {@link Inject} annotation and converts them
31: * into read-only fields containing the injected Block from the template. The annotation's value is
32: * the id of the block to inject; if ommitted, the block id is deduced from the field id.
33: * <p>
34: * Must be scheduled before {@link InjectWorker} because it uses the same annotation, Inject, with a
35: * different interpretation.
36: */
37: public class InjectBlockWorker implements ComponentClassTransformWorker {
38: static final String BLOCK_TYPE_NAME = Block.class.getName();
39:
40: public void transform(ClassTransformation transformation,
41: MutableComponentModel model) {
42: List<String> fieldNames = transformation
43: .findFieldsOfType(BLOCK_TYPE_NAME);
44:
45: if (fieldNames.isEmpty())
46: return;
47:
48: BodyBuilder builder = new BodyBuilder();
49: builder.begin();
50:
51: int count = 0;
52:
53: String resourcesFieldName = transformation
54: .getResourcesFieldName();
55:
56: for (String fieldName : fieldNames) {
57: Inject injectAnnotation = transformation
58: .getFieldAnnotation(fieldName, Inject.class);
59:
60: if (injectAnnotation == null)
61: continue;
62:
63: Id annotation = transformation.getFieldAnnotation(
64: fieldName, Id.class);
65:
66: String blockId = getBlockId(fieldName, annotation);
67:
68: builder.addln("%s = %s.getBlock(\"%s\");", fieldName,
69: resourcesFieldName, blockId);
70:
71: transformation.makeReadOnly(fieldName);
72:
73: transformation.claimField(fieldName, injectAnnotation);
74:
75: count++;
76: }
77:
78: // Fields yes, but no annotations, so nothing to really do.
79:
80: if (count == 0)
81: return;
82:
83: builder.end();
84:
85: transformation.extendMethod(
86: TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE,
87: builder.toString());
88: }
89:
90: private String getBlockId(String fieldName, Id annotation) {
91: if (annotation != null)
92: return annotation.value();
93:
94: return InternalUtils.stripMemberPrefix(fieldName);
95: }
96:
97: }
|