You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2006/09/07 01:40:59 UTC

svn commit: r440901 - in /tapestry/tapestry5/tapestry-core/trunk/src: main/java/org/apache/tapestry/internal/services/ main/java/org/apache/tapestry/util/ test/java/org/apache/tapestry/util/

Author: hlship
Date: Wed Sep  6 16:40:59 2006
New Revision: 440901

URL: http://svn.apache.org/viewvc?view=rev&rev=440901
Log:
Factor out the logic that builds the invocation of a method (as part of a ClassTransformation).

Added:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/MethodInvocationBuilder.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/ParameterBuilder.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/StringParameterBuilder.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/MethodInvocationBuilderTest.java
Modified:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ComponentLifecycleMethodWorker.java

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ComponentLifecycleMethodWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ComponentLifecycleMethodWorker.java?view=diff&rev=440901&r1=440900&r2=440901
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ComponentLifecycleMethodWorker.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ComponentLifecycleMethodWorker.java Wed Sep  6 16:40:59 2006
@@ -16,7 +16,6 @@
 
 import java.lang.annotation.Annotation;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.tapestry.MarkupWriter;
 import org.apache.tapestry.model.MutableComponentModel;
@@ -24,8 +23,7 @@
 import org.apache.tapestry.services.ComponentClassTransformWorker;
 import org.apache.tapestry.services.MethodSignature;
 import org.apache.tapestry.util.BodyBuilder;
-
-import static org.apache.tapestry.util.CollectionFactory.newMap;
+import org.apache.tapestry.util.MethodInvocationBuilder;
 
 /**
  * Converts one of the methods of {@link org.apache.tapestry.runtime.ComponentLifecycle} into a
@@ -41,12 +39,10 @@
 
     private final MethodSignature _lifecycleMethodSignature;
 
-    // This stuff should be factored out into its own utility
-    // class.
+    private final MethodInvocationBuilder _invocationBuilder = new MethodInvocationBuilder();
 
-    private final Map<String, String> _parameterTypeToCode = newMap();
     {
-        _parameterTypeToCode.put(MarkupWriter.class.getName(), "$1");
+        _invocationBuilder.addParameter(MarkupWriter.class.getName(), "$1");
     }
 
     public ComponentLifecycleMethodWorker(MethodSignature lifecycleMethodSignature,
@@ -78,14 +74,15 @@
         builder.addln(CHECK_ABORT_FLAG);
 
         for (MethodSignature sig : methods)
-            addMethodCallToBody(builder, sig);
+            addMethodCallToBody(builder, sig, transformation);
 
         builder.end();
 
         transformation.extendMethod(_lifecycleMethodSignature, builder.toString());
     }
 
-    private void addMethodCallToBody(BodyBuilder builder, MethodSignature sig)
+    private void addMethodCallToBody(BodyBuilder builder, MethodSignature sig,
+            ClassTransformation transformation)
     {
         boolean isVoid = sig.getReturnType().equals("void");
 
@@ -96,26 +93,7 @@
         // like how javac enables access to private members for inner classes (by introducing
         // synthetic, static methods).
 
-        builder.add("%s(", sig.getMethodName());
-
-        // TODO: And if the method throws any exceptions?
-
-        boolean comma = false;
-
-        for (String type : sig.getParameterTypes())
-        {
-            String code = _parameterTypeToCode.get(type);
-
-            // TODO: If null?
-
-            if (comma)
-                builder.add(", ");
-
-            builder.add(code);
-            comma = true;
-        }
-
-        builder.add(")");
+        builder.add(_invocationBuilder.buildMethodInvocation(sig, transformation));
 
         // Now, if non void ...
 

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/MethodInvocationBuilder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/MethodInvocationBuilder.java?view=auto&rev=440901
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/MethodInvocationBuilder.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/MethodInvocationBuilder.java Wed Sep  6 16:40:59 2006
@@ -0,0 +1,99 @@
+// Copyright 2006 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.util;
+
+import java.util.Map;
+
+import org.apache.tapestry.services.ClassTransformation;
+import org.apache.tapestry.services.MethodSignature;
+import org.apache.tapestry.services.TransformUtils;
+
+/**
+ * A utility class for building part of a method body to invoke a method. Analyzes the method and
+ * matches parameter types to ParameterBuilders.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public final class MethodInvocationBuilder
+{
+    private final Map<String, ParameterBuilder> _builders = CollectionFactory.newMap();
+
+    /**
+     * Maps a parameter type to a {@link ParameterBuilder}.
+     */
+    public void addParameter(String parameterType, ParameterBuilder builder)
+    {
+        // TODO: Name conflicts
+
+        _builders.put(parameterType, builder);
+    }
+
+    /**
+     * Maps a parameter type to a literal string to be used for the parameter expression.
+     * 
+     * @see StringParameterBuilder
+     */
+    public void addParameter(String parameterType, String expression)
+    {
+        addParameter(parameterType, new StringParameterBuilder(expression));
+    }
+
+    /**
+     * Builds the method invocation. Analyzes the type of each parameter to the method, and uses a
+     * {@link ParameterBuilder} to provide the expression. Supplies a default value (usually null)
+     * for any parameters that do not have parameter builders.
+     * 
+     * @param signature
+     *            of the method to invoke
+     * @param transformation
+     * @return method invocation expression
+     * @see TransformUtils#getDefaultValue(String)
+     */
+    public String buildMethodInvocation(MethodSignature signature,
+            ClassTransformation transformation)
+    {
+        StringBuilder builder = new StringBuilder(signature.getMethodName());
+
+        builder.append("(");
+
+        String[] parameterTypes = signature.getParameterTypes();
+
+        for (int i = 0; i < parameterTypes.length; i++)
+        {
+            if (i > 0)
+                builder.append(", ");
+
+            String type = parameterTypes[i];
+
+            ParameterBuilder parameterBuilder = _builders.get(type);
+
+            if (parameterBuilder == null)
+            {
+                // TODO: Log an error
+
+                builder.append(TransformUtils.getDefaultValue(type));
+            }
+            else
+            {
+                builder.append(parameterBuilder.buildParameter(transformation));
+            }
+        }
+
+        builder.append(")");
+
+        return builder.toString();
+    }
+
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/ParameterBuilder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/ParameterBuilder.java?view=auto&rev=440901
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/ParameterBuilder.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/ParameterBuilder.java Wed Sep  6 16:40:59 2006
@@ -0,0 +1,32 @@
+// Copyright 2006 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.util;
+
+import org.apache.tapestry.services.ClassTransformation;
+
+/**
+ * Builds single parameter value to pass into a method being invoked by a
+ * {@link org.apache.tapestry.util.MethodInvocationBuilder}.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public interface ParameterBuilder
+{
+    /**
+     * @param transformation
+     * @return the expression for the parameter
+     */
+    String buildParameter(ClassTransformation transformation);
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/StringParameterBuilder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/StringParameterBuilder.java?view=auto&rev=440901
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/StringParameterBuilder.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/util/StringParameterBuilder.java Wed Sep  6 16:40:59 2006
@@ -0,0 +1,46 @@
+// Copyright 2006 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.util;
+
+import org.apache.tapestry.services.ClassTransformation;
+
+import static java.lang.String.format;
+
+/**
+ * Implementation of {@link org.apache.tapestry.util.ParameterBuilder} that simply provides a static
+ * string value for the parameter expression.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public final class StringParameterBuilder implements ParameterBuilder
+{
+    private final String _expression;
+
+    public StringParameterBuilder(String expression)
+    {
+        _expression = expression;
+    }
+
+    public String buildParameter(ClassTransformation transformation)
+    {
+        return _expression;
+    }
+
+    @Override
+    public String toString()
+    {
+        return format("StringParameterBuilder[%s]", _expression);
+    }
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/MethodInvocationBuilderTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/MethodInvocationBuilderTest.java?view=auto&rev=440901
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/MethodInvocationBuilderTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/util/MethodInvocationBuilderTest.java Wed Sep  6 16:40:59 2006
@@ -0,0 +1,89 @@
+// Copyright 2006 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.util;
+
+import java.lang.reflect.Modifier;
+
+import org.apache.tapestry.MarkupWriter;
+import org.apache.tapestry.services.ClassTransformation;
+import org.apache.tapestry.services.MethodSignature;
+import org.apache.tapestry.test.BaseTestCase;
+import org.testng.annotations.Test;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class MethodInvocationBuilderTest extends BaseTestCase
+{
+    private static final String LOCALE_CLASS_NAME = "java.util.Locale";
+
+    private static final String MARKUP_WRITER_CLASS_NAME = MarkupWriter.class.getName();
+
+    @Test
+    public void known_parameter_type()
+    {
+        ClassTransformation transformation = newClassTransformation();
+
+        replay();
+
+        MethodSignature sig = new MethodSignature(Modifier.PUBLIC, "void", "myMethod", new String[]
+        { MARKUP_WRITER_CLASS_NAME }, null);
+
+        MethodInvocationBuilder invoker = new MethodInvocationBuilder();
+
+        invoker.addParameter(MARKUP_WRITER_CLASS_NAME, "$1");
+
+        assertEquals(invoker.buildMethodInvocation(sig, transformation), "myMethod($1)");
+
+        verify();
+    }
+
+    @Test
+    public void unknown_parameter_type()
+    {
+        ClassTransformation transformation = newClassTransformation();
+
+        replay();
+
+        MethodSignature sig = new MethodSignature(Modifier.PUBLIC, "void", "myMethod", new String[]
+        { MARKUP_WRITER_CLASS_NAME }, null);
+
+        MethodInvocationBuilder invoker = new MethodInvocationBuilder();
+
+        assertEquals(invoker.buildMethodInvocation(sig, transformation), "myMethod(null)");
+
+        verify();
+    }
+
+    @Test
+    public void multiple_parameters_for_method()
+    {
+        ClassTransformation transformation = newClassTransformation();
+
+        replay();
+
+        MethodSignature sig = new MethodSignature(Modifier.PUBLIC, "void", "myMethod", new String[]
+        { MARKUP_WRITER_CLASS_NAME, LOCALE_CLASS_NAME }, null);
+
+        MethodInvocationBuilder invoker = new MethodInvocationBuilder();
+
+        invoker.addParameter(MARKUP_WRITER_CLASS_NAME, "$1");
+        invoker.addParameter(LOCALE_CLASS_NAME, "$2");
+
+        assertEquals(invoker.buildMethodInvocation(sig, transformation), "myMethod($1, $2)");
+
+        verify();
+    }
+}