[jboss-cvs] JBossAS SVN: r72906 - in projects/jboss-deployers/trunk: deployers-impl/src/main/org/jboss/deployers/plugins/annotations and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 30 20:15:54 EDT 2008


Author: alesj
Date: 2008-04-30 20:15:54 -0400 (Wed, 30 Apr 2008)
New Revision: 72906

Added:
   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/support/AnnotationsHolder.java
   projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/support/TestAnnotation.java
Modified:
   projects/jboss-deployers/trunk/build/pom.xml
   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/WeakClassLoaderHolder.java
   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/AnnotationsTest.java
Log:
Parameter annotation improvement.

Modified: projects/jboss-deployers/trunk/build/pom.xml
===================================================================
--- projects/jboss-deployers/trunk/build/pom.xml	2008-04-30 23:59:20 UTC (rev 72905)
+++ projects/jboss-deployers/trunk/build/pom.xml	2008-05-01 00:15:54 UTC (rev 72906)
@@ -22,7 +22,7 @@
 
   <properties>
     <version.jboss.man>2.0.0.Beta12</version.jboss.man>
-    <version.jboss.microcontainer>2.0.0.Beta13</version.jboss.microcontainer>
+    <version.jboss.microcontainer>2.0.0-SNAPSHOT</version.jboss.microcontainer>
     <version.jboss.classloader>2.0.0-SNAPSHOT</version.jboss.classloader>
     <version.jboss.common.core>2.2.4.GA</version.jboss.common.core>
     <version.jboss.common.logging.spi>2.0.4.GA</version.jboss.common.logging.spi>

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-04-30 23:59:20 UTC (rev 72905)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultAnnotationEnvironment.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -118,13 +118,13 @@
     *
     * @param type the annotation type
     * @param annClass the annotation class
-    * @param expectedAccessibleObjectClass the ao class
+    * @param aoClass the ao class
     * @return classes
     */
    protected <A extends Annotation, M extends AccessibleObject> Set<Element<A, M>> transformToElements(
          ElementType type,
          Class<A> annClass,
-         Class<M> expectedAccessibleObjectClass
+         Class<M> aoClass
    )
    {
       Set<ClassSignaturePair> pairs = getCSPairs(annClass, type);
@@ -134,29 +134,17 @@
       ClassLoader classLoader = getClassLoader();
       Set<Element<A, M>> elements = new HashSet<Element<A, M>>();
       for (ClassSignaturePair pair : pairs)
-         elements.add(toElement(classLoader, pair, annClass, expectedAccessibleObjectClass));
+      {
+         Element<A, M> element;
+         if (type == ElementType.PARAMETER)
+            element = new ParametersElement<A,M>(classLoader, pair.getClassName(), pair.getSignature(), annClass, aoClass);
+         else
+            element = new DefaultElement<A,M>(classLoader, pair.getClassName(), pair.getSignature(), annClass, aoClass);
+         elements.add(element);
+      }
       return elements;
    }
 
-   /**
-    * Transform cs pair to element.
-    *
-    * @param classLoader the class loader
-    * @param pair the cs pair
-    * @param annClass the annotation class
-    * @param aoClass the ao class
-    * @return element
-    */
-   protected <A extends Annotation, M extends AccessibleObject> Element<A, M> toElement(
-         ClassLoader classLoader,
-         ClassSignaturePair pair,
-         Class<A> annClass,
-         Class<M> aoClass)
-   {
-      return new DefaultElement<A,M>(classLoader, pair.getClassName(), pair.getSignature(), annClass, aoClass);
-   }
-
-
    public Set<Class<?>> classIsAnnotatedWith(Class<? extends Annotation> annotation)
    {
       return transformToClasses(getCSPairs(annotation, ElementType.TYPE));

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-04-30 23:59:20 UTC (rev 72905)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/DefaultElement.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -26,10 +26,6 @@
 import java.lang.reflect.AccessibleObject;
 
 import org.jboss.deployers.spi.annotations.Element;
-import org.jboss.metadata.plugins.loader.reflection.AnnotatedElementMetaDataLoader;
-import org.jboss.metadata.spi.loader.MetaDataLoader;
-import org.jboss.metadata.spi.retrieval.AnnotationItem;
-import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
 import org.jboss.metadata.spi.signature.ConstructorSignature;
 import org.jboss.metadata.spi.signature.FieldSignature;
 import org.jboss.metadata.spi.signature.MethodSignature;
@@ -37,16 +33,16 @@
 import org.jboss.reflect.plugins.introspection.ReflectionUtils;
 
 /**
- * Default annoattions element.
+ * Default annotations element.
  *
  * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
  */
 public class DefaultElement<A extends Annotation, M extends AccessibleObject> extends WeakClassLoaderHolder implements Element<A, M>
 {
-   private String className;
-   private Signature signature;
-   private Class<A> annClass;
-   private Class<M> aoClass;
+   protected String className;
+   protected Signature signature;
+   protected Class<A> annClass;
+   protected Class<M> aoClass;
 
    private SoftReference<Class<?>> classRef;
 
@@ -83,28 +79,10 @@
       return clazz;
    }
 
-   /**
-    * Get meta data retireval for signature on owner.
-    *
-    * @return meta data retrieval
-    */
-   protected MetaDataRetrieval getMetaDataRetrieval()
-   {
-      MetaDataLoader loader = new AnnotatedElementMetaDataLoader(getOwner());
-      MetaDataRetrieval retrieval = loader.getComponentMetaDataRetrieval(signature);
-      if (retrieval == null)
-         throw new IllegalArgumentException("No such signature " + signature + " on loader: " + loader);
-
-      return retrieval;
-   }
-
    public A getAnnotation()
    {
-      AnnotationItem<A> item = getMetaDataRetrieval().retrieveAnnotation(annClass);
-      if (item == null)
-         throw new IllegalArgumentException("Expecting annotation: " + annClass);
-
-      return item.getAnnotation();
+      AccessibleObject accessibleObject = getAccessibleObject();
+      return accessibleObject.getAnnotation(annClass);
    }
 
    public M getAccessibleObject()

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-04-30 23:59:20 UTC (rev 72905)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationDeployer.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -21,9 +21,7 @@
 */
 package org.jboss.deployers.plugins.annotations;
 
-import javassist.ClassPath;
 import javassist.ClassPool;
-import javassist.LoaderClassPath;
 import org.jboss.classloading.spi.dependency.Module;
 import org.jboss.deployers.spi.DeploymentException;
 import org.jboss.deployers.spi.annotations.AnnotationEnvironment;
@@ -58,20 +56,22 @@
    {
       ClassPool pool = ClassPool.getDefault();
       ClassLoader classLoader = unit.getClassLoader();
-      ClassPath classPath = new LoaderClassPath(classLoader);
-      pool.insertClassPath(classPath);
+
+      GenericAnnotationResourceVisitor visitor = new GenericAnnotationResourceVisitor(pool, classLoader);
+      visitor.setForceAnnotations(forceAnnotations);
+
+      ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+      // TODO - any other way?
+      Thread.currentThread().setContextClassLoader(classLoader);
       try
       {
-         GenericAnnotationResourceVisitor visitor = new GenericAnnotationResourceVisitor(pool, classLoader);
-         visitor.setForceAnnotations(forceAnnotations);
-
          module.visit(visitor);
-
-         unit.addAttachment(AnnotationEnvironment.class, visitor.getEnv());
       }
       finally
       {
-         pool.removeClassPath(classPath);
+         Thread.currentThread().setContextClassLoader(tcl);
       }
+
+      unit.addAttachment(AnnotationEnvironment.class, visitor.getEnv());
    }
 }

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-04-30 23:59:20 UTC (rev 72905)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/GenericAnnotationResourceVisitor.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -29,6 +29,8 @@
 import javassist.CtClass;
 import javassist.CtMember;
 import javassist.NotFoundException;
+import javassist.CtConstructor;
+import javassist.CtMethod;
 import org.jboss.classloading.spi.visitor.ClassFilter;
 import org.jboss.classloading.spi.visitor.ResourceContext;
 import org.jboss.classloading.spi.visitor.ResourceFilter;
@@ -36,6 +38,9 @@
 import org.jboss.deployers.spi.annotations.AnnotationEnvironment;
 import org.jboss.logging.Logger;
 import org.jboss.metadata.spi.signature.Signature;
+import org.jboss.metadata.spi.signature.javassist.JavassistSignatureFactory;
+import org.jboss.metadata.spi.signature.javassist.JavassistConstructorParametersSignature;
+import org.jboss.metadata.spi.signature.javassist.JavassistMethodParametersSignature;
 
 /**
  * Generic annotation scanner deployer.
@@ -116,7 +121,7 @@
    protected void handleCtClass(CtClass ctClass, ResourceContext resource) throws ClassNotFoundException, NotFoundException
    {
       Object[] annotations = forceAnnotations ? ctClass.getAnnotations() : ctClass.getAvailableAnnotations();
-      handleAnnotations(ElementType.TYPE, null, annotations, resource);
+      handleAnnotations(ElementType.TYPE, (Signature)null, annotations, resource);
       handleCtMembers(ElementType.CONSTRUCTOR, ctClass.getDeclaredConstructors(), resource);
       handleCtMembers(ElementType.METHOD, ctClass.getDeclaredMethods(), resource);
       handleCtMembers(ElementType.FIELD, ctClass.getDeclaredFields(), resource);
@@ -155,14 +160,41 @@
             {
                CtBehavior behavior = (CtBehavior)member;
                Object[][] paramAnnotations = forceAnnotations ? behavior.getParameterAnnotations() : behavior.getAvailableParameterAnnotations();
-               for (Object[] paramAnn : paramAnnotations)
-                  handleAnnotations(ElementType.PARAMETER, member, paramAnn, resource);
+               for (int index = 0; index < paramAnnotations.length; index++)
+               {
+                  handleAnnotations(ElementType.PARAMETER, getBehaviorSignature(behavior, index), paramAnnotations[index], resource);                  
+               }
             }
          }
       }
    }
 
    /**
+    * Get parameters signature.
+    *
+    * @param behavior the ct behavior
+    * @param index the index
+    * @return parameters signature
+    * @throws ClassNotFoundException for any error
+    */
+   protected Signature getBehaviorSignature(CtBehavior behavior, int index) throws ClassNotFoundException
+   {
+      try
+      {
+         if (behavior instanceof CtConstructor)
+            return new JavassistConstructorParametersSignature((CtConstructor)behavior, index);
+         else if (behavior instanceof CtMethod)
+            return new JavassistMethodParametersSignature((CtMethod)behavior, index);
+         else
+            throw new IllegalArgumentException("Unknown ct behavior: " + behavior);
+      }
+      catch (NotFoundException e)
+      {
+         throw new ClassNotFoundException("Exception creating signature: " + behavior, e);
+      }
+   }
+
+   /**
     * Handle annotations.
     *
     * @param type where we found the annotations
@@ -172,16 +204,27 @@
     */
    protected void handleAnnotations(ElementType type, CtMember member, Object[] annotations, ResourceContext resource)
    {
+      Signature signature = null;
+      if (member != null)
+         signature = JavassistSignatureFactory.getSignature(member);
+      handleAnnotations(type, signature, annotations, resource);
+   }
+
+   /**
+    * Handle annotations.
+    *
+    * @param type where we found the annotations
+    * @param signature the signature
+    * @param annotations the actual annotations
+    * @param resource the resource we're visiting
+    */
+   protected void handleAnnotations(ElementType type, Signature signature, Object[] annotations, ResourceContext resource)
+   {
       if (annotations != null && annotations.length > 0)
       {
          for (Object annObject : annotations)
          {
             Annotation annotation = Annotation.class.cast(annObject);
-            Signature signature = null;
-/*
-            if (member != null)
-               signature = JavassistSignatureFactory.getSignature(member);
-*/
             env.putAnnotation(annotation.annotationType(), type, resource.getClassName(), signature);
          }
       }

Copied: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ParametersElement.java (from rev 72891, 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/ParametersElement.java	                        (rev 0)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/ParametersElement.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -0,0 +1,91 @@
+/*
+* 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.deployers.plugins.annotations;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+import org.jboss.metadata.spi.signature.ConstructorParametersSignature;
+import org.jboss.metadata.spi.signature.MethodParametersSignature;
+import org.jboss.metadata.spi.signature.Signature;
+
+/**
+ * Parameters annotations element.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ParametersElement<A extends Annotation, M extends AccessibleObject> extends DefaultElement<A, M>
+{
+   public ParametersElement(ClassLoader classLoader, String className, Signature signature, Class<A> annClass, Class<M> aoClass)
+   {
+      super(classLoader, className, signature, annClass, aoClass);
+   }
+
+   public A getAnnotation()
+   {
+      Annotation[] annotations = null;
+      Class<?> clazz = getOwner();
+      if (signature instanceof ConstructorParametersSignature)
+      {
+         ConstructorParametersSignature cps = (ConstructorParametersSignature)signature;
+         try
+         {
+            Constructor constructor = clazz.getConstructor(signature.getParametersTypes(clazz));
+            annotations = constructor.getParameterAnnotations()[cps.getParam()];
+         }
+         catch (NoSuchMethodException ignored)
+         {
+         }
+      }
+      else if (signature instanceof MethodParametersSignature)
+      {
+         MethodParametersSignature mps = (MethodParametersSignature)signature;
+         try
+         {
+            Method method = clazz.getMethod(signature.getName(), signature.getParametersTypes(clazz));
+            annotations = method.getParameterAnnotations()[mps.getParam()];
+         }
+         catch (NoSuchMethodException ignored)
+         {
+         }
+      }
+
+      if (annotations == null || annotations.length == 0)
+         throw new IllegalArgumentException("Expected annotations " + className + "." + signature);
+
+      for(Annotation annotation : annotations)
+      {
+         if (annClass.equals(annotation.annotationType()))
+            return annClass.cast(annotation);
+      }
+
+      throw new IllegalArgumentException("No matching annotation: " + Arrays.asList(annotations));
+   }
+
+   public M getAccessibleObject()
+   {
+      throw new UnsupportedOperationException("No such accessible object for parameters.");
+   }
+}
\ No newline at end of file

Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/WeakClassLoaderHolder.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/WeakClassLoaderHolder.java	2008-04-30 23:59:20 UTC (rev 72905)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/annotations/WeakClassLoaderHolder.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -64,7 +64,7 @@
    {
       try
       {
-         return getClassLoader().loadClass(className);
+         return Class.forName(className, false, getClassLoader());
       }
       catch (ClassNotFoundException e)
       {

Added: projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/support/AnnotationsHolder.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/support/AnnotationsHolder.java	                        (rev 0)
+++ projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/support/AnnotationsHolder.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -0,0 +1,43 @@
+/*
+* 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.support;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at TestAnnotation("class")
+public class AnnotationsHolder
+{
+   @TestAnnotation("field")
+   private Object object;
+
+   @TestAnnotation("constructor")
+   public AnnotationsHolder()
+   {
+   }
+
+   @TestAnnotation("method")
+   public void something(@TestAnnotation("parameter") Object object)
+   {
+      System.out.println("object = " + object);
+   }
+}

Added: projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/support/TestAnnotation.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/support/TestAnnotation.java	                        (rev 0)
+++ projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/support/TestAnnotation.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -0,0 +1,37 @@
+/*
+* 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.support;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface TestAnnotation
+{
+   String value() default "";
+}

Modified: 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/AnnotationEnvTestCase.java	2008-04-30 23:59:20 UTC (rev 72905)
+++ projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationEnvTestCase.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -21,7 +21,22 @@
 */
 package org.jboss.test.deployers.annotations.test;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Set;
+
 import junit.framework.Test;
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+import org.jboss.deployers.client.spi.DeployerClient;
+import org.jboss.deployers.client.spi.Deployment;
+import org.jboss.deployers.spi.annotations.AnnotationEnvironment;
+import org.jboss.deployers.spi.annotations.Element;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.test.deployers.annotations.support.AnnotationsHolder;
+import org.jboss.test.deployers.annotations.support.TestAnnotation;
 
 /**
  * AnnotationEnvTestCase.
@@ -40,8 +55,59 @@
       return suite(AnnotationEnvTestCase.class);
    }
 
+   @SuppressWarnings("unchecked")
    public void testSimpleUsage() throws Exception
    {
-      // todo
+      DeployerClient deployer = getMainDeployer();
+
+      Deployment deployment = createSimpleDeployment("a");
+      addClassLoadingMetaData(
+            deployment,
+            deployment.getName(),
+            null,
+            ClassLoaderUtils.classNameToPath("org.jboss.test.deployers.annotations.support.AnnotationsHolder"),
+            ClassLoaderUtils.classNameToPath("org.jboss.test.deployers.annotations.support.TestAnnotation")
+      );
+
+      DeploymentUnit unit = assertDeploy(deployer, deployment);
+      try
+      {
+         ClassLoader cl = unit.getClassLoader();
+         Class<TestAnnotation> taClass = (Class<TestAnnotation>)cl.loadClass("org.jboss.test.deployers.annotations.support.TestAnnotation");
+
+         AnnotationEnvironment env = getAnnotationEnvironment(unit);
+         Set<Class<?>> classes = env.classIsAnnotatedWith(taClass);
+         assertNotNull(classes);
+         assertEquals(AnnotationsHolder.class.getName(), classes.iterator().next().getName());
+
+         Element<TestAnnotation, Constructor> ec = getSingleton(env.classHasConstructorAnnotatedWith(taClass));
+         Annotation ta = ec.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("constructor", getValue(ta));
+
+         Element<TestAnnotation, Field> ef = getSingleton(env.classHasFieldAnnotatedWith(taClass));
+         ta = ef.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("field", getValue(ta));
+
+         Element<TestAnnotation, Method> em = getSingleton(env.classHasMethodAnnotatedWith(taClass));
+         ta = em.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("method", getValue(ta));
+
+         Element<TestAnnotation, AccessibleObject> ep = getSingleton(env.classHasParameterAnnotatedWith(taClass));
+         ta = ep.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("parameter", getValue(ta));
+      }
+      finally
+      {
+         assertUndeploy(deployer, deployment);
+      }
    }
+
+   public void testClassNotLoaded() throws Exception
+   {
+      // TODO
+   }
 }

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-04-30 23:59:20 UTC (rev 72905)
+++ projects/jboss-deployers/trunk/deployers-impl/src/tests/org/jboss/test/deployers/annotations/test/AnnotationsTest.java	2008-05-01 00:15:54 UTC (rev 72906)
@@ -21,6 +21,10 @@
 */
 package org.jboss.test.deployers.annotations.test;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
+import java.util.Set;
+
 import org.jboss.classloader.plugins.system.DefaultClassLoaderSystem;
 import org.jboss.classloader.spi.ClassLoaderSystem;
 import org.jboss.classloader.spi.ParentPolicy;
@@ -32,12 +36,14 @@
 import org.jboss.classloading.spi.metadata.ClassLoadingMetaDataFactory;
 import org.jboss.classloading.spi.version.Version;
 import org.jboss.deployers.client.spi.DeployerClient;
+import org.jboss.deployers.plugins.annotations.GenericAnnotationDeployer;
 import org.jboss.deployers.plugins.classloading.AbstractClassLoaderDescribeDeployer;
-import org.jboss.deployers.plugins.annotations.GenericAnnotationDeployer;
+import org.jboss.deployers.spi.annotations.AnnotationEnvironment;
+import org.jboss.deployers.spi.annotations.Element;
+import org.jboss.deployers.spi.attachments.Attachments;
 import org.jboss.deployers.spi.attachments.MutableAttachments;
 import org.jboss.deployers.spi.attachments.PredeterminedManagedObjectAttachments;
 import org.jboss.deployers.spi.deployer.Deployer;
-import org.jboss.deployers.spi.annotations.AnnotationEnvironment;
 import org.jboss.test.deployers.AbstractDeployerTest;
 import org.jboss.test.deployers.classloading.support.MockClassLoaderDescribeDeployer;
 import org.jboss.test.deployers.classloading.support.MockLevelClassLoaderSystemDeployer;
@@ -57,41 +63,41 @@
       super(name);
    }
 
-   protected static ClassLoadingMetaData addClassLoadingMetaData(PredeterminedManagedObjectAttachments deployment, String name, Version version, Class<?>... packages)
+   protected static ClassLoadingMetaData addClassLoadingMetaData(PredeterminedManagedObjectAttachments deployment, String name, Version version, String... paths)
    {
-      return addClassLoadingMetaData(deployment, name, version, false, packages);
+      return addClassLoadingMetaData(deployment, name, version, false, paths);
    }
 
-   protected static ClassLoadingMetaData addClassLoadingMetaData(PredeterminedManagedObjectAttachments deployment, String name, Version version, boolean useVersionOnPackages, Class<?>... packages)
+   protected static ClassLoadingMetaData addClassLoadingMetaData(PredeterminedManagedObjectAttachments deployment, String name, Version version, boolean useVersionOnPackages, String... paths)
    {
-      ClassLoadingMetaData classLoadingMetaData = createMetaData(deployment, name, version, useVersionOnPackages, packages);
+      ClassLoadingMetaData classLoadingMetaData = createMetaData(name, version, useVersionOnPackages, paths);
       addMetaData(deployment, classLoadingMetaData);
       return classLoadingMetaData;
    }
 
-   protected static ClassLoadingMetaData createMetaData(PredeterminedManagedObjectAttachments deployment, String name, Version version, Class<?>... packages)
+   protected static ClassLoadingMetaData createMetaData(String name, Version version, String... paths)
    {
-      return createMetaData(deployment, name, version, false, packages);
+      return createMetaData(name, version, false, paths);
    }
 
-   protected static ClassLoadingMetaData createMetaData(PredeterminedManagedObjectAttachments deployment, String name, Version version, boolean useVersionOnPackages, Class<?>... packages)
+   protected static ClassLoadingMetaData createMetaData(String name, Version version, boolean useVersionOnPackages, String... paths)
    {
       MockClassLoadingMetaData classLoadingMetaData = new MockClassLoadingMetaData(name, version);
 
-      classLoadingMetaData.setPaths(packages);
+      classLoadingMetaData.setPaths(paths);
 
       CapabilitiesMetaData capabilities = classLoadingMetaData.getCapabilities();
       Capability capability = classLoadingMetaDataFactory.createModule(name, version);
       capabilities.addCapability(capability);
 
-      if (packages != null)
+      if (paths != null)
       {
-         for (Class<?> pkg : packages)
+         for (String path : paths)
          {
             if (useVersionOnPackages)
-               capability = classLoadingMetaDataFactory.createPackage(pkg.getPackage().getName(), version);
+               capability = classLoadingMetaDataFactory.createPackage(path, version);
             else
-               capability = classLoadingMetaDataFactory.createPackage(pkg.getPackage().getName());
+               capability = classLoadingMetaDataFactory.createPackage(path);
             capabilities.addCapability(capability);
          }
       }
@@ -106,13 +112,27 @@
       mutable.addAttachment(ClassLoadingMetaData.class, md);
    }
 
-   protected AnnotationEnvironment getAnnotationEnvironment(PredeterminedManagedObjectAttachments attachments)
+   protected AnnotationEnvironment getAnnotationEnvironment(Attachments attachments)
    {
-      AnnotationEnvironment env = attachments.getPredeterminedManagedObjects().getAttachment(AnnotationEnvironment.class);
+      AnnotationEnvironment env = attachments.getAttachment(AnnotationEnvironment.class);
       assertNotNull(env);
       return env;
    }
 
+   protected <A extends Annotation, M extends AccessibleObject> Element<A,M> getSingleton(Set<Element<A,M>> elements)
+   {
+      assertNotNull(elements);
+      assertEquals(1, elements.size());
+      return elements.iterator().next();
+   }
+
+   protected Object getValue(Annotation annotation) throws Exception
+   {
+      assertNotNull(annotation);
+      Class<? extends Annotation> clazz = annotation.annotationType();
+      return clazz.getMethod("value").invoke(annotation);
+   }
+
    protected DeployerClient getMainDeployer(Deployer... deployers)
    {
       ClassLoading classLoading = new ClassLoading();




More information about the jboss-cvs-commits mailing list