[jboss-cvs] JBossAS SVN: r105879 - projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jun 9 13:25:51 EDT 2010


Author: kabir.khan at jboss.com
Date: 2010-06-09 13:25:50 -0400 (Wed, 09 Jun 2010)
New Revision: 105879

Modified:
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/Util.java
Log:
[JBREFLECT-125] Cache the annotation types by name, along with the default attributes

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/Util.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/Util.java	2010-06-09 16:53:36 UTC (rev 105878)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/Util.java	2010-06-09 17:25:50 UTC (rev 105879)
@@ -24,9 +24,11 @@
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Array;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.WeakHashMap;
 
 import org.jboss.reflect.plugins.bytecode.bytes.BytecodePrimitive;
 import org.jboss.reflect.plugins.bytecode.bytes.ClassBytes;
@@ -49,6 +51,9 @@
 {
    final static Annotation[] NO_ANNOTATIONS = new Annotation[0];
    
+   private final static Map<Class<?>, Map<String, String>> RETURN_TYPES_BY_NAME = Collections.synchronizedMap(new WeakHashMap<Class<?>, Map<String, String>>());
+   private final static Map<Class<?>, Map<String, Object>> DEFAULT_VALUES = Collections.synchronizedMap(new WeakHashMap<Class<?>, Map<String, Object>>()); 
+   
    static String jvmNameToTypeInfoName(String jvmName)
    {
       if (jvmName == null)
@@ -119,8 +124,9 @@
       
       ClassBytes classBytes;
       
-      AllAnnotationAttributesReader allAnnotationAttributesReader = new AllAnnotationAttributesReader();
+      Map<String, String> returnTypesByName;
       
+      
       AnnotationReader(ParentReader parent, ClassLoader loader, String desc)
       {
          this.parent = parent;
@@ -130,7 +136,15 @@
          classBytes = AsmClassBytesFactory.INSTANCE.loadClassBytes(loader, toClassName(desc));
          if (classBytes == null)
             throw new IllegalStateException("Could not load bytes for " + toClassName(desc) + " in " + loader);
-         ((AsmClassBytes)classBytes).getReader().accept(allAnnotationAttributesReader, AsmClassBytes.STANDARD_FLAGS);
+         
+         returnTypesByName = RETURN_TYPES_BY_NAME.get(clazz);
+         if (returnTypesByName == null)
+         {
+            AllAnnotationAttributesReader allAnnotationAttributesReader = new AllAnnotationAttributesReader();
+            ((AsmClassBytes)classBytes).getReader().accept(allAnnotationAttributesReader, AsmClassBytes.STANDARD_FLAGS);
+            returnTypesByName = Collections.unmodifiableMap(allAnnotationAttributesReader.returnTypesByName);
+            RETURN_TYPES_BY_NAME.put(clazz, returnTypesByName);
+         }
       }
 
       public void setValueInParent(String name, Object value)
@@ -150,7 +164,7 @@
    
       public AnnotationVisitor visitArray(String name)
       {
-         return new ArrayReader(this, loader, name, loadClass(loader, allAnnotationAttributesReader.returnTypesByName.get(name).substring(1)));
+         return new ArrayReader(this, loader, name, loadClass(loader, returnTypesByName.get(name).substring(1)));
       }
 
       Object createAnnotation()
@@ -168,18 +182,28 @@
       
       void handleDefaultAttributes()
       {
-         DefaultAnnotationAttributeReader defaults = null;
-         for (String name : allAnnotationAttributesReader.returnTypesByName.keySet())
+         Map<String, Object> defaults = null;
+         for (String name : returnTypesByName.keySet())
          {
             if (!values.containsKey(name))
             {
                if (defaults == null)
                {
-                  defaults = new DefaultAnnotationAttributeReader(loader, allAnnotationAttributesReader);
-                  if (classBytes instanceof AsmClassBytes)
-                     ((AsmClassBytes)classBytes).getReader().accept(defaults, AsmClassBytes.STANDARD_FLAGS | ClassReader.INCLUDE_DEFAULT_ANNOTATION_VALUES);
+                  defaults = DEFAULT_VALUES.get(clazz);
+                  if (defaults == null)
+                  {
+                     if (classBytes instanceof AsmClassBytes)
+                     {
+                        DefaultAnnotationAttributeReader defaultsReader = new DefaultAnnotationAttributeReader(loader, returnTypesByName);
+                        ((AsmClassBytes)classBytes).getReader().accept(defaultsReader, AsmClassBytes.STANDARD_FLAGS | ClassReader.INCLUDE_DEFAULT_ANNOTATION_VALUES);
+                        defaults = defaultsReader.defaultAttributesByName;
+                     }
+                     else
+                        defaults = Collections.<String, Object>emptyMap();
+                  }
+                  DEFAULT_VALUES.put(clazz, Collections.unmodifiableMap(defaults));
                }
-               Object value = defaults.defaultAttributesByName.get(name);
+               Object value = defaults.get(name);
                if (value == null)
                   throw new IllegalStateException("No value and no default for " + name + " in " + clazz.getName());
                values.put(name, value);
@@ -220,12 +244,12 @@
          
          final Map<String, Object> defaultAttributesByName = new HashMap<String, Object>();
          final ClassLoader loader;
-         final AllAnnotationAttributesReader allAnnotationAttributesReader;
+         final Map<String, String> returnTypesByName;
          
-         public DefaultAnnotationAttributeReader(ClassLoader loader, AllAnnotationAttributesReader allAnnotationAttributesReader)
+         public DefaultAnnotationAttributeReader(ClassLoader loader, Map<String, String> returnTypesByName)
          {
             this.loader = loader;
-            this.allAnnotationAttributesReader = allAnnotationAttributesReader;
+            this.returnTypesByName = returnTypesByName;
          }
          
          @Override
@@ -272,7 +296,7 @@
          
             public AnnotationVisitor visitArray(String name)
             {
-               return new ArrayReader(this, loader, this.name, loadClass(loader, allAnnotationAttributesReader.returnTypesByName.get(this.name).substring(1)));
+               return new ArrayReader(this, loader, this.name, loadClass(loader, returnTypesByName.get(this.name).substring(1)));
             }
 
             @SuppressWarnings("unchecked")



More information about the jboss-cvs-commits mailing list