|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
InjectPageWorker.java | 100% | 100% | 100% | 100% |
|
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.enhance; | |
16 | ||
17 | import java.lang.reflect.Modifier; | |
18 | ||
19 | import org.apache.hivemind.ApplicationRuntimeException; | |
20 | import org.apache.hivemind.Location; | |
21 | import org.apache.hivemind.service.BodyBuilder; | |
22 | import org.apache.hivemind.service.MethodSignature; | |
23 | import org.apache.tapestry.IPage; | |
24 | import org.apache.tapestry.spec.InjectSpecification; | |
25 | ||
26 | /** | |
27 | * Injects code to access a named page within the application. | |
28 | * | |
29 | * @author Howard Lewis Ship | |
30 | * @since 4.0 | |
31 | */ | |
32 | public class InjectPageWorker implements InjectEnhancementWorker | |
33 | { | |
34 | 3 | public void performEnhancement(EnhancementOperation op, InjectSpecification spec) |
35 | { | |
36 | 3 | performEnhancement(op, spec.getObject(), spec.getProperty(), spec.getLocation()); |
37 | } | |
38 | ||
39 | 3 | public void performEnhancement(EnhancementOperation op, String pageName, String propertyName, |
40 | Location location) | |
41 | { | |
42 | 3 | Class propertyType = op.getPropertyType(propertyName); |
43 | ||
44 | 3 | if (propertyType == null) |
45 | 1 | propertyType = Object.class; |
46 | 2 | else if (propertyType.isPrimitive()) |
47 | 1 | throw new ApplicationRuntimeException(EnhanceMessages.wrongTypeForPageInjection( |
48 | propertyName, | |
49 | propertyType), null, location, null); | |
50 | ||
51 | 2 | op.claimProperty(propertyName); |
52 | ||
53 | 2 | MethodSignature sig = new MethodSignature(propertyType, op |
54 | .getAccessorMethodName(propertyName), null, null); | |
55 | ||
56 | 2 | BodyBuilder builder = new BodyBuilder(); |
57 | ||
58 | 2 | builder.add("return "); |
59 | ||
60 | // If the property type is not IPage or a superclass of IPage then a cast | |
61 | // is needed. | |
62 | ||
63 | 2 | if (!propertyType.isAssignableFrom(IPage.class)) |
64 | 1 | builder.add("({0})", propertyType.getName()); |
65 | ||
66 | 2 | builder.add("getPage().getRequestCycle().getPage(\"{0}\");", pageName); |
67 | ||
68 | 2 | op.addMethod(Modifier.PUBLIC, sig, builder.toString()); |
69 | } | |
70 | } |
|