Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MetaAnnotationWorker |
|
| 3.3333333333333335;3.333 |
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.util.ArrayList; | |
18 | import java.util.Collections; | |
19 | import java.util.List; | |
20 | ||
21 | import org.apache.hivemind.ApplicationRuntimeException; | |
22 | import org.apache.hivemind.Location; | |
23 | import org.apache.tapestry.enhance.EnhancementOperation; | |
24 | import org.apache.tapestry.spec.IComponentSpecification; | |
25 | ||
26 | /** | |
27 | * Recognizes the {@link org.apache.tapestry.annotations.Meta} annotation, and converts it into | |
28 | * properties on the specification. It is desirable to have meta data in base classes be "merged" | |
29 | * with meta data from sub classes, with the sub classes overriding any conflicting elements from | |
30 | * the base class. What we do is work our way up the inheritance tree to java.lang.Object and work | |
31 | * with each Meta annotation we find. | |
32 | * | |
33 | * @author Howard M. Lewis Ship | |
34 | * @since 4.0 | |
35 | */ | |
36 | 0 | public class MetaAnnotationWorker implements ClassAnnotationEnhancementWorker |
37 | { | |
38 | ||
39 | public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, | |
40 | Class baseClass, Location location) | |
41 | { | |
42 | 0 | List<Meta> metas = assembleMetas(baseClass); |
43 | ||
44 | 0 | for (Meta meta : metas) |
45 | 0 | applyPropertiesFromMeta(meta, spec, location); |
46 | 0 | } |
47 | ||
48 | private List<Meta> assembleMetas(Class baseClass) | |
49 | { | |
50 | 0 | Class searchClass = baseClass; |
51 | 0 | List<Meta> result = new ArrayList<Meta>(); |
52 | 0 | Meta lastMeta = null; |
53 | ||
54 | while (true) | |
55 | { | |
56 | 0 | Meta meta = (Meta) searchClass.getAnnotation(Meta.class); |
57 | ||
58 | // When reach a class that doesn't define or inherit a @Meta, we're done | |
59 | 0 | if (meta == null) |
60 | 0 | break; |
61 | ||
62 | // Cheap approach, based on annotation inheritance, for determining | |
63 | // whether a meta is already in the result list | |
64 | ||
65 | 0 | if (meta != lastMeta) |
66 | 0 | result.add(meta); |
67 | ||
68 | 0 | searchClass = searchClass.getSuperclass(); |
69 | 0 | } |
70 | ||
71 | 0 | Collections.reverse(result); |
72 | ||
73 | 0 | return result; |
74 | } | |
75 | ||
76 | private void applyPropertiesFromMeta(Meta meta, IComponentSpecification spec, Location location) | |
77 | { | |
78 | 0 | String[] pairs = meta.value(); |
79 | ||
80 | 0 | for (String pair : pairs) |
81 | { | |
82 | 0 | int equalx = pair.indexOf('='); |
83 | ||
84 | // The only big problem is that the location will be the location of the @Meta in | |
85 | // the subclass, even if the @Meta with a problem is from a base class. | |
86 | ||
87 | 0 | if (equalx < 0) |
88 | 0 | throw new ApplicationRuntimeException(AnnotationMessages.missingEqualsInMeta(pair), |
89 | location, null); | |
90 | ||
91 | 0 | String key = pair.substring(0, equalx); |
92 | 0 | String value = pair.substring(equalx + 1); |
93 | ||
94 | 0 | spec.setProperty(key, value); |
95 | } | |
96 | 0 | } |
97 | } |