You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by da...@apache.org on 2007/10/15 23:26:43 UTC

svn commit: r584927 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry/internal/services/ main/java/org/apache/tapestry/services/ main/java/org/apache/tapestry/test/ test/java/org/apache/tapestry/integration/app2/ test/java...

Author: dadams
Date: Mon Oct 15 14:26:40 2007
New Revision: 584927

URL: http://svn.apache.org/viewvc?rev=584927&view=rev
Log:
Fixed TAPESTRY-1615: ClassTransformation needs ability to rename a method and extend the beginning of a method

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/FortyTwo.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod2.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/pagelevel/PrefixMethodTest.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.tml
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ClassTransformation.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/PageTester.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/services/LocaleAppModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/InternalClassTransformationImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java?rev=584927&r1=584926&r2=584927&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java Mon Oct 15 14:26:40 2007
@@ -649,6 +649,27 @@
 
         _addedMethods.add(method);
     }
+    
+    public void prefixMethod(TransformMethodSignature methodSignature, String methodBody)
+    {
+        failIfFrozen();
+        
+        CtMethod method = findMethod(methodSignature);
+        try
+        {
+            method.insertBefore(methodBody);
+        }
+        catch (CannotCompileException ex)
+        {
+            throw new MethodCompileException(ServicesMessages.methodCompileError(
+                    methodSignature,
+                    methodBody,
+                    ex), methodBody, ex);
+        }
+        
+        addMethodToDescription("prefix", methodSignature, methodBody);
+        _addedMethods.add(method);
+    }
 
     private void addMethodToDescription(String operation, TransformMethodSignature methodSignature,
             String methodBody)

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ClassTransformation.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ClassTransformation.java?rev=584927&r1=584926&r2=584927&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ClassTransformation.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ClassTransformation.java Mon Oct 15 14:26:40 2007
@@ -17,6 +17,8 @@
 import java.lang.annotation.Annotation;
 import java.util.List;
 
+import javassist.CtBehavior;
+
 import org.apache.tapestry.ioc.AnnotationProvider;
 import org.slf4j.Logger;
 
@@ -265,6 +267,20 @@
      */
     void extendMethod(TransformMethodSignature methodSignature, String methodBody);
 
+    /**
+     * Inserts code at the beginning of a method body (i.e. {@link CtBehavior#insertBefore(String)}.
+     * <p>
+     * The method may be declared in the class, or may be inherited from a super-class. For
+     * inherited methods, a method is added that first invokes the super implementation. Use
+     * {@link #addMethod(TransformMethodSignature, String)} when it is necessary to control when the
+     * super-class method is invoked.
+     * 
+     * @param methodSignature
+     * @param methodBody
+     * @throws IllegalArgumentException If the provided Javassist method body could not be compiled
+     */
+    void prefixMethod(TransformMethodSignature methodSignature, String methodBody);
+    
     /**
      * Returns the name of a field that provides the {@link org.apache.tapestry.ComponentResources}
      * for the transformed component. This will be a protected field, accessible to the class and

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/PageTester.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/PageTester.java?rev=584927&r1=584926&r2=584927&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/PageTester.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/PageTester.java Mon Oct 15 14:26:40 2007
@@ -93,8 +93,10 @@
      * @param contextPath
      *            The path to the context root so that Tapestry can find the templates (if they're
      *            put there).
+     * @param modulesClasses
+     *            Classes of additional modules to load
      */
-    public PageTester(String appPackage, String appName, String contextPath)
+    public PageTester(String appPackage, String appName, String contextPath, Class... moduleClasses)
     {
         _preferedLanguage = Locale.ENGLISH;
         _contextPath = contextPath;
@@ -106,7 +108,8 @@
                 PageTesterModule.TEST_MODE);
 
         initializer.addModules(PageTesterModule.class);
-
+        initializer.addModules(moduleClasses);
+        
         _registry = initializer.getRegistry();
 
         _request = _registry.getObject(TestableRequest.class, null);

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/FortyTwo.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/FortyTwo.java?rev=584927&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/FortyTwo.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/FortyTwo.java Mon Oct 15 14:26:40 2007
@@ -0,0 +1,33 @@
+// Copyright 2007 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
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.integration.app2;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/** Indicates that a method should return the answer to life, the universe, and everything.
+ * Used for testing.
+ */
+@Target(ElementType.METHOD)
+@Retention(RUNTIME)
+@Documented
+public @interface FortyTwo
+{
+
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.java?rev=584927&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.java Mon Oct 15 14:26:40 2007
@@ -0,0 +1,26 @@
+// Copyright 2007 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
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.integration.app2.pages;
+
+import org.apache.tapestry.integration.app2.FortyTwo;
+
+public class TestPrefixMethod
+{
+    @FortyTwo
+    public int getValue() {
+        return 0;
+    }
+    
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod2.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod2.java?rev=584927&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod2.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/pages/TestPrefixMethod2.java Mon Oct 15 14:26:40 2007
@@ -0,0 +1,20 @@
+// Copyright 2007 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
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.integration.app2.pages;
+
+public class TestPrefixMethod2 extends TestPrefixMethod
+{
+
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/services/LocaleAppModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/services/LocaleAppModule.java?rev=584927&r1=584926&r2=584927&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/services/LocaleAppModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app2/services/LocaleAppModule.java Mon Oct 15 14:26:40 2007
@@ -14,7 +14,13 @@
 
 package org.apache.tapestry.integration.app2.services;
 
+import org.apache.tapestry.integration.app2.FortyTwo;
 import org.apache.tapestry.ioc.MappedConfiguration;
+import org.apache.tapestry.ioc.OrderedConfiguration;
+import org.apache.tapestry.model.MutableComponentModel;
+import org.apache.tapestry.services.ClassTransformation;
+import org.apache.tapestry.services.ComponentClassTransformWorker;
+import org.apache.tapestry.services.TransformMethodSignature;
 
 public class LocaleAppModule
 {
@@ -24,4 +30,21 @@
         configuration.add("tapestry.supported-locales", "en,fr,de");
     }
 
+    public static void contributeComponentClassTransformWorker(
+            OrderedConfiguration<ComponentClassTransformWorker> configuration)
+    {
+        configuration.add("FortyTwo", new FortyTwoWorker());
+    }
+    
+    private static final class FortyTwoWorker implements ComponentClassTransformWorker {
+
+        public void transform(ClassTransformation transformation, MutableComponentModel model)
+        {
+            for(TransformMethodSignature sig : transformation.findMethodsWithAnnotation(FortyTwo.class))
+            {
+                transformation.prefixMethod(sig, "return 42;");
+            }
+        }
+        
+    }
 }

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/pagelevel/PrefixMethodTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/pagelevel/PrefixMethodTest.java?rev=584927&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/pagelevel/PrefixMethodTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/pagelevel/PrefixMethodTest.java Mon Oct 15 14:26:40 2007
@@ -0,0 +1,42 @@
+// Copyright 2007 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
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.integration.pagelevel;
+
+import org.apache.tapestry.dom.Document;
+import org.apache.tapestry.integration.app2.services.LocaleAppModule;
+import org.apache.tapestry.test.PageTester;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class PrefixMethodTest extends Assert
+{
+    private PageTester _tester;
+        
+    @Test
+    public void prefix_method() throws Exception
+    {
+        // REFACTOR this happens in a bunch of places
+        String appPackage = "org.apache.tapestry.integration.app2";
+        String appName = "";
+        _tester = new PageTester(appPackage, appName, PageTester.DEFAULT_CONTEXT_PATH, LocaleAppModule.class);
+        Document doc = _tester.renderPage("TestPrefixMethod");
+        
+        assertEquals(doc.getElementById("value").getChildText(), "42");
+
+        // should override the method in the superclass
+        doc = _tester.renderPage("TestPrefixMethod2");
+        assertEquals(doc.getElementById("value").getChildText(), "42");
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/InternalClassTransformationImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/InternalClassTransformationImplTest.java?rev=584927&r1=584926&r2=584927&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/InternalClassTransformationImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/InternalClassTransformationImplTest.java Mon Oct 15 14:26:40 2007
@@ -1039,6 +1039,33 @@
 
         verify();
     }
+    
+    @Test
+    public void prefix_method() throws Exception {
+        Logger logger = mockLogger();
+        TransformMethodSignature sig = new TransformMethodSignature(Modifier.PUBLIC, "int", "getParentField", null, null);
+        
+        replay();
+        
+        InternalClassTransformation ct = createClassTransformation(ParentClass.class, logger);
+        ct.prefixMethod(sig, "return 42;");
+        
+        String desc = ct.toString();
+        assertTrue(desc.contains("prefix"));
+        assertTrue(desc.contains("getParentField"));
+
+        // fail if frozen
+        ct.finish();
+        try
+        {
+            ct.prefixMethod(sig, "return 0;");
+            unreachable();
+        }
+        catch (IllegalStateException e) { }
+        
+        
+        verify();
+    }    
 
     @Test
     public void remove_field() throws Exception

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.tml?rev=584927&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPrefixMethod.tml Mon Oct 15 14:26:40 2007
@@ -0,0 +1,3 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+  <span id="value">${value}</span>
+</html>
\ No newline at end of file