| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.transform; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.annotations.InjectPage; |
| 18 | |
import org.apache.tapestry5.ioc.internal.util.InternalUtils; |
| 19 | |
import org.apache.tapestry5.ioc.util.BodyBuilder; |
| 20 | |
import org.apache.tapestry5.model.MutableComponentModel; |
| 21 | |
import org.apache.tapestry5.services.*; |
| 22 | |
|
| 23 | |
import java.lang.reflect.Modifier; |
| 24 | |
import java.util.List; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class InjectPageWorker implements ComponentClassTransformWorker |
| 32 | |
{ |
| 33 | |
private final ComponentSource componentSource; |
| 34 | |
|
| 35 | |
private final ComponentClassResolver resolver; |
| 36 | |
|
| 37 | |
public InjectPageWorker(ComponentSource componentSource, ComponentClassResolver resolver) |
| 38 | 58 | { |
| 39 | 58 | this.componentSource = componentSource; |
| 40 | 58 | this.resolver = resolver; |
| 41 | 58 | } |
| 42 | |
|
| 43 | |
public void transform(ClassTransformation transformation, MutableComponentModel model) |
| 44 | |
{ |
| 45 | 814 | List<String> names = transformation.findFieldsWithAnnotation(InjectPage.class); |
| 46 | |
|
| 47 | 814 | if (names.isEmpty()) return; |
| 48 | |
|
| 49 | 40 | String componentSource = transformation.addInjectedField(ComponentSource.class, "componentSource", |
| 50 | |
this.componentSource); |
| 51 | |
|
| 52 | |
|
| 53 | 40 | for (String name : names) |
| 54 | 42 | addInjectedPage(transformation, name, componentSource); |
| 55 | |
|
| 56 | 40 | } |
| 57 | |
|
| 58 | |
private void addInjectedPage(ClassTransformation transformation, String fieldName, String componentSource) |
| 59 | |
{ |
| 60 | 42 | InjectPage annotation = transformation.getFieldAnnotation(fieldName, InjectPage.class); |
| 61 | |
|
| 62 | 42 | transformation.claimField(fieldName, annotation); |
| 63 | |
|
| 64 | 42 | String pageName = annotation.value(); |
| 65 | |
|
| 66 | 42 | String fieldType = transformation.getFieldType(fieldName); |
| 67 | 42 | String methodName = transformation.newMemberName("read_inject_page", fieldName); |
| 68 | |
|
| 69 | 42 | String injectedPageName = InternalUtils.isBlank(pageName) ? resolver |
| 70 | |
.resolvePageClassNameToPageName(fieldType) : pageName; |
| 71 | |
|
| 72 | 42 | TransformMethodSignature sig = new TransformMethodSignature(Modifier.PRIVATE, fieldType, methodName, null, |
| 73 | |
null); |
| 74 | |
|
| 75 | 42 | BodyBuilder builder = new BodyBuilder(); |
| 76 | 42 | builder.begin(); |
| 77 | |
|
| 78 | 42 | builder.addln("return (%s) %s.getPage(\"%s\");", fieldType, componentSource, injectedPageName); |
| 79 | |
|
| 80 | 42 | builder.end(); |
| 81 | |
|
| 82 | 42 | transformation.addMethod(sig, builder.toString()); |
| 83 | 42 | transformation.replaceReadAccess(fieldName, methodName); |
| 84 | 42 | transformation.makeReadOnly(fieldName); |
| 85 | 42 | transformation.removeField(fieldName); |
| 86 | 42 | } |
| 87 | |
} |