Skip to content

Commit

Permalink
[JBREFLECT-49]
Browse files Browse the repository at this point in the history
Tried to make JavassistTypeInfo and JavassistTypeInfoFactoryImpl mutable
compliant. Note that this breaks the current testsuite since
JavassisitTypeInfo.getType() do not any longer return any object.
  • Loading branch information
stalep committed Jan 29, 2009
1 parent 3cd6959 commit a1a396c
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 6 deletions.
Expand Up @@ -323,32 +323,34 @@ public MethodInfo[] getDeclaredMethods()

public boolean isArray()
{
return getType().isArray();
return getCtClass().isArray();
}

//TODO: need to change the use of getType() here
public boolean isCollection()
{
return Collection.class.isAssignableFrom(getType());
}

//TODO: need to change the use of getType() here
public boolean isMap()
{
return Map.class.isAssignableFrom(getType());
}

public boolean isAnnotation()
{
return getType().isAnnotation();
return getCtClass().isAnnotation();
}

public boolean isEnum()
{
return getType().isEnum();
return getCtClass().isEnum();
}

public boolean isPrimitive()
{
return getType().isPrimitive();
return getCtClass().isPrimitive();
}

/**
Expand Down Expand Up @@ -700,4 +702,9 @@ public Object getAttachment(String name)
}
return attachments.getAttachment(name);
}

protected CtClass getCtClass()
{
return ctClass;
}
}
Expand Up @@ -22,7 +22,9 @@
package org.jboss.reflect.plugins.javassist;

import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.Map;

import javassist.ClassPool;
import javassist.CtClass;
Expand All @@ -38,6 +40,8 @@
import org.jboss.reflect.plugins.EnumConstantInfoImpl;
import org.jboss.reflect.spi.AnnotationInfo;
import org.jboss.reflect.spi.AnnotationValue;
import org.jboss.reflect.spi.ClassInfo;
import org.jboss.reflect.spi.ClassPoolFactory;
import org.jboss.reflect.spi.NumberInfo;
import org.jboss.reflect.spi.PrimitiveInfo;
import org.jboss.reflect.spi.TypeInfo;
Expand All @@ -52,7 +56,8 @@
*/
public class JavassistTypeInfoFactoryImpl extends WeakClassCache implements TypeInfoFactory, AnnotationHelper
{
static final ClassPool pool = ClassPool.getDefault();
//TODO: Need to change this to a usable CPF.
static final ClassPoolFactory poolFactory = new DummyClassPoolFactory();

static final AnnotationValue[] NO_ANNOTATIONS = new AnnotationValue[0];
/**
Expand Down Expand Up @@ -167,7 +172,146 @@ else if (ctClass.isEnum())
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
protected Object instantiate(CtClass ctClass)
{
try
{

if (ctClass.isArray())
{
TypeInfo componentType = getTypeInfo(ctClass.getComponentType());
return new JavassistArrayInfoImpl(this, ctClass, null, componentType);
}

if (ctClass.isAnnotation())
{
JavassistAnnotationInfo result = new JavassistAnnotationInfo(this, ctClass, null);
CtMethod[] methods = ctClass.getDeclaredMethods();
AnnotationAttributeImpl[] atttributes = new AnnotationAttributeImpl[methods.length];
for (int i = 0 ; i < methods.length ; i++)
{
atttributes[i] = new AnnotationAttributeImpl(methods[i].getName(), getTypeInfo(methods[i].getReturnType()), null);
}
result.setAttributes(atttributes);
return result;

}
else if (ctClass.isEnum())
{
JavassistEnumInfo enumInfo = new JavassistEnumInfo(this, ctClass, null);
CtField[] fields = ctClass.getFields();
EnumConstantInfoImpl[] constants = new EnumConstantInfoImpl[fields.length];
int i = 0;
for (CtField field : fields)
{
AnnotationValue[] annotations = getAnnotations(field);
constants[i++] = new EnumConstantInfoImpl(field.getName(), enumInfo, annotations);
}
enumInfo.setEnumConstants(constants);
return enumInfo;
}


return new JavassistTypeInfo(this, ctClass, null);
}
catch (NotFoundException e)
{
throw new RuntimeException(e);
}
}

/**
* 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 Object get(String name, ClassLoader cl) throws ClassNotFoundException
{
if (name == null)
throw new IllegalArgumentException("Null name");
if (cl == null)
throw new IllegalArgumentException("Null classloader");
try
{
CtClass clazz = poolFactory.getPoolForLoader(cl).get(name);

return get(clazz);
}
catch(NotFoundException nfe)
{
throw new ClassNotFoundException(nfe.getMessage());
}
}

/**
* Get the information for a class
*
*
* @param clazz the class
* @return the info
*/
@Override
public Object get(Class clazz)
{
try
{
ClassLoader cl = clazz.getClassLoader();
if(cl == null)
cl = Thread.currentThread().getContextClassLoader();
return get(clazz.getName(), cl);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException("Class not found: "+e.getMessage());
}
}


/**
* Get the information for a class
*
* @param clazz the class
* @return the info
*/
public Object get(CtClass clazz)
{
if (clazz == null)
throw new IllegalArgumentException("Null class");

Map classLoaderCache = getClassLoaderCache(clazz.getClassPool().getClassLoader());

WeakReference weak = (WeakReference) classLoaderCache.get(clazz.getName());
if (weak != null)
{
Object result = weak.get();
if (result != null)
{
if(compare(clazz, (ClassInfo) result))
return result;
else
{
classLoaderCache.remove(clazz.getName());
}
}
}

Object result = instantiate(clazz);

weak = new WeakReference(result);
classLoaderCache.put(clazz.getName(), weak);

// we just ignore generate(..) since it doesnt do anything atm
// generate(clazz, result);

return result;
}

/**
* Get the type info
*
Expand Down Expand Up @@ -237,7 +381,8 @@ protected CtClass getCtClass(String name)
{
try
{
return pool.get(name);
//TODO: Need to change this
return poolFactory.getPoolForLoader(null).get(name);
}
catch (NotFoundException e)
{
Expand Down Expand Up @@ -369,4 +514,14 @@ public AnnotationValue createAnnotationValue(AnnotationInfo info, Object ann)
return AnnotationValueFactory.createAnnotationValue(this, this, info, ann);
}


private boolean compare(CtClass clazz, ClassInfo info)
{
if(clazz.getDeclaredMethods().length == info.getDeclaredMethods().length &&
clazz.getDeclaredConstructors().length == info.getDeclaredConstructors().length &&
clazz.getDeclaredFields().length == info.getDeclaredFields().length)
return true;
else
return false;
}
}

0 comments on commit a1a396c

Please sign in to comment.