Author: hlship
Date: Fri Feb 19 18:54:07 2010
New Revision: 911926
URL: http://svn.apache.org/viewvc?rev=911926&view=rev
Log:
Record PropertyWorker to create empty methods and immediately add advice
(leveraging FieldAccess)
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PropertyWorker.java
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PropertyWorker.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PropertyWorker.java?rev=911926&r1=911925&r2=911926&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PropertyWorker.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PropertyWorker.java
Fri Feb 19 18:54:07 2010
@@ -1,10 +1,10 @@
-// Copyright 2008 The Apache Software Foundation
+// Copyright 2008, 2010 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
-// http://www.apache.org/licenses/LICENSE-2.0
+// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,54 +14,79 @@
package org.apache.tapestry5.internal.transform;
+import java.lang.reflect.Modifier;
+
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.internal.util.InternalUtils;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.services.ClassTransformation;
import org.apache.tapestry5.services.ComponentClassTransformWorker;
+import org.apache.tapestry5.services.ComponentMethodAdvice;
+import org.apache.tapestry5.services.ComponentMethodInvocation;
+import org.apache.tapestry5.services.FieldAccess;
+import org.apache.tapestry5.services.TransformField;
import org.apache.tapestry5.services.TransformMethodSignature;
-import java.lang.reflect.Modifier;
-
/**
* Provides the getter and setter methods. The methods are added as
"existing", meaning that field access to them will
* be transformed as necessary by other annotations. This worker needs to be
scheduled before any worker that might
* delete a field.
- *
+ *
* @see org.apache.tapestry5.annotations.Property
*/
public class PropertyWorker implements ComponentClassTransformWorker
{
public void transform(ClassTransformation transformation,
MutableComponentModel model)
{
- for (String fieldName :
transformation.findFieldsWithAnnotation(Property.class))
+ for (TransformField field :
transformation.matchFieldsWithAnnotation(Property.class))
{
- Property annotation = transformation.getFieldAnnotation(fieldName,
Property.class);
+ createAccessorsForField(transformation, field);
+ }
+ }
- String propertyName =
InternalUtils.capitalize(InternalUtils.stripMemberName(fieldName));
+ private void createAccessorsForField(ClassTransformation transformation,
TransformField field)
+ {
+ Property annotation = field.getAnnotation(Property.class);
- String fieldType = transformation.getFieldType(fieldName);
+ String propertyName =
InternalUtils.capitalize(InternalUtils.stripMemberName(field.getName()));
- if (annotation.read())
- {
- TransformMethodSignature getter
- = new TransformMethodSignature(Modifier.PUBLIC |
Modifier.FINAL, fieldType,
- "get" + propertyName,
- null, null);
+ if (annotation.read())
+ addGetter(transformation, field, propertyName);
- transformation.addTransformedMethod(getter, "return " +
fieldName + ";");
- }
+ if (annotation.write())
+ addSetter(transformation, field, propertyName);
+ }
- if (annotation.write())
- {
- TransformMethodSignature setter
- = new TransformMethodSignature(Modifier.PUBLIC |
Modifier.FINAL, "void", "set" + propertyName,
- new String[]
{fieldType}, null);
+ private void addSetter(ClassTransformation transformation, TransformField
field, String propertyName)
+ {
+ TransformMethodSignature setter = new
TransformMethodSignature(Modifier.PUBLIC, "void", "set" + propertyName,
+ new String[]
+ { field.getType() }, null);
- transformation.addTransformedMethod(setter, fieldName + " =
$1;");
+ final FieldAccess access = field.getAccess();
+
+ transformation.createMethod(setter).addAdvice(new
ComponentMethodAdvice()
+ {
+ public void advise(ComponentMethodInvocation invocation)
+ {
+ access.write(invocation.getInstance(),
invocation.getParameter(0));
}
+ });
+ }
- // The field is NOT claimed, because we want annotation for the
fields to operate.
- }
+ private void addGetter(ClassTransformation transformation, TransformField
field, String propertyName)
+ {
+ TransformMethodSignature getter = new
TransformMethodSignature(Modifier.PUBLIC, field.getType(), "get"
+ + propertyName, null, null);
+
+ final FieldAccess access = field.getAccess();
+
+ transformation.createMethod(getter).addAdvice(new
ComponentMethodAdvice()
+ {
+ public void advise(ComponentMethodInvocation invocation)
+ {
+
invocation.overrideResult(access.read(invocation.getInstance()));
+ }
+ });
}
}
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java?rev=911926&r1=911925&r2=911926&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java
Fri Feb 19 18:54:07 2010
@@ -813,7 +813,6 @@
assertTextPresent("Class
org.apache.tapestry5.integration.app1.pages.Datum contains field(s) (_value)
that are not private. You should change these fields to private, and add
accessor methods if needed.");
}
-
@Test
public void method_advice()
{
@@ -958,7 +957,7 @@
{
clickThru("Getter Method Already Exists");
- assertTextPresent("Unable to add new method public final
java.lang.String getName() as it already exists.");
+ assertTextPresent("Unable to create new method public java.lang.String
getName() as it already exists in class
org.apache.tapestry5.integration.app1.pages.GetterMethodAlreadyExists.");
}
/**