[jboss-cvs] JBossAS SVN: r68920 - in projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop: annotation and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Jan 12 09:02:52 EST 2008


Author: flavia.rainone at jboss.com
Date: 2008-01-12 09:02:52 -0500 (Sat, 12 Jan 2008)
New Revision: 68920

Modified:
   projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/Advisor.java
   projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/annotation/PortableAnnotationElement.java
   projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/metadata/SimpleMetaData.java
Log:
[JBAOP-510] Methods to return metadata have been added to Advisor.

Modified: projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/Advisor.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/Advisor.java	2008-01-12 13:55:40 UTC (rev 68919)
+++ projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/Advisor.java	2008-01-12 14:02:52 UTC (rev 68920)
@@ -23,6 +23,7 @@
 
 import gnu.trove.TLongObjectHashMap;
 
+import java.lang.annotation.Annotation;
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
@@ -31,6 +32,7 @@
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -45,6 +47,7 @@
 import javassist.CtConstructor;
 import javassist.CtField;
 import javassist.CtMethod;
+import javassist.NotFoundException;
 
 import org.jboss.aop.advice.AdviceBinding;
 import org.jboss.aop.advice.AspectDefinition;
@@ -375,6 +378,159 @@
       return hasAnnotation(tgt, null, annotation);
    }
 
+   /**
+    * Returns the tags of all metadata applied to the advised class. Note that
+    * this includes the names of the annotations.
+    * 
+    * @return the metadata tags applied to the advised class
+    */
+   public Collection<String> getMetaDataTags()
+   {
+      ArrayList<String> annotationNames = new ArrayList<String>();
+      
+      annotationNames.addAll(this.getClassMetaData().getTags());
+      annotationNames.addAll(annotations.getClassAnnotations().keySet());
+      
+      if (metadata != null)
+      {
+         for (Annotation annotation: metadata.getAnnotations())
+         {
+            annotationNames.add(annotation.getClass().getName());
+         }
+      }
+      else
+      {
+         try
+         {
+            AnnotationElement.loadAllAnnotationsPresent(this.clazz, annotationNames);
+         }
+         catch (NotFoundException e)
+         {
+            throw new RuntimeException("Unexpected error: advised class not found", e);
+         }
+      }
+      
+      return annotationNames;
+   }
+   
+   /**
+    * Returns the tags of all metadatas applied to {@code field}. Note that
+    * this includes all the annotation names.
+    * 
+    * @param field the advised field
+    * @return the metadata tags applied to the advised field
+    */
+   public Collection<String> getMetaDataTags(Field field)
+   {
+      Collection<String> annotationNames = new ArrayList<String>();
+      SimpleMetaData fieldMetaData = this.getFieldMetaData().getFieldMetaData(field.getName());
+      if (fieldMetaData != null)
+      {
+         annotationNames.addAll(fieldMetaData.getTags());
+      }
+      annotationNames.addAll(this.getDefaultMetaData().getTags());
+      annotationNames.addAll(((Map) annotations.getAnnotations().get(field)).keySet());
+      if (metadata != null)
+      {
+         MetaData componentMetaData = this.metadata.getComponentMetaData(new FieldSignature(field));
+         for (Annotation annotation: componentMetaData.getAnnotations())
+         {
+            annotationNames.add(annotation.getClass().getName());
+         }
+      }
+      else
+      {
+         try
+         {
+            AnnotationElement.loadAllAnnotationsPresent(field, annotationNames);
+         }
+         catch (NotFoundException e)
+         {
+            throw new RuntimeException("Unexpected error: advised class not found", e);
+         }
+      }
+      return annotationNames;
+   }
+   
+   /**
+    * Returns the tags of all metadatas applied to {@code constructor}. Note that
+    * this includes all the annotation names.
+    * 
+    * @param constructor the advised constructor
+    * @return the metadata tags applied to the advised constructor
+    */
+   public Collection<String> getMetaDataTags(Constructor constructor)
+   {
+      ArrayList<String> annotationNames = new ArrayList<String>();
+      if (this.getConstructorMetaData().getConstructorMetaData(constructor) != null)
+      {
+         annotationNames.addAll(this.getConstructorMetaData().
+               getConstructorMetaData(constructor).getTags());
+      }
+      annotationNames.addAll(this.getDefaultMetaData().getTags());
+      annotationNames.addAll(((Map) annotations.getAnnotations().get(constructor)).keySet());      
+      
+      if (metadata != null)
+      {
+         MetaData constructorMetaData = this.metadata.getComponentMetaData(
+               new ConstructorSignature(constructor));
+         for (Annotation annotation: constructorMetaData.getAnnotations())
+         {
+            annotationNames.add(annotation.getClass().getName());
+         }
+      }
+      else
+      {
+         try
+         {
+            AnnotationElement.loadAllAnnotationsPresent(constructor, annotationNames);
+         }
+         catch (NotFoundException e)
+         {
+            throw new RuntimeException("Unexpected error: advised class not found", e);
+         }
+      }
+      return annotationNames;
+   }
+   
+   /**
+    * Returns the metadata tags applied to {@code method}. Notice that this includes
+    * all annotation names.
+    * 
+    * @param method the advised method
+    * @return the metadata tags applied to the advised method
+    */
+   public Collection<String> getMetaDataTags(Method method)
+   {
+      ArrayList<String> annotationNames = new ArrayList<String>();
+      if (this.getMethodMetaData().getMethodMetaData(method) != null)
+      {
+         annotationNames.addAll(this.getMethodMetaData().getMethodMetaData(method).getTags());
+      }
+      annotationNames.addAll(this.getDefaultMetaData().getTags());
+      annotationNames.addAll(((Map) annotations.getAnnotations().get(method)).keySet());
+      if (metadata != null)
+      {
+         MetaData methodMetaData = this.metadata.getComponentMetaData(new MethodSignature(method));
+         for (Annotation annotation: methodMetaData.getAnnotations())
+         {
+            annotationNames.add(annotation.getClass().getName());
+         }
+      }
+      else
+      {
+         try
+         {
+            AnnotationElement.loadAllAnnotationsPresent(method, annotationNames);
+         }
+         catch (NotFoundException e)
+         {
+            throw new RuntimeException("Unexpected error: advised class not found", e);
+         }
+      }
+      return annotationNames;
+   }
+   
    private boolean hasAnnotation(Class tgt, String annotation, Class annotationClass)
    {
       if (annotation == null && annotationClass == null)

Modified: projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/annotation/PortableAnnotationElement.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/annotation/PortableAnnotationElement.java	2008-01-12 13:55:40 UTC (rev 68919)
+++ projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/annotation/PortableAnnotationElement.java	2008-01-12 14:02:52 UTC (rev 68920)
@@ -21,6 +21,12 @@
   */
 package org.jboss.aop.annotation;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Collection;
+
 import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtConstructor;
@@ -31,16 +37,12 @@
 import javassist.bytecode.ClassFile;
 import javassist.bytecode.FieldInfo;
 import javassist.bytecode.MethodInfo;
+import javassist.bytecode.annotation.Annotation;
 
+import org.jboss.annotation.factory.javassist.AnnotationProxy;
 import org.jboss.aop.AspectManager;
-import org.jboss.annotation.factory.javassist.AnnotationProxy;
 import org.jboss.aop.util.ReflectToJavassist;
 
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-
 /**
  * This base class is portable between JDK 1.4 and JDK 1.5
  * AnnotationElement will be different for JDK 1.4 and JDK 1.5
@@ -77,6 +79,33 @@
       return visible.getAnnotation(annotation) != null;
    }
 
+   public static void loadAllAnnotationsPresent(Field field,
+         Collection<String> annotationNames) throws NotFoundException
+   {
+      CtField ctField = ReflectToJavassist.fieldToJavassist(field);
+      if (ctField == null)
+      {
+         return;
+      }
+      FieldInfo fi = ctField.getFieldInfo2();
+      AnnotationsAttribute visible = (AnnotationsAttribute) fi.getAttribute(AnnotationsAttribute.visibleTag);
+      if (visible != null)
+      {
+         for (Annotation annotation: visible.getAnnotations())
+         {
+            annotationNames.add(annotation.getTypeName());
+         }
+      }
+      AnnotationsAttribute invisible = (AnnotationsAttribute) fi.getAttribute(AnnotationsAttribute.invisibleTag);
+      if (invisible != null)
+      {
+         for (Annotation annotation: visible.getAnnotations())
+         {
+            annotationNames.add(annotation.getTypeName());
+         }
+      }
+   }
+   
    public static boolean isAnyAnnotationPresent(CtField ctField, String annotation)
    {
       FieldInfo mi = ctField.getFieldInfo2();
@@ -116,6 +145,33 @@
       return AnnotationElement.isAnyAnnotationPresent(ctField, annotation);
 
    }
+   
+   public static void loadAllAnnotationsPresent(Method method,
+         Collection<String> annotationNames) throws NotFoundException
+   {
+      CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method);
+      if (ctMethod == null)
+      {
+         return;
+      }
+      MethodInfo mi = ctMethod.getMethodInfo2();
+      AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
+      if (visible != null)
+      {
+         for (Annotation annotation: visible.getAnnotations())
+         {
+            annotationNames.add(annotation.getTypeName());
+         }
+      }
+      AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
+      if (invisible != null)
+      {
+         for (Annotation annotation: visible.getAnnotations())
+         {
+            annotationNames.add(annotation.getTypeName());
+         }
+      }
+   }
 
    public static boolean isAnyAnnotationPresent(Method method, String annotation) throws Exception
    {
@@ -174,6 +230,33 @@
 
       return visible.getAnnotation(annotation) != null;
    }
+   
+   public static void loadAllAnnotationsPresent(Constructor constructor,
+         Collection<String> annotationNames) throws NotFoundException
+   {
+      CtConstructor ctConstructor = ReflectToJavassist.constructorToJavassist(constructor);
+      if (ctConstructor == null)
+      {
+         return;
+      }
+      MethodInfo ci = ctConstructor.getMethodInfo2();
+      AnnotationsAttribute visible = (AnnotationsAttribute) ci.getAttribute(AnnotationsAttribute.visibleTag);
+      if (visible != null)
+      {
+         for (Annotation annotation: visible.getAnnotations())
+         {
+            annotationNames.add(annotation.getTypeName());
+         }
+      }
+      AnnotationsAttribute invisible = (AnnotationsAttribute) ci.getAttribute(AnnotationsAttribute.invisibleTag);
+      if (invisible != null)
+      {
+         for (Annotation annotation: visible.getAnnotations())
+         {
+            annotationNames.add(annotation.getTypeName());
+         }
+      }
+   }
 
    public static boolean isAnyAnnotationPresent(Constructor con, String annotation) throws Exception
    {
@@ -266,6 +349,32 @@
 
       return false;
    }
+   
+   public static void loadAllAnnotationsPresent(Class clazz,
+            Collection<String> annotationNames) throws NotFoundException
+   {
+      if (clazz == Void.TYPE)
+      {
+         return;
+      }
+      ClassFile cf = AnnotationElement.getClassFile(clazz);
+      AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
+      if (visible != null)
+      {
+         for (Annotation annotation: visible.getAnnotations())
+         {
+            annotationNames.add(annotation.getTypeName());
+         }
+      }
+      AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
+      if (invisible != null)
+      {
+         for (Annotation annotation: visible.getAnnotations())
+         {
+            annotationNames.add(annotation.getTypeName());
+         }
+      }
+   }
 
    protected static ClassFile getClassFile(Class clazz) throws NotFoundException
    {

Modified: projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/metadata/SimpleMetaData.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/metadata/SimpleMetaData.java	2008-01-12 13:55:40 UTC (rev 68919)
+++ projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/metadata/SimpleMetaData.java	2008-01-12 14:02:52 UTC (rev 68920)
@@ -24,6 +24,8 @@
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -149,6 +151,25 @@
    {
       return metaData.get(name) != null;
    }
+   
+   /**
+    * Returns all the tag names contained by this instance.
+    *  
+    * @return a collection containing all {@code String} typed tags added to this
+    *         meta data instance.
+    */
+   public synchronized Collection<String> getTags()
+   {
+      Collection<String> tagStrings = new ArrayList<String>(metaData.size());
+      for(Object tag: metaData.keySet())
+      {
+         if (tag instanceof String)
+         {
+            tagStrings.add((String) tag);
+         }
+      }
+      return tagStrings;
+   }
 
    /**
     * Tags metadata to structure.  Use for tags with no attributes (i.e.




More information about the jboss-cvs-commits mailing list