01: // Copyright 2006 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.annotations.Mixin;
20: import org.apache.tapestry.ioc.internal.util.InternalUtils;
21: import org.apache.tapestry.model.MutableComponentModel;
22: import org.apache.tapestry.services.ClassTransformation;
23: import org.apache.tapestry.services.ComponentClassResolver;
24: import org.apache.tapestry.services.ComponentClassTransformWorker;
25: import org.apache.tapestry.services.TransformConstants;
26:
27: /**
28: * Supports the {@link Mixin} annotation, which allows a mixin to be part of the implementation of a
29: * component. The annotation is applied to a field, which will become read-only, and contain a
30: * reference to the mixin instance.
31: */
32: public class MixinWorker implements ComponentClassTransformWorker {
33: private final ComponentClassResolver _resolver;
34:
35: public MixinWorker(final ComponentClassResolver resolver) {
36: _resolver = resolver;
37: }
38:
39: public void transform(ClassTransformation transformation,
40: MutableComponentModel model) {
41: List<String> fields = transformation
42: .findFieldsWithAnnotation(Mixin.class);
43:
44: for (String fieldName : fields) {
45: Mixin annotation = transformation.getFieldAnnotation(
46: fieldName, Mixin.class);
47:
48: String mixinType = annotation.value();
49:
50: String fieldType = transformation.getFieldType(fieldName);
51:
52: String mixinClassName = InternalUtils.isBlank(mixinType) ? fieldType
53: : _resolver.resolveMixinTypeToClassName(mixinType);
54:
55: model.addMixinClassName(mixinClassName);
56:
57: transformation.makeReadOnly(fieldName);
58:
59: String body = String.format(
60: "%s = (%s) %s.getMixinByClassName(\"%s\");",
61: fieldName, fieldType, transformation
62: .getResourcesFieldName(), mixinClassName);
63:
64: transformation
65: .extendMethod(
66: TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE,
67: body);
68:
69: transformation.claimField(fieldName, annotation);
70: }
71: }
72: }
|