[jboss-cvs] JBossAS SVN: r64431 - in projects/aop/trunk/aop/src: main/org/jboss/aop/proxy/container and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Aug 2 11:54:30 EDT 2007


Author: kabir.khan at jboss.com
Date: 2007-08-02 11:54:29 -0400 (Thu, 02 Aug 2007)
New Revision: 64431

Added:
   projects/aop/trunk/aop/src/main/TestAnnotation.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/AnnotatedPOJO.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/Annotation.java
Modified:
   projects/aop/trunk/aop/src/main/org/jboss/aop/proxy/container/ContainerProxyFactory.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/ProxyTestCase.java
Log:
[JBAOP-450] Added failing fix and test. The failure is likely a javassist issue with copying attributes between different classes

Added: projects/aop/trunk/aop/src/main/TestAnnotation.java
===================================================================
--- projects/aop/trunk/aop/src/main/TestAnnotation.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/main/TestAnnotation.java	2007-08-02 15:54:29 UTC (rev 64431)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestAnnotation<T>
+{
+   T x;
+   
+   void setX(T x)
+   {
+      this.x = x;
+   }
+   
+   T getX()
+   {
+     return x; 
+   }
+}

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/proxy/container/ContainerProxyFactory.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/proxy/container/ContainerProxyFactory.java	2007-08-02 15:19:32 UTC (rev 64430)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/proxy/container/ContainerProxyFactory.java	2007-08-02 15:54:29 UTC (rev 64431)
@@ -42,6 +42,11 @@
 import javassist.Modifier;
 import javassist.NotFoundException;
 import javassist.SerialVersionUID;
+import javassist.bytecode.AnnotationsAttribute;
+import javassist.bytecode.ClassFile;
+import javassist.bytecode.ParameterAnnotationsAttribute;
+import javassist.bytecode.SignatureAttribute;
+import javassist.bytecode.annotation.Annotation;
 
 import org.jboss.aop.Advised;
 import org.jboss.aop.Advisor;
@@ -246,6 +251,9 @@
          addMethodFromTemplate(template, "toString", toStringBody());
       }
       
+      copyAnnotations(superclass, proxy);
+      copySignature(superclass, proxy);
+      
       return proxy;
    }
    
@@ -595,6 +603,8 @@
                CtMethod newMethod = CtNewMethod.make(methods[m].getReturnType(), methods[m].getName(), methods[m].getParameterTypes(), methods[m].getExceptionTypes(), code, proxy);
                newMethod.setModifiers(Modifier.PUBLIC);
                proxy.addMethod(newMethod);
+
+               copySignature(methods[m], newMethod);
             }
 
             proxy.addInterface(intfClass);
@@ -661,6 +671,9 @@
          CtMethod newMethod = CtNewMethod.make(m.getReturnType(), m.getName(), m.getParameterTypes(), m.getExceptionTypes(), code, proxy);
          newMethod.setModifiers(Modifier.PUBLIC);
          proxy.addMethod(newMethod);
+
+         copyAnnotations(m, newMethod);
+         copySignature(m, newMethod);
       }
    }
    
@@ -710,6 +723,8 @@
             CtMethod newMethod = CtNewMethod.make(methods[m].getReturnType(), methods[m].getName(), methods[m].getParameterTypes(), methods[m].getExceptionTypes(), code, proxy);
             newMethod.setModifiers(Modifier.PUBLIC);
             proxy.addMethod(newMethod);
+            
+            copySignature(methods[m], newMethod);
          }
 
          proxy.addInterface(intfClass);
@@ -796,4 +811,85 @@
          }
       }
    }
+   
+   private void copyAnnotations(CtMethod src, CtMethod dest) throws NotFoundException
+   {
+      javassist.bytecode.MethodInfo srcInfo = src.getMethodInfo2();
+      javassist.bytecode.MethodInfo destInfo = dest.getMethodInfo2();
+      copyAnnotations(srcInfo, destInfo, AnnotationsAttribute.invisibleTag);
+      copyAnnotations(srcInfo, destInfo, AnnotationsAttribute.visibleTag);
+
+      int numParams = src.getParameterTypes().length;
+      copyParameterAnnotations(numParams, srcInfo, destInfo, ParameterAnnotationsAttribute.visibleTag);
+      copyParameterAnnotations(numParams, srcInfo, destInfo, ParameterAnnotationsAttribute.invisibleTag);
+
+   }
+   
+   private void copyAnnotations(javassist.bytecode.MethodInfo src, javassist.bytecode.MethodInfo dest, String annotationTag)
+   {
+      AnnotationsAttribute attribute = (AnnotationsAttribute) src.getAttribute(annotationTag);
+      if (attribute != null)
+      {
+         dest.addAttribute(attribute.copy(dest.getConstPool(), new HashMap()));
+      }
+   }
+   
+   private void copyParameterAnnotations(int numParams, javassist.bytecode.MethodInfo src, javassist.bytecode.MethodInfo dest, String paramsTag)
+   {
+      ParameterAnnotationsAttribute params = (ParameterAnnotationsAttribute)src.getAttribute(paramsTag);
+      if (params != null)
+      {
+         dest.addAttribute(params.copy(dest.getConstPool(), new HashMap()));
+         ParameterAnnotationsAttribute srcParams = new ParameterAnnotationsAttribute(src.getConstPool(), paramsTag);
+         Annotation[][] emptyParamAnnotations = new Annotation[numParams][];
+         for (int i = 0 ; i < numParams ; i++)
+         {
+            emptyParamAnnotations[i] = new Annotation[0];
+         }
+         srcParams.setAnnotations(emptyParamAnnotations);
+         src.addAttribute(srcParams);
+      }
+   }
+
+   private void copyAnnotations(CtClass src, CtClass dest) throws NotFoundException
+   {
+      ClassFile srcFile = src.getClassFile2();
+      ClassFile destFile = dest.getClassFile2();
+      copyAnnotations(srcFile, destFile, AnnotationsAttribute.invisibleTag);
+      copyAnnotations(srcFile, destFile, AnnotationsAttribute.visibleTag);
+   }
+   
+   private void copyAnnotations(ClassFile src, ClassFile dest, String annotationTag)
+   {
+      AnnotationsAttribute attribute = (AnnotationsAttribute) src.getAttribute(annotationTag);
+      if (attribute != null)
+      {
+         dest.addAttribute(attribute.copy(dest.getConstPool(), new HashMap()));
+      }
+   }
+   
+   
+   private void copySignature(CtMethod src, CtMethod dest)
+   {
+      javassist.bytecode.MethodInfo srcInfo = src.getMethodInfo2();
+      javassist.bytecode.MethodInfo destInfo = dest.getMethodInfo2();
+      
+      SignatureAttribute sig = (SignatureAttribute)srcInfo.getAttribute(SignatureAttribute.tag);
+      if (sig != null)
+      {
+         destInfo.addAttribute(sig.copy(destInfo.getConstPool(), new HashMap()));
+      }
+   }
+   
+   private void copySignature(CtClass src, CtClass dest)
+   {
+      ClassFile srcFile = src.getClassFile2();
+      ClassFile destFile = dest.getClassFile2();
+      
+      SignatureAttribute sig = (SignatureAttribute)srcFile.getAttribute(SignatureAttribute.tag);
+      if (sig != null)
+      {
+         destFile.addAttribute(sig.copy(destFile.getConstPool(), new HashMap()));
+      }
+   }
 }   

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/AnnotatedPOJO.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/AnnotatedPOJO.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/AnnotatedPOJO.java	2007-08-02 15:54:29 UTC (rev 64431)
@@ -0,0 +1,45 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.proxy;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+ at Annotation
+public class AnnotatedPOJO<T>
+{
+   private T x;
+   
+   @Annotation
+   public void setX(@Annotation T x)
+   {
+      this.x = x;
+   }
+   
+   @Annotation
+   public T getX()
+   {
+      return x;
+   }
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/Annotation.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/Annotation.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/Annotation.java	2007-08-02 15:54:29 UTC (rev 64431)
@@ -0,0 +1,35 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.proxy;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface Annotation {
+
+}

Modified: projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/ProxyTestCase.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/ProxyTestCase.java	2007-08-02 15:19:32 UTC (rev 64430)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/ProxyTestCase.java	2007-08-02 15:54:29 UTC (rev 64431)
@@ -24,6 +24,7 @@
 import java.io.Externalizable;
 import java.io.InputStream;
 import java.io.Serializable;
+import java.lang.reflect.Method;
 import java.rmi.MarshalledObject;
 
 import org.jboss.aop.AspectManager;
@@ -361,4 +362,37 @@
          manager.removeInterceptorFactory("perinstanceaspect");
       }
    }
+   
+   public void testAnnotationsExistInProxy() throws Exception
+   {
+      AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
+      params.setInterfaces(new Class[]{SomeInterface.class});
+      params.setTarget(new AnnotatedPOJO());
+      
+      GeneratedAOPProxyFactory factory = new GeneratedAOPProxyFactory();
+      AnnotatedPOJO pojo = (AnnotatedPOJO)factory.createAdvisedProxy(params);
+      
+      Class proxyClass = pojo.getClass();
+      assertNotSame(AnnotatedPOJO.class, proxyClass);
+      assertEquals(AnnotatedPOJO.class, proxyClass.getSuperclass());
+      
+      checkExpectedAnnotations(AnnotatedPOJO.class);
+      checkExpectedAnnotations(proxyClass);
+   }
+   
+    private void checkExpectedAnnotations(Class clazz) throws Exception
+   {
+      Annotation ann = (Annotation)clazz.getAnnotation(Annotation.class);
+      assertNotNull(ann);
+
+      Method getter = clazz.getMethod("getX");
+      assertNotNull(getter.getAnnotation(Annotation.class));
+      
+      Method setter = clazz.getMethod("setX", Object.class);
+      assertNotNull(setter.getAnnotation(Annotation.class));
+      assertEquals(1, setter.getParameterTypes().length);
+      assertEquals(1, setter.getParameterAnnotations().length);
+      assertEquals(1, setter.getParameterAnnotations()[0].length);
+      assertEquals(Annotation.class, setter.getParameterAnnotations()[0][0].annotationType());
+   }
 }




More information about the jboss-cvs-commits mailing list