[jboss-cvs] JBossAS SVN: r75488 - in projects/jboss-deployers/trunk/deployers-impl/src: tests/org/jboss/test/deployers/annotations and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jul 8 04:48:15 EDT 2008


Author: alesj
Date: 2008-07-08 04:48:15 -0400 (Tue, 08 Jul 2008)
New Revision: 75488

Added:
   projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationEnvKeepTestCase.java
Modified:
   projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/AbstractElement.java
   projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ClassElement.java
   projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ClassSignaturePair.java
   projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultAnnotationEnvironment.java
   projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultElement.java
   projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationDeployer.java
   projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationResourceVisitor.java
   projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ParametersElement.java
   projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/DeployersAnnotationsTestSuite.java
   projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationsTest.java
Log:
[JBDEPLOY-57]; Keep the javassist provided annotation values, optional. By default we don't cache them.

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/AbstractElement.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/AbstractElement.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/AbstractElement.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -36,10 +36,11 @@
 {
    protected String className;
    protected Class<A> annClass;
+   protected A annotation;
 
    private SoftReference<Class<?>> classRef;
 
-   public AbstractElement(ClassLoader classLoader, String className, Class<A> annClass)
+   public AbstractElement(ClassLoader classLoader, String className, Class<A> annClass, A annotation)
    {
       super(classLoader);
 
@@ -50,6 +51,7 @@
 
       this.className = className;
       this.annClass = annClass;
+      this.annotation = annotation;
    }
 
    public String getOwnerClassName()
@@ -73,6 +75,19 @@
 
    public A getAnnotation()
    {
+      if (annotation == null)
+         annotation = readAnnotation();
+
+      return annotation;
+   }
+
+   /**
+    * Read the annotation.
+    *
+    * @return the read annotation
+    */
+   protected A readAnnotation()
+   {
       AnnotatedElement annotatedElement = getAnnotatedElement();
       return annotatedElement.getAnnotation(annClass);
    }

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ClassElement.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ClassElement.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ClassElement.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -31,9 +31,9 @@
  */
 public class ClassElement<A extends Annotation, M extends AnnotatedElement> extends AbstractElement<A, M>
 {
-   public ClassElement(ClassLoader classLoader, String className, Class<A> annClass)
+   public ClassElement(ClassLoader classLoader, String className, Class<A> annClass, A annotation)
    {
-      super(classLoader, className, annClass);
+      super(classLoader, className, annClass, annotation);
    }
 
    @SuppressWarnings("unchecked")

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ClassSignaturePair.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ClassSignaturePair.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ClassSignaturePair.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -21,38 +21,71 @@
 */
 package org.jboss.deployers.plugins.annotations;
 
+import java.lang.annotation.Annotation;
+
 import org.jboss.metadata.spi.signature.Signature;
 import org.jboss.util.JBossObject;
 
 /**
  * Class name and signature pair.
+ * With those two we can re-create annotation value.
  *
+ * If the keepAnnotations flag is on in DefaultAnnotationEnvironment
+ * we cache the annotation value from GenericAnnotationResourceVisitor.
+ *
  * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
  */
 public class ClassSignaturePair extends JBossObject
 {
    private String className;
    private Signature signature;
+   private Annotation annotation;
 
    public ClassSignaturePair(String className, Signature signature)
    {
+      this(className, signature, null);
+   }
+
+   public ClassSignaturePair(String className, Signature signature, Annotation annotation)
+   {
       if (className == null)
          throw new IllegalArgumentException("Null class name");
 
       this.className = className;
       this.signature = signature;
+      this.annotation = annotation;
    }
 
+   /**
+    * Get the classname.
+    *
+    * @return the classname
+    */
    public String getClassName()
    {
       return className;
    }
 
+   /**
+    * Get the signature.
+    *
+    * @return the signature
+    */
    public Signature getSignature()
    {
       return signature;
    }
 
+   /**
+    * Get the annotation.
+    *
+    * @return the annotation
+    */
+   public Annotation getAnnotation()
+   {
+      return annotation;
+   }
+
    protected int getHashCode()
    {
       int hash = className.hashCode();

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultAnnotationEnvironment.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultAnnotationEnvironment.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultAnnotationEnvironment.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -47,11 +47,14 @@
  */
 public class DefaultAnnotationEnvironment extends WeakClassLoaderHolder implements AnnotationEnvironment, Serializable
 {
+   /** The serial version UID */
    private static final long serialVersionUID = 1L;
    /** The log */
    private static final Logger log = Logger.getLogger(DefaultAnnotationEnvironment.class);
    /** The info map */
    private transient Map<Class<? extends Annotation>, Map<ElementType, Set<ClassSignaturePair>>> env;
+   /** Should we keep the annotation */
+   private boolean keepAnnotations;
 
    public DefaultAnnotationEnvironment(ClassLoader classLoader)
    {
@@ -60,6 +63,16 @@
    }
 
    /**
+    * Set the keep annotations flag.
+    *
+    * @param keepAnnotations the keep annotations flag
+    */
+   public void setKeepAnnotations(boolean keepAnnotations)
+   {
+      this.keepAnnotations = keepAnnotations;
+   }
+
+   /**
     * Get env map.
     *
     * @return the env map
@@ -75,13 +88,15 @@
    /**
     * Put the annotation info.
     *
-    * @param annClass the annotation class
+    * @param annotation the annotation
     * @param type the annotation type
     * @param className the class name
     * @param signature the signature
     */
-   void putAnnotation(Class<? extends Annotation> annClass, ElementType type, String className, Signature signature)
+   void putAnnotation(Annotation annotation, ElementType type, String className, Signature signature)
    {
+      Class<? extends Annotation> annClass = annotation.annotationType();
+
       if (log.isTraceEnabled())
          log.trace("Adding annotation @" + annClass.getSimpleName() + " for " + className + " at type " + type + ", signature: " + signature);
 
@@ -93,13 +108,20 @@
          elements = new HashMap<ElementType, Set<ClassSignaturePair>>();
          env.put(annClass, elements);
       }
+
       Set<ClassSignaturePair> classes = elements.get(type);
       if (classes == null)
       {
          classes = CollectionsFactory.createLazySet();
          elements.put(type, classes);
       }
-      classes.add(new ClassSignaturePair(className, signature));
+
+      ClassSignaturePair pair;
+      if (keepAnnotations)
+         pair = new ClassSignaturePair(className, signature, annotation);
+      else
+         pair = new ClassSignaturePair(className, signature);
+      classes.add(pair);
    }
 
    /**
@@ -142,13 +164,16 @@
       Set<Element<A, M>> elements = new HashSet<Element<A, M>>();
       for (ClassSignaturePair pair : pairs)
       {
+         String className = pair.getClassName();
+         A annotation = annClass.cast(pair.getAnnotation());
+
          Element<A, M> element;
          if (type == ElementType.TYPE)
-            element = new ClassElement<A, M>(classLoader, pair.getClassName(), annClass);
+            element = new ClassElement<A, M>(classLoader, className, annClass, annotation);
          else if (type == ElementType.PARAMETER)
-            element = new ParametersElement<A,M>(classLoader, pair.getClassName(), pair.getSignature(), annClass, aoClass);
+            element = new ParametersElement<A,M>(classLoader, className, pair.getSignature(), annClass, annotation, aoClass);
          else
-            element = new DefaultElement<A,M>(classLoader, pair.getClassName(), pair.getSignature(), annClass, aoClass);
+            element = new DefaultElement<A,M>(classLoader, className, pair.getSignature(), annClass, annotation, aoClass);
          elements.add(element);
       }
       return elements;

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultElement.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultElement.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultElement.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -42,9 +42,9 @@
    protected Signature signature;
    protected Class<M> aoClass;
 
-   public DefaultElement(ClassLoader classLoader, String className, Signature signature, Class<A> annClass, Class<M> aoClass)
+   public DefaultElement(ClassLoader classLoader, String className, Signature signature, Class<A> annClass, A annotation, Class<M> aoClass)
    {
-      super(classLoader, className, annClass);
+      super(classLoader, className, annClass, annotation);
 
       if (signature == null)
          throw new IllegalArgumentException("Null signature");

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationDeployer.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationDeployer.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -37,6 +37,7 @@
 public class GenericAnnotationDeployer extends AbstractSimpleRealDeployer<Module>
 {
    private boolean forceAnnotations;
+   private boolean keepAnnotations;
 
    public GenericAnnotationDeployer()
    {
@@ -55,6 +56,16 @@
       this.forceAnnotations = forceAnnotations;
    }
 
+   /**
+    * Set the keep annotations flag.
+    *
+    * @param keepAnnotations the keep annotations flag
+    */
+   public void setKeepAnnotations(boolean keepAnnotations)
+   {
+      this.keepAnnotations = keepAnnotations;
+   }
+
    public void deploy(DeploymentUnit unit, Module module) throws DeploymentException
    {
       if (log.isTraceEnabled())
@@ -65,9 +76,9 @@
 
       GenericAnnotationResourceVisitor visitor = new GenericAnnotationResourceVisitor(pool, classLoader);
       visitor.setForceAnnotations(forceAnnotations);
+      visitor.setKeepAnnotations(keepAnnotations);
 
       ClassLoader tcl = Thread.currentThread().getContextClassLoader();
-      // TODO - any other way?
       Thread.currentThread().setContextClassLoader(classLoader);
       try
       {

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationResourceVisitor.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationResourceVisitor.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationResourceVisitor.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -236,7 +236,7 @@
          for (Object annObject : annotations)
          {
             Annotation annotation = Annotation.class.cast(annObject);
-            env.putAnnotation(annotation.annotationType(), type, resource.getClassName(), signature);
+            env.putAnnotation(annotation, type, resource.getClassName(), signature);
          }
       }
    }
@@ -252,6 +252,16 @@
    }
 
    /**
+    * Set the keep annotations flag.
+    *
+    * @param keepAnnotations the keep annotations flag
+    */
+   public void setKeepAnnotations(boolean keepAnnotations)
+   {
+      env.setKeepAnnotations(keepAnnotations);
+   }
+
+   /**
     * Get the built environment.
     *
     * @return the annoattion environment

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ParametersElement.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ParametersElement.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ParametersElement.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -38,12 +38,12 @@
  */
 public class ParametersElement<A extends Annotation, M extends AnnotatedElement> extends DefaultElement<A, M>
 {
-   public ParametersElement(ClassLoader classLoader, String className, Signature signature, Class<A> annClass, Class<M> aoClass)
+   public ParametersElement(ClassLoader classLoader, String className, Signature signature, Class<A> annClass, A annotation, Class<M> aoClass)
    {
-      super(classLoader, className, signature, annClass, aoClass);
+      super(classLoader, className, signature, annClass, annotation, aoClass);
    }
 
-   public A getAnnotation()
+   protected A readAnnotation()
    {
       Annotation[] annotations = null;
       Class<?> clazz = getOwner();

Modified: projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/DeployersAnnotationsTestSuite.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/DeployersAnnotationsTestSuite.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/DeployersAnnotationsTestSuite.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -24,6 +24,7 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
+import org.jboss.test.deployers.annotations.test.AnnotationEnvKeepTestCase;
 import org.jboss.test.deployers.annotations.test.AnnotationEnvTestCase;
 
 /**
@@ -43,6 +44,7 @@
       TestSuite suite = new TestSuite("Annotations Scanning Tests");
 
       suite.addTest(AnnotationEnvTestCase.suite());
+      suite.addTest(AnnotationEnvKeepTestCase.suite());
 
       return suite;
    }

Copied: projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationEnvKeepTestCase.java (from rev 75446, projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationEnvTestCase.java)
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationEnvKeepTestCase.java	                        (rev 0)
+++ projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationEnvKeepTestCase.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -0,0 +1,52 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.deployers.annotations.test;
+
+import junit.framework.Test;
+import org.jboss.deployers.spi.deployer.Deployer;
+import org.jboss.deployers.plugins.annotations.GenericAnnotationDeployer;
+
+/**
+ * AnnotationEnvKeepTestCase.
+ * In this test we keep the javassist provided annotations.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class AnnotationEnvKeepTestCase extends AnnotationEnvTestCase
+{
+   public AnnotationEnvKeepTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(AnnotationEnvKeepTestCase.class);
+   }
+
+   protected Deployer createGenericAnnotationDeployer()
+   {
+      GenericAnnotationDeployer deployer = new GenericAnnotationDeployer();
+      deployer.setKeepAnnotations(true);
+      return deployer; 
+   }
+}
\ No newline at end of file

Modified: projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationsTest.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationsTest.java	2008-07-08 08:38:48 UTC (rev 75487)
+++ projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationsTest.java	2008-07-08 08:48:15 UTC (rev 75488)
@@ -170,7 +170,7 @@
       deployer2.setClassLoading(classLoading);
       deployer2.setSystem(system);
 
-      Deployer deployer3 = new GenericAnnotationDeployer();
+      Deployer deployer3 = createGenericAnnotationDeployer();
 
       if (deployers != null && deployers.length > 0)
       {
@@ -184,4 +184,9 @@
 
       return createMainDeployer(deployer1, deployer2, deployer3);
    }
+
+   protected Deployer createGenericAnnotationDeployer()
+   {
+      return new GenericAnnotationDeployer();
+   }
 }




More information about the jboss-cvs-commits mailing list