[jboss-cvs] JBossAS SVN: r104107 - in projects/jboss-reflect/trunk/src: test/java/org/jboss/test/classinfo/test and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Apr 21 07:13:39 EDT 2010
Author: kabir.khan at jboss.com
Date: 2010-04-21 07:13:38 -0400 (Wed, 21 Apr 2010)
New Revision: 104107
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/JavassistTypeInfoFactoryImpl.java
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistUtil.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoProxyTest.java
Log:
[JBREFLECT-119] Tidy up how the ClassInfos are loaded, e.g. we don't need to load up CtClass every time we call TypeInfoFactory.get*()
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-04-21 08:23:38 UTC (rev 104106)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java 2010-04-21 11:13:38 UTC (rev 104107)
@@ -886,10 +886,17 @@
{
ClassLoader cl = ctClass.getClassPool().getClassLoader();
+ String name = packageName + ".package-info";
@SuppressWarnings("static-access")
- CtClass clazz = factory.getPoolFactory().getPoolForLoader(cl).get(packageName + ".package-info");
- ClassInfo info = (ClassInfo)factory.get(clazz, null, cl);
- annotations = info.getAnnotations();
+ CtClass clazz = factory.getPoolFactory().getPoolForLoader(cl).get(name);
+ try
+ {
+ ClassInfo info = (ClassInfo)factory.get(clazz, null, name, cl, false);
+ annotations = info.getAnnotations();
+ }
+ catch (ClassNotFoundException ignoree)
+ {
+ }
}
catch (NotFoundException e)
{
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java 2010-04-21 08:23:38 UTC (rev 104106)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java 2010-04-21 11:13:38 UTC (rev 104107)
@@ -37,6 +37,7 @@
import javassist.CtMethod;
import javassist.CtPrimitiveType;
import javassist.NotFoundException;
+import javassist.bytecode.Descriptor;
import javassist.bytecode.SignatureAttribute;
import javassist.bytecode.SignatureAttribute.ArrayType;
import javassist.bytecode.SignatureAttribute.BaseType;
@@ -250,202 +251,6 @@
}
/**
- * Get the information for a class
- *
- * @param name the name
- * @param cl the classloader
- * @return the info
- * @throws ClassNotFoundException when the class cannot be found
- */
- @Override
- public TypeInfo get(String name, ClassLoader cl) throws ClassNotFoundException
- {
- return get(name, cl, null);
- }
-
- /**
- * Get the information for a class
- *
- * @param name the name
- * @param cl the classloader
- * @param clazz the class
- * @return the info
- * @throws ClassNotFoundException when the class cannot be found
- */
- protected TypeInfo get(String name, ClassLoader cl, Class<?> clazz) throws ClassNotFoundException
- {
- if (name == null)
- throw new IllegalArgumentException("Null name");
- if (cl == null)
- throw new IllegalArgumentException("Null classloader");
-
- try
- {
- CtClass ctClass = poolFactory.getPoolForLoader(cl).get(name);
- return get(ctClass, clazz, cl);
- }
- catch(NotFoundException nfe)
- {
- return delegateToIntrospectionImplementation(cl, name);
- }
- }
-
- /**
- * Proxies, whether
- * <ul>
- * <li>JDK dynamic proxies</li>
- * <li>javassist ProxyFactory proxies - created by calling ClassLoader.defineClass()</li>
- * </ul>
- * are not visible to the javassist classpools, and neither will classes generated by cglib or other
- * frameworks, so try to load up the class from the reflect implementation.
- *
- * @param cl the classloader
- * @param name the name of the class
- * @return the info
- * @throws ClassNotFoundException when the class cannot be found
- */
- private TypeInfo delegateToIntrospectionImplementation(ClassLoader cl, String name) throws ClassNotFoundException
- {
- //Check the class has been loaded
- try
- {
- cl.loadClass(name);
- IntrospectionTypeInfoFactory factory = new IntrospectionTypeInfoFactory();
- return factory.getTypeInfo(name, cl);
- }
- catch (ClassNotFoundException e)
- {
- throw e;
- }
- }
-
- /**
- * Get the information for a class
- *
- *
- * @param clazz the class
- * @return the info
- */
- @SuppressWarnings("unchecked")
- @Override
- public TypeInfo get(Class clazz)
- {
- try
- {
- ClassLoader cl = SecurityActions.getClassLoader(clazz);
- if(cl == null)
- cl = SecurityActions.getContextClassLoader();
- return get(clazz.getName(), cl, clazz);
- }
- catch (ClassNotFoundException e)
- {
- throw new RuntimeException("Class not found: "+e.getMessage());
- }
- }
-
-
- /**
- * Get the information for a class
- *
- * @param ctClass the class
- * @param clazz the class
- * @return the info
- */
- protected TypeInfo get(CtClass ctClass, Class<?> clazz)
- {
- if (ctClass == null)
- throw new IllegalArgumentException("Null class");
-
- if (clazz != null && clazz.getClassLoader() != null)
- return get(ctClass, clazz, clazz.getClassLoader());
- else
- return get(ctClass, clazz, ctClass.getClassPool().getClassLoader());
- }
-
- /**
- * Get the information for a class
- *
- * @param ctClass the class
- * @param clazz the class
- * @param cl the class loader
- * @return the info
- */
- protected TypeInfo get(CtClass ctClass, Class<?> clazz, ClassLoader cl)
- {
- if (ctClass == null)
- throw new IllegalArgumentException("Null class");
-
- TypeInfo primitive = PrimitiveInfo.valueOf(ctClass.getName());
- if (primitive != null)
- return primitive;
-
- NumberInfo number = NumberInfo.valueOf(ctClass.getName());
- if (number != null)
- {
- synchronized (number)
- {
- if (number.getPhase() != NumberInfo.Phase.INITIALIZING)
- {
- if (number.getPhase() != NumberInfo.Phase.COMPLETE)
- {
- number.initializing();
- Class<?> useClass = clazz;
- try
- {
- if (useClass == null)
- useClass = ctClass.getClassPool().getClassLoader().loadClass(ctClass.getName());
- }
- catch (ClassNotFoundException e)
- {
- throw new RuntimeException(e);
- }
- number.setDelegate(get(useClass));
- }
- return number;
- }
- }
- }
-
- Map<String, WeakReference<TypeInfo>> classLoaderCache = getClassLoaderCache(cl);
- TypeInfo result = getFromCache(ctClass.getName(), classLoaderCache);
- if (result != null)
- return result;
-
- result = instantiate(ctClass, clazz);
-
- WeakReference<TypeInfo>weak = new WeakReference<TypeInfo>(result);
- classLoaderCache.put(ctClass.getName(), weak);
-
-// we just ignore generate(..) since it doesnt do anything atm
-// generate(clazz, result);
-
- return result;
- }
-
- /**
- * Reads the type info from the cache
- */
- private TypeInfo getFromCache(String name, Map<String, WeakReference<TypeInfo>> classLoaderCache)
- {
- WeakReference<TypeInfo> weak = classLoaderCache.get(name);
- if (weak != null)
- {
- TypeInfo result = weak.get();
- if (result != null)
- {
- CtClass clazz = getCtClass(result, false);
- if (clazz != null)
- {
- if (compare(clazz, (ClassInfo)result))
- return result;
- }
- classLoaderCache.remove(name);
- }
- }
- return null;
- }
-
- /**
* Get the type info
*
* @param ctClass the ctClass
@@ -564,6 +369,131 @@
if (cl == null)
cl = SecurityActions.getContextClassLoader();
+ return get(name, cl);
+ }
+
+ /**
+ * Gets the type info for a reflect type
+ *
+ * @param type the type
+ * @return the type info
+ */
+ public TypeInfo getTypeInfo(Type type)
+ {
+ if (type instanceof Class)
+ return getTypeInfo((Class<?>) type);
+ else if (type instanceof ParameterizedType)
+ return getParameterizedType((ParameterizedType)type);
+ else if (type instanceof WildcardType)
+ return getWildcardType((WildcardType)type);
+ else if (type instanceof GenericArrayType)
+ return getGenericArrayType((GenericArrayType)type);
+ else if (type instanceof TypeVariable)
+ return getTypeVariable((TypeVariable<?>) type);
+ else
+ throw new UnsupportedOperationException("Unknown type: " + type + " class=" + type.getClass());
+ }
+
+ /**
+ * Get the information for a class
+ *
+ * @param name the name
+ * @param cl the classloader
+ * @return the info
+ * @throws ClassNotFoundException when the class cannot be found
+ */
+ @Override
+ public TypeInfo get(String name, ClassLoader cl) throws ClassNotFoundException
+ {
+ return get(name, cl, null);
+ }
+
+ /**
+ * Get the information for a class
+ *
+ *
+ * @param clazz the class
+ * @return the info
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public TypeInfo get(Class clazz)
+ {
+ try
+ {
+ ClassLoader cl = SecurityActions.getClassLoader(clazz);
+ if(cl == null)
+ cl = SecurityActions.getContextClassLoader();
+ return get(clazz.getName(), cl, clazz);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new RuntimeException("Class not found: "+e.getMessage());
+ }
+ }
+
+ /**
+ * Get the information for a class
+ *
+ * @param name the name
+ * @param cl the classloader
+ * @param clazz the class
+ * @return the info
+ * @throws ClassNotFoundException when the class cannot be found
+ */
+ protected TypeInfo get(String name, ClassLoader cl, Class<?> clazz) throws ClassNotFoundException
+ {
+ if (name == null)
+ throw new IllegalArgumentException("Null name");
+ if (cl == null)
+ throw new IllegalArgumentException("Null classloader");
+
+ TypeInfo info = get(null, clazz, name, cl, true);
+ if (info != null)
+ return info;
+
+ return delegateToIntrospectionImplementation(cl, name);
+ }
+
+ /**
+ * Get the information for a class
+ *
+ * @param ctClass the class
+ * @param clazz the class
+ * @return the info
+ */
+ protected TypeInfo get(CtClass ctClass)
+ {
+ if (ctClass == null)
+ throw new IllegalArgumentException("Null class");
+
+ try
+ {
+ return get(ctClass, null, ctClass.getName(), ctClass.getClassPool().getClassLoader(), false);
+ }
+ catch(Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Get the information for a class
+ *
+ * @param ctClass the class, may be null
+ * @param clazz the class, may be null
+ * @param name the name of the class
+ * @param cl the class loader
+ * @return the info
+ * @throws IllegalArgumentException if either name or cl are null
+ */
+ protected TypeInfo get(CtClass ctClass, Class<?> clazz, String name, ClassLoader cl, boolean delegateToReflectIfNotFound) throws ClassNotFoundException
+ {
+ if (name == null)
+ throw new IllegalArgumentException("Null name");
+ if (cl == null)
+ throw new IllegalArgumentException("Null class loader");
+
TypeInfo primitive = PrimitiveInfo.valueOf(name);
if (primitive != null)
return primitive;
@@ -578,39 +508,96 @@
if (number.getPhase() != NumberInfo.Phase.COMPLETE)
{
number.initializing();
- number.setDelegate(get(Class.forName(name, false, cl)));
+ Class<?> useClass = clazz;
+ try
+ {
+ if (useClass == null)
+ useClass = cl.loadClass(name);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new RuntimeException(e);
+ }
+ number.setDelegate(get(useClass));
}
return number;
}
}
}
+
+ //Handle array names
+ if (name.charAt(0) == '[')
+ name = Descriptor.toClassName(name);
+
+ Map<String, WeakReference<TypeInfo>> classLoaderCache = getClassLoaderCache(cl);
+ TypeInfo result = getFromCache(name, classLoaderCache);
+ if (result != null)
+ return result;
+
+ try
+ {
+ if (ctClass == null)
+ ctClass = poolFactory.getPoolForLoader(cl).get(name);
+ result = instantiate(ctClass, clazz);
+ }
+ catch(NotFoundException e)
+ {
+ result = delegateToIntrospectionImplementation(cl, name);
+ }
- Class<?> clazz = Class.forName(name, false, cl);
- return getTypeInfo(clazz);
+
+ WeakReference<TypeInfo>weak = new WeakReference<TypeInfo>(result);
+ classLoaderCache.put(name, weak);
+
+// we just ignore generate(..) since it doesnt do anything atm
+// generate(clazz, result);
+
+ return result;
}
-
+
/**
- * Gets the type info for a reflect type
+ * Proxies, whether
+ * <ul>
+ * <li>JDK dynamic proxies</li>
+ * <li>javassist ProxyFactory proxies - created by calling ClassLoader.defineClass()</li>
+ * </ul>
+ * are not visible to the javassist classpools, and neither will classes generated by cglib or other
+ * frameworks, so try to load up the class from the reflect implementation.
*
- * @param type the type
- * @return the type info
+ * @param cl the classloader
+ * @param name the name of the class
+ * @return the info
+ * @throws ClassNotFoundException when the class cannot be found
*/
- public TypeInfo getTypeInfo(Type type)
+ private TypeInfo delegateToIntrospectionImplementation(ClassLoader cl, String name) throws ClassNotFoundException
{
- if (type instanceof Class)
- return getTypeInfo((Class<?>) type);
- else if (type instanceof ParameterizedType)
- return getParameterizedType((ParameterizedType)type);
- else if (type instanceof WildcardType)
- return getWildcardType((WildcardType)type);
- else if (type instanceof GenericArrayType)
- return getGenericArrayType((GenericArrayType)type);
- else if (type instanceof TypeVariable)
- return getTypeVariable((TypeVariable<?>) type);
- else
- throw new UnsupportedOperationException("Unknown type: " + type + " class=" + type.getClass());
+ IntrospectionTypeInfoFactory factory = new IntrospectionTypeInfoFactory();
+ return factory.getTypeInfo(name, cl);
}
+ /**
+ * Reads the type info from the cache
+ */
+ private TypeInfo getFromCache(String name, Map<String, WeakReference<TypeInfo>> classLoaderCache)
+ {
+ WeakReference<TypeInfo> weak = classLoaderCache.get(name);
+ if (weak != null)
+ {
+ TypeInfo result = weak.get();
+ if (result != null)
+ {
+ CtClass clazz = getCtClass(result, false);
+ if (clazz != null)
+ {
+ if (compare(clazz, (ClassInfo)result))
+ return result;
+ }
+ classLoaderCache.remove(name);
+ }
+ }
+ return null;
+ }
+
public AnnotationValue[] getAnnotations(Object obj)
{
try
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistUtil.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistUtil.java 2010-04-21 08:23:38 UTC (rev 104106)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistUtil.java 2010-04-21 11:13:38 UTC (rev 104107)
@@ -158,7 +158,7 @@
public static TypeInfo toTypeInfo(CtClass ctClass)
{
JavassistTypeInfoFactoryImpl impl = JavassistTypeInfoFactory.delegate;
- return impl.get(ctClass, null);
+ return impl.get(ctClass);
}
public static CtClass[] toCtClass(ClassInfo[] classes)
Modified: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoProxyTest.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoProxyTest.java 2010-04-21 08:23:38 UTC (rev 104106)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/ClassInfoProxyTest.java 2010-04-21 11:13:38 UTC (rev 104107)
@@ -98,18 +98,7 @@
}
}
assertEquals("test", echo.invoke(proxy, new Object[] {"test"}));
- }
-
- private void testGenericInterface(Class<?> clazz) throws Throwable
- {
- ClassInfoImpl expected = new ClassInfoImpl(clazz.getName(), ModifierInfo.PUBLIC);
- TypeInfo info = testBasics(clazz, expected);
- assertFalse(info.isArray());
- assertFalse(info.isEnum());
- assertFalse(info.isPrimitive());
-
- ClassInfo classInfo = (ClassInfo) info;
- assertClassInfo(classInfo, clazz);
+ assertEquals(type, getTypeInfoFactory().getTypeInfo(proxy.getClass()));
}
}
More information about the jboss-cvs-commits
mailing list