| 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.InjectContainer; |
| 18 | |
import org.apache.tapestry5.ioc.util.BodyBuilder; |
| 19 | |
import org.apache.tapestry5.model.MutableComponentModel; |
| 20 | |
import org.apache.tapestry5.runtime.Component; |
| 21 | |
import org.apache.tapestry5.services.ClassTransformation; |
| 22 | |
import org.apache.tapestry5.services.ComponentClassTransformWorker; |
| 23 | |
import org.apache.tapestry5.services.TransformConstants; |
| 24 | |
|
| 25 | |
import java.util.List; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 58 | public class InjectContainerWorker implements ComponentClassTransformWorker |
| 32 | |
{ |
| 33 | |
|
| 34 | |
public void transform(ClassTransformation transformation, MutableComponentModel model) |
| 35 | |
{ |
| 36 | 814 | List<String> names = transformation.findFieldsWithAnnotation(InjectContainer.class); |
| 37 | |
|
| 38 | 814 | if (names.isEmpty()) |
| 39 | 788 | return; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 26 | BodyBuilder builder = new BodyBuilder(); |
| 45 | 26 | builder.begin(); |
| 46 | |
|
| 47 | 26 | builder.addln("%s container = %s.getContainer();", Component.class.getName(), transformation |
| 48 | |
.getResourcesFieldName()); |
| 49 | |
|
| 50 | 26 | for (String fieldName : names) |
| 51 | |
{ |
| 52 | 26 | InjectContainer annotation = transformation.getFieldAnnotation( |
| 53 | |
fieldName, |
| 54 | |
InjectContainer.class); |
| 55 | |
|
| 56 | 26 | transformation.claimField(fieldName, annotation); |
| 57 | |
|
| 58 | 26 | String fieldType = transformation.getFieldType(fieldName); |
| 59 | |
|
| 60 | 26 | builder.addln("try"); |
| 61 | 26 | builder.begin(); |
| 62 | 26 | builder.addln("this.%s = (%s) container;", fieldName, fieldType); |
| 63 | 26 | builder.end(); |
| 64 | 26 | builder.addln("catch (ClassCastException ex)"); |
| 65 | 26 | builder.begin(); |
| 66 | 26 | builder.addln( |
| 67 | |
"String message = %s.buildCastExceptionMessage(container, \"%s.%s\", \"%s\");", |
| 68 | |
InjectContainerWorker.class.getName(), |
| 69 | |
model.getComponentClassName(), |
| 70 | |
fieldName, |
| 71 | |
fieldType); |
| 72 | 26 | builder.addln("throw new RuntimeException(message, ex);"); |
| 73 | 26 | builder.end(); |
| 74 | |
|
| 75 | 26 | transformation.makeReadOnly(fieldName); |
| 76 | 26 | } |
| 77 | |
|
| 78 | 26 | builder.end(); |
| 79 | |
|
| 80 | 26 | transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder |
| 81 | |
.toString()); |
| 82 | 26 | } |
| 83 | |
|
| 84 | |
public static String buildCastExceptionMessage(Component component, String fieldName, |
| 85 | |
String fieldType) |
| 86 | |
{ |
| 87 | 2 | return TransformMessages.componentNotAssignableToField(component, fieldName, fieldType); |
| 88 | |
} |
| 89 | |
} |