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/06 21:11:12 UTC

svn commit: r1089582 - 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/ test/java/testsubjects/

Author: hlship
Date: Wed Apr  6 19:11:11 2011
New Revision: 1089582

URL: http://svn.apache.org/viewvc?rev=1089582&view=rev
Log:
TAP5-853: Add access to advised method's annotations, return type, and parameter types

Modified:
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/MethodInvocationBundle.java
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java
    tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy
    tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/SingleMethod.java

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java?rev=1089582&r1=1089581&r2=1089582&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java Wed Apr  6 19:11:11 2011
@@ -14,6 +14,9 @@
 
 package org.apache.tapestry5.internal.plastic;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
 import org.apache.tapestry5.plastic.InstanceContext;
 import org.apache.tapestry5.plastic.MethodInvocation;
 
@@ -94,5 +97,30 @@ public abstract class AbstractMethodInvo
         return bundle.methodDescription.argumentTypes.length;
     }
 
+    public <T extends Annotation> boolean hasAnnotation(Class<T> annotationType)
+    {
+        return getAnnotation(annotationType) != null;
+    }
+
+    public <T extends Annotation> T getAnnotation(Class<T> annotationType)
+    {
+        return method().getAnnotation(annotationType);
+    }
+
+    public Method method()
+    {
+        return bundle.getMethod(getInstance());
+    }
+
+    public Class getReturnType()
+    {
+        return method().getReturnType();
+    }
+
+    public Class getParameterType(int index)
+    {
+        return method().getParameterTypes()[index];
+    }
+
     protected abstract void proceedToAdvisedMethod();
 }

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/MethodInvocationBundle.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/MethodInvocationBundle.java?rev=1089582&r1=1089581&r2=1089582&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/MethodInvocationBundle.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/MethodInvocationBundle.java Wed Apr  6 19:11:11 2011
@@ -14,6 +14,8 @@
 
 package org.apache.tapestry5.internal.plastic;
 
+import java.lang.reflect.Method;
+
 import org.apache.tapestry5.plastic.MethodAdvice;
 import org.apache.tapestry5.plastic.MethodDescription;
 
@@ -26,9 +28,41 @@ public class MethodInvocationBundle
 
     public final MethodAdvice[] advice;
 
+    private volatile Method method;
+
     public MethodInvocationBundle(MethodDescription methodDescription, MethodAdvice[] advice)
     {
         this.methodDescription = methodDescription;
         this.advice = advice;
     }
+
+    public Method getMethod(Object instance)
+    {
+        if (method == null)
+            method = findMethod(instance.getClass());
+
+        return method;
+    }
+
+    @SuppressWarnings("unchecked")
+    private Method findMethod(Class clazz)
+    {
+        try
+        {
+            Class[] types = new Class[methodDescription.argumentTypes.length];
+
+            for (int i = 0; i < types.length; i++)
+            {
+                types[i] = PlasticInternalUtils.toClass(clazz.getClassLoader(), methodDescription.argumentTypes[i]);
+            }
+
+            return clazz.getDeclaredMethod(methodDescription.methodName, types);
+        }
+        catch (Exception ex)
+        {
+            throw new RuntimeException(String.format("Unable to locate Method %s: %s", methodDescription,
+                    PlasticInternalUtils.toMessage(ex)), ex);
+        }
+    }
+
 }

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java?rev=1089582&r1=1089581&r2=1089582&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java Wed Apr  6 19:11:11 2011
@@ -16,9 +16,10 @@ package org.apache.tapestry5.plastic;
 
 /**
  * A representation of the invocation of a method that allows the behavior of the method to be advised: either by
- * changing parameter values, or by changing the return value, or by catch or throwing different exeptions.
+ * changing parameter values, or by changing the return value, or by catch or throwing different exceptions. Provides
+ * access to annotations on the advised method.
  */
-public interface MethodInvocation extends MethodInvocationResult
+public interface MethodInvocation extends MethodInvocationResult, AnnotationAccess
 {
     /** The instance on which the method was originally invoked. */
     Object getInstance();
@@ -82,4 +83,10 @@ public interface MethodInvocation extend
 
     /** Returns the number of parameters passed to the advised method. */
     int getParameterCount();
+
+    /** Returns the method's return type. */
+    Class getReturnType();
+
+    /** Returns the type of the indicated parameter. */
+    Class getParameterType(int index);
 }

Modified: tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy?rev=1089582&r1=1089581&r2=1089582&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy (original)
+++ tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy Wed Apr  6 19:11:11 2011
@@ -4,6 +4,9 @@ import java.sql.SQLException
 
 import org.apache.tapestry5.plastic.test.NoopAdvice
 
+import testannotations.Maybe
+import testannotations.Truth
+
 class MethodAdviceTests extends AbstractPlasticSpecification {
     def "advice for a void method"() {
         setup:
@@ -14,13 +17,21 @@ class MethodAdviceTests extends Abstract
 
             findMethod(pc, "aSingleMethod").addAdvice ({
                 didInvoke = true
-                
+
                 assert it.methodName == "aSingleMethod"
                 assert it.parameterCount == 1
-                
+
                 assert it.getParameter(0) == 123
-                
-                it.proceed()                
+
+                assert it.returnType == void.class
+                assert it.getParameterType(0) == int.class
+
+                assert it.hasAnnotation(Deprecated.class) == false
+                assert it.hasAnnotation(Maybe.class) == true
+
+                assert it.getAnnotation(Maybe.class).value() == Truth.YES
+
+                it.proceed()
             } as MethodAdvice)
         } as PlasticClassTransformer)
 
@@ -65,28 +76,27 @@ class MethodAdviceTests extends Abstract
 
         o.dupe(2, "Fam") == "FAM FAM FAM FAM FAM FAM FAM FAM FAM FAM FAM FAM"
     }
-    
+
     def "method that throws exceptions"() {
-        
+
         setup:
-        
+
         def mgr = createMgr({ PlasticClass pc ->
             findMethod(pc, "maybeThrow").addAdvice(new NoopAdvice())
         } as PlasticClassTransformer)
-        
+
         def o = mgr.getClassInstantiator("testsubjects.MethodAdviceTarget").newInstance()
 
         expect:
-        
+
         o.maybeThrow(7) == 7
-        
+
         when:
-        
+
         o.maybeThrow(0)
-        
-        then: 
-        
+
+        then:
+
         thrown(SQLException)
-                
     }
 }

Modified: tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/SingleMethod.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/SingleMethod.java?rev=1089582&r1=1089581&r2=1089582&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/SingleMethod.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/SingleMethod.java Wed Apr  6 19:11:11 2011
@@ -14,8 +14,12 @@
 
 package testsubjects;
 
+import testannotations.Maybe;
+import testannotations.Truth;
+
 public class SingleMethod
 {
+    @Maybe(Truth.YES)
     public void aSingleMethod(int value)
     {
     }