You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/04/30 00:55:13 UTC
svn commit: r1097990 - in /tapestry/tapestry5/trunk/plastic/src:
main/java/org/apache/tapestry5/internal/plastic/
main/java/org/apache/tapestry5/plastic/
test/groovy/org/apache/tapestry5/plastic/
Author: hlship
Date: Fri Apr 29 22:55:13 2011
New Revision: 1097990
URL: http://svn.apache.org/viewvc?rev=1097990&view=rev
Log:
TAP5-853: Allow a field's conduit to be a computed value
Modified:
tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java
tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticField.java
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy
Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java?rev=1097990&r1=1097989&r2=1097990&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java Fri Apr 29 22:55:13 2011
@@ -707,6 +707,30 @@ public class PlasticClassImpl extends Lo
return null;
}
+ public PlasticField setComputedConduit(ComputedValue<FieldConduit<?>> computedConduit)
+ {
+ assert computedConduit != null;
+
+ check();
+
+ verifyInitialState("set the computed FieldConduit for");
+
+ // First step: define a field to store the conduit and add constructor logic
+ // to initialize it
+
+ PlasticField conduitField = introduceField(FieldConduit.class, node.name + "_FieldConduit").injectComputed(
+ computedConduit);
+
+ replaceFieldReadAccess(conduitField.getName());
+ replaceFieldWriteAccess(conduitField.getName());
+
+ // TODO: Do we keep the field or not? It will now always be null/0/false.
+
+ state = FieldState.CONDUIT;
+
+ return null;
+ }
+
public PlasticField createAccessors(PropertyAccessType accessType)
{
check();
@@ -925,6 +949,7 @@ public class PlasticClassImpl extends Lo
}
});
}
+
}
/**
Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticField.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticField.java?rev=1097990&r1=1097989&r2=1097990&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticField.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticField.java Fri Apr 29 22:55:13 2011
@@ -24,7 +24,7 @@ public interface PlasticField extends An
{
/** Returns the class containing this field. */
PlasticClass getPlasticClass();
-
+
/**
* Returns a handle that can be used to directly access a private field of a
* transformed class instance.
@@ -109,10 +109,20 @@ public interface PlasticField extends An
* @return the field for further manipulation
* @throws IllegalStateException
* if the field already has an injection or a conduit
+ * @return this field for further manipulation
*/
PlasticField setConduit(FieldConduit<?> conduit);
/**
+ * Sets the conduit for the field to a value computed when the class is instantiated
+ *
+ * @param computedConduit
+ * object that will compute the actual conduit to be used
+ * @return this field for further manipulation
+ */
+ PlasticField setComputedConduit(ComputedValue<FieldConduit<?>> computedConduit);
+
+ /**
* Creates access to the field, using the default property name derived from the name of the field.
* The default property name is the same as the name of the field, but with any leading or trailing underscore
* characters removed (a common convention among some programmers). Also, strips leading "m_" from the field name
Modified: tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy?rev=1097990&r1=1097989&r2=1097990&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy (original)
+++ tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy Fri Apr 29 22:55:13 2011
@@ -17,60 +17,78 @@ package org.apache.tapestry5.plastic
import org.apache.tapestry5.plastic.FieldConduit;
import org.apache.tapestry5.plastic.PlasticManager;
-import spock.lang.Specification
+import spock.lang.Specification
class FieldConduitTests extends Specification
{
def mgr = new PlasticManager()
-
+
def "setting a field invokes the conduit"() {
-
+
FieldConduit fc = Mock()
-
+
def pc = mgr.getPlasticClass("testsubjects.IntFieldHolder")
-
+
pc.allFields.first().setConduit(fc)
-
+
def o = pc.createInstantiator().newInstance()
-
+
when:
-
+
o.setValue(123)
-
+
then:
-
- 1 * fc.set(_, 123)
-
+
+ 1 * fc.set(_, 123)
+
when:
fc.get(_) >>> 999
-
+
then:
-
+
o.getValue() == 999
-
+
expect:
-
+
// The field doesn't really get used (it may be removed some day, though its useful because of the
// annotations that may be on it).
-
- o.@value == 0
+
+ o.@value == 0
}
-
- def "field initializations are visible to the conduit"() {
+
+ def "use of computed conduit"() {
FieldConduit fc = Mock()
-
+
+ def pc = mgr.getPlasticClass("testsubjects.IntFieldHolder")
+
+ pc.allFields.first().setComputedConduit({ return fc } as ComputedValue)
+
+ def o = pc.createInstantiator().newInstance()
+
+ when:
+
+ o.setValue(456)
+
+ then:
+
+ 1 * fc.set(_, 456)
+ }
+
+ def "field initializations are visible to the conduit"() {
+ FieldConduit fc = Mock()
+
def pc = mgr.getPlasticClass("testsubjects.LongFieldHolder")
-
+
pc.allFields.first().setConduit(fc)
-
+
when:
-
+
pc.createInstantiator().newInstance()
-
+
then:
-
+
// 100 is the initial value of the field
-
+
1 * fc.set(_, 100)
}
}