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.lang.annotation.Annotation;
18:
19: import org.apache.tapestry.internal.util.MethodInvocationBuilder;
20: import org.apache.tapestry.model.MutableComponentModel;
21: import org.apache.tapestry.services.ClassTransformation;
22: import org.apache.tapestry.services.ComponentClassTransformWorker;
23: import org.apache.tapestry.services.MethodFilter;
24: import org.apache.tapestry.services.MethodSignature;
25:
26: /**
27: * Similar to {@link ComponentLifecycleMethodWorker} but applies to annotations/methods related to
28: * the overall page lifecycle.
29: */
30: public class PageLifecycleAnnotationWorker implements
31: ComponentClassTransformWorker {
32: private final Class<? extends Annotation> _methodAnnotationClass;
33:
34: private final MethodSignature _lifecycleMethodSignature;
35:
36: private final String _methodAlias;
37:
38: private final MethodInvocationBuilder _invocationBuilder = new MethodInvocationBuilder();
39:
40: public PageLifecycleAnnotationWorker(
41: final Class<? extends Annotation> methodAnnotationClass,
42: final MethodSignature lifecycleMethodSignature,
43: final String methodAlias) {
44: _methodAnnotationClass = methodAnnotationClass;
45: _lifecycleMethodSignature = lifecycleMethodSignature;
46: _methodAlias = methodAlias;
47: }
48:
49: public void transform(final ClassTransformation transformation,
50: MutableComponentModel model) {
51: MethodFilter filter = new MethodFilter() {
52: public boolean accept(MethodSignature signature) {
53: if (signature.getMethodName().equals(_methodAlias))
54: return true;
55:
56: return transformation.getMethodAnnotation(signature,
57: _methodAnnotationClass) != null;
58: }
59: };
60:
61: // Did this they easy way, because I doubt there will be more than one signature.
62: // If I expected lots of signatures, I'd build up a BodyBuilder in the loop and extend the
63: // method outside the loop.
64:
65: for (MethodSignature signature : transformation
66: .findMethods(filter)) {
67: // TODO: Filter out the non-void methods (with a non-fatal warning/error?) For the
68: // moment, we just invoke the method anyway, and ignore the result. Also, MethodInvocationBuilder
69: // is very forgiving (and silent) about unexpected parameters (passing null/0/false).
70:
71: String body = _invocationBuilder.buildMethodInvocation(
72: signature, transformation);
73:
74: transformation.extendMethod(_lifecycleMethodSignature, body
75: + ";");
76: }
77: }
78:
79: }
|