Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
InitialValueAnnotationWorker |
|
| 3.0;3 |
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // 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.annotations; | |
16 | ||
17 | import java.lang.reflect.Method; | |
18 | ||
19 | import org.apache.hivemind.Location; | |
20 | import org.apache.hivemind.Resource; | |
21 | import org.apache.tapestry.enhance.EnhancementOperation; | |
22 | import org.apache.tapestry.spec.IComponentSpecification; | |
23 | import org.apache.tapestry.spec.IPropertySpecification; | |
24 | import org.apache.tapestry.spec.PropertySpecification; | |
25 | ||
26 | /** | |
27 | * Looks for {@link org.apache.tapestry.annotations.InitialValue} annotations on methods that don't | |
28 | * have a {@link org.apache.tapestry.annotations.Persist} annotation (that's handled by | |
29 | * {@link org.apache.tapestry.annotations.PersistAnnotationWorker}); adds an | |
30 | * {@link org.apache.tapestry.spec.IPropertySpecification} for the property, so that its initial | |
31 | * value may be set. | |
32 | * | |
33 | * @author Howard M. Lewis Ship | |
34 | * @since 4.0 | |
35 | */ | |
36 | 0 | public class InitialValueAnnotationWorker implements SecondaryAnnotationWorker |
37 | { | |
38 | /** | |
39 | * Returns true if the method has the InitialValue annotation. | |
40 | */ | |
41 | public boolean canEnhance(Method method) | |
42 | { | |
43 | 0 | return method.getAnnotation(InitialValue.class) != null; |
44 | } | |
45 | ||
46 | public void peformEnhancement(EnhancementOperation op, IComponentSpecification spec, | |
47 | Method method, Resource classResource) | |
48 | { | |
49 | 0 | InitialValue iv = method.getAnnotation(InitialValue.class); |
50 | ||
51 | 0 | if (iv == null) |
52 | 0 | return; |
53 | ||
54 | 0 | if (method.getAnnotation(Persist.class) != null) |
55 | 0 | return; |
56 | ||
57 | 0 | Location location = AnnotationUtils.buildLocationForAnnotation(method, iv, classResource); |
58 | ||
59 | 0 | String propertyName = AnnotationUtils.getPropertyName(method); |
60 | ||
61 | // Define a transient property | |
62 | ||
63 | 0 | IPropertySpecification pspec = new PropertySpecification(); |
64 | ||
65 | 0 | pspec.setName(propertyName); |
66 | 0 | pspec.setLocation(location); |
67 | 0 | pspec.setInitialValue(iv.value()); |
68 | ||
69 | 0 | spec.addPropertySpecification(pspec); |
70 | 0 | } |
71 | } |