[jboss-cvs] JBossAS SVN: r104994 - in projects/jboss-reflect/trunk/src/main/java/org/jboss: reflect/plugins/javassist and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed May 19 13:12:05 EDT 2010


Author: kabir.khan at jboss.com
Date: 2010-05-19 13:12:04 -0400 (Wed, 19 May 2010)
New Revision: 104994

Modified:
   projects/jboss-reflect/trunk/src/main/java/org/jboss/config/plugins/property/PropertyConfiguration.java
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistAnnotatedParameterInfo.java
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistHelper.java
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistMethodInfo.java
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java
Log:
[JBREFLECT-124] Start avoiding calling CtBehavior.getParameterTypes() too much, as it is an inefficient call which hits the classpools every time

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/config/plugins/property/PropertyConfiguration.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/config/plugins/property/PropertyConfiguration.java	2010-05-19 17:08:03 UTC (rev 104993)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/config/plugins/property/PropertyConfiguration.java	2010-05-19 17:12:04 UTC (rev 104994)
@@ -29,7 +29,6 @@
 import org.jboss.config.plugins.AbstractConfiguration;
 import org.jboss.joinpoint.spi.JoinpointFactoryBuilder;
 import org.jboss.logging.Logger;
-import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
 import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactory;
 import org.jboss.reflect.spi.TypeInfoFactory;
 
@@ -94,7 +93,7 @@
    @Override
    protected TypeInfoFactory createDefaultTypeInfoFactory() throws Throwable
    {
-      TypeInfoFactory factory = (TypeInfoFactory) loadFromProperties(PropertyConfigurationConstants.TYPE_INFO_FACTORY_NAME, IntrospectionTypeInfoFactory.class.getName(), TypeInfoFactory.class);
+      TypeInfoFactory factory = (TypeInfoFactory) loadFromProperties(PropertyConfigurationConstants.TYPE_INFO_FACTORY_NAME, JavassistTypeInfoFactory.class.getName(), TypeInfoFactory.class);
       if (log.isDebugEnabled())
          log.debug("TypeInfoFactory: " + factory);
       return factory;

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistAnnotatedParameterInfo.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistAnnotatedParameterInfo.java	2010-05-19 17:08:03 UTC (rev 104993)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistAnnotatedParameterInfo.java	2010-05-19 17:12:04 UTC (rev 104994)
@@ -28,7 +28,6 @@
 import javassist.CtBehavior;
 import javassist.CtClass;
 import javassist.NotFoundException;
-import javassist.bytecode.SignatureAttribute.ClassSignature;
 import javassist.bytecode.SignatureAttribute.MethodSignature;
 
 import org.jboss.reflect.plugins.AnnotationHelper;
@@ -138,14 +137,15 @@
       
       try
       {
+         CtClass[] ctParameters = ctBehavior.getParameterTypes();
          MethodSignature sig = getMethodSignature();
-         if (sig != null && sig.getParameterTypes().length == ctBehavior.getParameterTypes().length)
+         if (sig != null && sig.getParameterTypes().length == ctParameters.length)
          {
-            parameterTypes = JavassistHelper.createParameterTypes(ctBehavior, sig, typeInfo);
+            parameterTypes = JavassistHelper.createParameterTypes(sig, typeInfo);
          }
          else
          {
-            CtClass[] types = ctBehavior.getParameterTypes();
+            CtClass[] types = ctParameters;
             parameterTypes = new TypeInfo[types.length];
             for (int i = 0; i < types.length; ++i)
                parameterTypes[i] = typeInfo.getFactory().getTypeInfo(types[i]);

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistHelper.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistHelper.java	2010-05-19 17:08:03 UTC (rev 104993)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistHelper.java	2010-05-19 17:12:04 UTC (rev 104994)
@@ -265,12 +265,11 @@
    /**
     * Creates the generic parameter types for a constructor or method
     * 
-    * @param behavior the method or constructor
     * @param sig the method/constructor signature
     * @param typeInfo the javassist type info
     * @return the parameter types 
     */
-   public static TypeInfo[] createParameterTypes(final CtBehavior behavior, final MethodSignature sig, final JavassistClassInfo typeInfo)
+   public static TypeInfo[] createParameterTypes(final MethodSignature sig, final JavassistClassInfo typeInfo)
    {
       SignatureAttribute.Type[] types = sig.getParameterTypes();
       TypeInfo[] parameterTypes = new TypeInfo[types.length];

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistMethodInfo.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistMethodInfo.java	2010-05-19 17:08:03 UTC (rev 104993)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistMethodInfo.java	2010-05-19 17:12:04 UTC (rev 104994)
@@ -46,6 +46,7 @@
  *  {@code MutableMethodInfo}.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
  * @version $Revision$
  * @see MutableMethodInfo
  */

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java	2010-05-19 17:08:03 UTC (rev 104993)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java	2010-05-19 17:12:04 UTC (rev 104994)
@@ -26,6 +26,7 @@
 import java.lang.reflect.Array;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
@@ -34,6 +35,8 @@
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import javassist.CannotCompileException;
+import javassist.ClassPool;
+import javassist.CtBehavior;
 import javassist.CtClass;
 import javassist.CtConstructor;
 import javassist.CtField;
@@ -41,6 +44,7 @@
 import javassist.CtNewConstructor;
 import javassist.CtNewMethod;
 import javassist.NotFoundException;
+import javassist.bytecode.Descriptor;
 import javassist.bytecode.SignatureAttribute.ClassSignature;
 import javassist.bytecode.SignatureAttribute.ClassType;
 
@@ -671,24 +675,18 @@
     */
    protected MutableConstructorInfo generateConstructorInfo(CtConstructor constructor)
    {
-      try
+      //Create the signature from the constructor signature directly so we can lazy load parameters
+      //CtBeahavior.getParameterTypes() does not cache the parameters meaning that it is an expensive
+      //call needing to hit the class pool to load the CtClass every time 
+      String[] params = getParameterTypeStringsForSignature(constructor);
+      
+      SignatureKey key = new SignatureKey(null, params);
+      JavassistConstructorInfo info = new JavassistConstructorInfo(factory, this, constructor);
+      synchronized (constructors)
       {
-         CtClass[] parameterTypes = constructor.getParameterTypes();
-         String[] params = new String[parameterTypes.length];
-         for (int i = 0; i < params.length; ++i)
-            params[i] = getParameterTypeStringForSignature(parameterTypes[i]);
-         SignatureKey key = new SignatureKey(null, params);
-         JavassistConstructorInfo info = new JavassistConstructorInfo(factory, this, constructor);
-         synchronized (constructors)
-         {
-            constructors.put(key, info);
-         }
-         return info;
+         constructors.put(key, info);
       }
-      catch (NotFoundException e)
-      {
-         throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for constructor of " + getName(), e);
-      }
+      return info;
    }
 
    /**
@@ -755,21 +753,13 @@
     */
    protected MutableMethodInfo generateMethodInfo(CtMethod method)
    {
-      try
-      {
-         CtClass[] parameterTypes = method.getParameterTypes();
-         String[] params = new String[parameterTypes.length];
-         for (int i = 0; i < params.length; ++i)
-         {
-            params[i] = getParameterTypeStringForSignature(parameterTypes[i]);
-         }
-         SignatureKey key = new SignatureKey(method.getName(), params);
-         return generateMethodInfo(key, method);
-      }
-      catch (NotFoundException e)
-      {
-         throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for method " + method.getName(), e);
-      }
+      //Create the signature from the constructor signature directly so we can lazy load parameters
+      //CtBeahavior.getParameterTypes() does not cache the parameters meaning that it is an expensive
+      //call needing to hit the class pool to load the CtClass every time 
+      String[] params = getParameterTypeStringsForSignature(method);
+      
+      SignatureKey key = new SignatureKey(method.getName(), params);
+      return generateMethodInfo(key, method);
    }
    
    /**
@@ -1328,36 +1318,132 @@
    }
 
    /**
-    * Constructs the string for a parameter for a signature in the same way as TypeInfo does
+    * Constructs the parameter strings for a behaviour's signature in the same way
+    * as TypeInfo does
     * 
-    * @param clazz
-    * @return the string
+    * @param behaviour
+    * @return the parameter strings
     */
-   protected String getParameterTypeStringForSignature(CtClass clazz) throws NotFoundException
+   protected String[] getParameterTypeStringsForSignature(CtBehavior behavior)
    {
-      if (clazz.isArray())
+      String desc = behavior.getSignature();
+      String[] args = new String[Descriptor.numOfParameters(desc)];
+
+      int n = 0;
+      int i = 1;
+      do
       {
+         i = toCtClass(desc, i, args, n++);
+      }
+      while (i > 0 && n < args.length);
+      return args;
+   }
+
+   /**
+    * Put the next argument into the parameter string array 
+    * 
+    * @param desc the beahviour's descriptor
+    * @param i the current index in the descriptor
+    * @param args the parameter string array
+    * @param n the current index of the arguments array
+    */
+   private static int toCtClass(String desc, int i, String[] args, int n)
+   {
+      int i2;
+      String name;
+
+      int arrayDim = 0;
+      char c = desc.charAt(i);
+      if (c == ')')
+         return ++i;
+      while (c == '[')
+      {
+         ++arrayDim;
+         c = desc.charAt(++i);
+      }
+
+      boolean object = false;
+      if (c == 'L')
+      {
+         i2 = desc.indexOf(';', ++i);
+         name = desc.substring(i, i2++).replace('/', '.');
+         object = true;
+      }
+      else
+      {
+         String type = arrayDim == 0 ? toPrimitiveClass(c) : String.valueOf(c);
+
+         i2 = i + 1;
+         if (arrayDim == 0)
+         {
+            args[n] = type;
+            return i2; // neither an array type or a class type
+         }
+         else
+            name = type;
+      }
+
+      if (arrayDim > 0)
+      {
          StringBuilder sb = new StringBuilder();
-         while (clazz.isArray())
-         {
+         while (arrayDim-- > 0)
             sb.append("[");
-            clazz = clazz.getComponentType();
-         }
-         if (!clazz.isPrimitive())
-         {
+
+         if (object)
             sb.append("L");
-            sb.append(clazz.getName());
+         sb.append(name);
+         if (object)
             sb.append(";");
-         }
-         else
-            sb.append(PrimitiveInfo.getPrimativeArrayType(clazz.getName()));
-         
-         return sb.toString();
+
+         name = sb.toString();
       }
-      else
-         return clazz.getName();
+
+      args[n] = name;
+      return i2;
    }
 
+   /**
+    * Convert a
+    */
+   static String toPrimitiveClass(char c)
+   {
+      String type = null;
+      switch (c)
+      {
+         case 'Z' :
+            type = "boolean";
+            break;
+         case 'C' :
+            type = "char";
+            break;
+         case 'B' :
+            type = "byte";
+            break;
+         case 'S' :
+            type = "short";
+            break;
+         case 'I' :
+            type = "int";
+            break;
+         case 'J' :
+            type = "long";
+            break;
+         case 'F' :
+            type = "float";
+            break;
+         case 'D' :
+            type = "double";
+            break;
+         case 'V' :
+            type = "void";
+            break;
+         default :
+            throw new IllegalArgumentException("Unknown primitive type " + c);
+      }
+
+      return type;
+   }
+
    public String getTypeVariable()
    {
       return null;




More information about the jboss-cvs-commits mailing list