[jboss-cvs] JBossAS SVN: r107362 - in projects/jboss-reflect/trunk/src: main/java/org/jboss/reflect/plugins/bytecode/bytes and 5 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Aug 3 12:53:30 EDT 2010
Author: kabir.khan at jboss.com
Date: 2010-08-03 12:53:29 -0400 (Tue, 03 Aug 2010)
New Revision: 107362
Added:
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/BadAnnotationValue.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/BadAnnotationValueClass.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/ClassInfoBadAnnotationValue.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/BadAnnotationValueClassInfoTest.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/BytecodeBadAnnotationValueClassInfoTestCase.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionBadAnnotationValueClassInfoTestCase.java
projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistBadAnnotationValueClassInfoTestCase.java
Modified:
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/BytecodeTypeInfoFactoryImpl.java
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmBehaviourBytes.java
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmFieldBytes.java
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmMemberBytes.java
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/introspection/IntrospectionTypeInfoFactoryImpl.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/JavassistTypeInfoFactoryImpl.java
Log:
[JBREFLECT-137] Better error messages and tests for all implementations when reading annotations referring to non-existant classes
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/BytecodeTypeInfoFactoryImpl.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/BytecodeTypeInfoFactoryImpl.java 2010-08-03 16:48:27 UTC (rev 107361)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/BytecodeTypeInfoFactoryImpl.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -483,11 +483,26 @@
Object[] annotations;
if (obj instanceof ClassBytes)
{
- annotations = ((ClassBytes)obj).getAnnotations();
+ try
+ {
+ annotations = ((ClassBytes)obj).getAnnotations();
+ }
+ catch(Exception e)
+ {
+ throw new RuntimeException("Could not read annotations for " + ((ClassBytes)obj).getTypeInfoName(), e);
+ }
}
else if (obj instanceof MemberBytes)
{
- annotations = ((MemberBytes)obj).getAnnotations();
+ MemberBytes member = (MemberBytes)obj;
+ try
+ {
+ annotations = member.getAnnotations();
+ }
+ catch(Exception e)
+ {
+ throw new RuntimeException("Could not read annotations for " + member.getOwner().getTypeInfoName() + "." + member.getName() + member.getJvmSignature(), e);
+ }
}
else
{
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java 2010-08-03 16:48:27 UTC (rev 107361)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -30,6 +30,13 @@
public interface MemberBytes extends Bytes
{
/**
+ * Gets the class bytes containing this member
+ *
+ * @return the class bytes
+ */
+ ClassBytes getOwner();
+
+ /**
* Gets the name of this member.
*
* @return the name
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmBehaviourBytes.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmBehaviourBytes.java 2010-08-03 16:48:27 UTC (rev 107361)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmBehaviourBytes.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -70,15 +70,22 @@
public Annotation[][] getParameterAnnotations()
{
- LoadParameterAnnotationsVisitor visitor = new LoadParameterAnnotationsVisitor();
- getClazz().getReader().readMethod(visitor, AsmClassBytes.STANDARD_FLAGS | ClassReader.INCLUDE_METHOD_PARAMETER_ANNOTATIONS, null, null, getByteCodeIndex());
- return visitor.getAnnotations();
+ try
+ {
+ LoadParameterAnnotationsVisitor visitor = new LoadParameterAnnotationsVisitor();
+ getOwner().getReader().readMethod(visitor, AsmClassBytes.STANDARD_FLAGS | ClassReader.INCLUDE_METHOD_PARAMETER_ANNOTATIONS, null, null, getByteCodeIndex());
+ return visitor.getAnnotations();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not read parameter annotations for " + getOwner().getTypeInfoName() + "." + getName() + getJvmSignature(), e);
+ }
}
public Annotation[] getAnnotations()
{
LoadBehaviourAnnotationsVisitor visitor = new LoadBehaviourAnnotationsVisitor();
- getClazz().getReader().readMethod(visitor, AsmClassBytes.STANDARD_FLAGS | ClassReader.INCLUDE_METHOD_ANNOTATIONS, null, null, getByteCodeIndex());
+ getOwner().getReader().readMethod(visitor, AsmClassBytes.STANDARD_FLAGS | ClassReader.INCLUDE_METHOD_ANNOTATIONS, null, null, getByteCodeIndex());
return visitor.getAnnotations();
}
@@ -106,7 +113,7 @@
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible)
{
- return Util.createAnnotationVisitor(this, AsmBehaviourBytes.this.getClazz().getClassLoader(), desc);
+ return Util.createAnnotationVisitor(this, AsmBehaviourBytes.this.getOwner().getClassLoader(), desc);
}
public void setValueInParent(String name, Object value)
@@ -172,7 +179,7 @@
try
{
lastIndex = parameter;
- return Util.createAnnotationVisitor(this, AsmBehaviourBytes.this.getClazz().getClassLoader(), desc);
+ return Util.createAnnotationVisitor(this, AsmBehaviourBytes.this.getOwner().getClassLoader(), desc);
}
catch (Exception e)
{
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmFieldBytes.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmFieldBytes.java 2010-08-03 16:48:27 UTC (rev 107361)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmFieldBytes.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -47,7 +47,7 @@
public Annotation[] getAnnotations()
{
LoadFieldAnnotationsVisitor visitor = new LoadFieldAnnotationsVisitor();
- getClazz().getReader().readField(visitor, AsmClassBytes.STANDARD_FLAGS | ClassReader.INCLUDE_FIELD_ANNOTATIONS, new Attribute[0], null, getByteCodeIndex());
+ getOwner().getReader().readField(visitor, AsmClassBytes.STANDARD_FLAGS | ClassReader.INCLUDE_FIELD_ANNOTATIONS, new Attribute[0], null, getByteCodeIndex());
return visitor.getAnnotations();
}
@@ -79,7 +79,7 @@
{
public AnnotationVisitor visitAnnotation(String desc, boolean visible)
{
- return Util.createAnnotationVisitor(this, AsmFieldBytes.this.getClazz().getClassLoader(), desc);
+ return Util.createAnnotationVisitor(this, AsmFieldBytes.this.getOwner().getClassLoader(), desc);
}
public void setValueInParent(String name, Object value)
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmMemberBytes.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmMemberBytes.java 2010-08-03 16:48:27 UTC (rev 107361)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/asm/AsmMemberBytes.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -77,7 +77,7 @@
return byteCodeIndex;
}
- AsmClassBytes getClazz()
+ public AsmClassBytes getOwner()
{
return clazz;
}
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/introspection/IntrospectionTypeInfoFactoryImpl.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/introspection/IntrospectionTypeInfoFactoryImpl.java 2010-08-03 16:48:27 UTC (rev 107361)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/introspection/IntrospectionTypeInfoFactoryImpl.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -27,6 +27,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.GenericDeclaration;
+import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
@@ -175,7 +176,7 @@
infos[i] = new ReflectConstructorInfoImpl(annotations,
getTypeInfos(genericParameterTypes),
- getParameterAnnotations(constructors[i].getParameterAnnotations()),
+ getParameterAnnotations(readParameterAnnotations(constructors[i])),
getClassInfos(constructors[i].getGenericExceptionTypes()),
constructors[i].getModifiers(),
(ClassInfo) getTypeInfo(constructors[i].getDeclaringClass()));
@@ -233,7 +234,7 @@
methods[i].getName(),
getTypeInfo(methods[i].getGenericReturnType()),
getTypeInfos(methods[i].getGenericParameterTypes()),
- getParameterAnnotations(methods[i].getParameterAnnotations()),
+ getParameterAnnotations(readParameterAnnotations(methods[i])),
getClassInfos(methods[i].getGenericExceptionTypes()),
methods[i].getModifiers(),
(ClassInfo) getTypeInfo(methods[i].getDeclaringClass()));
@@ -638,6 +639,37 @@
}
}
+ private Annotation[][] readParameterAnnotations(final Method method)
+ {
+ try
+ {
+ return method.getParameterAnnotations();
+ }
+ catch (Exception e)
+ {
+ handleReadParameterAnnotationsFailure(method, e);
+ return null;
+ }
+ }
+
+ private Annotation[][] readParameterAnnotations(final Constructor<?> constructor)
+ {
+ try
+ {
+ return constructor.getParameterAnnotations();
+ }
+ catch (Exception e)
+ {
+ handleReadParameterAnnotationsFailure(constructor, e);
+ return null;
+ }
+ }
+
+ private void handleReadParameterAnnotationsFailure(Member m, Throwable t)
+ {
+ throw new RuntimeException("Could not read parameter annotations for " + m, t);
+ }
+
protected AnnotationValue[][] getParameterAnnotations(Annotation[][] annotations)
{
AnnotationValue[][] annotationValues = new AnnotationValue[annotations.length][];
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-08-03 16:48:27 UTC (rev 107361)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistAnnotatedParameterInfo.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -216,10 +216,9 @@
Object[][] parameterAnnotations = ctBehavior.getParameterAnnotations();
setupParameterAnnotations(parameterAnnotations);
}
- catch (ClassNotFoundException e)
+ catch (Exception e)
{
- // AutoGenerated
- throw new RuntimeException(e);
+ throw new RuntimeException("Could not read parameter annotations for " + ctBehavior, 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-08-03 16:48:27 UTC (rev 107361)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistTypeInfoFactoryImpl.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -57,6 +57,7 @@
import org.jboss.reflect.plugins.EnumConstantInfoImpl;
import org.jboss.reflect.plugins.GenericsUtil;
import org.jboss.reflect.plugins.TypeVariableAware;
+import org.jboss.reflect.plugins.bytecode.bytes.ClassBytes;
import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
import org.jboss.reflect.plugins.javassist.classpool.ClassPoolFactory;
import org.jboss.reflect.plugins.javassist.classpool.DefaultClassPoolFactory;
@@ -689,7 +690,7 @@
}
catch (Throwable t)
{
- throw new RuntimeException(t);
+ throw new RuntimeException("Could not read annotations for " + (obj instanceof CtClass ? ((CtClass)obj).getName() : obj.toString()), t);
}
}
Copied: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/BadAnnotationValue.java (from rev 107349, projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java)
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/BadAnnotationValue.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/BadAnnotationValue.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -0,0 +1,36 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.support;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface BadAnnotationValue
+{
+ Class<?>[] value();
+}
Copied: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/BadAnnotationValueClass.java (from rev 107349, projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java)
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/BadAnnotationValueClass.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/BadAnnotationValueClass.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -0,0 +1,32 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.support;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class BadAnnotationValueClass
+{
+
+}
Copied: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/ClassInfoBadAnnotationValue.java (from rev 107349, projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java)
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/ClassInfoBadAnnotationValue.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/support/ClassInfoBadAnnotationValue.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -0,0 +1,44 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.support;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+ at BadAnnotationValue(BadAnnotationValueClass.class)
+public class ClassInfoBadAnnotationValue
+{
+ @BadAnnotationValue(BadAnnotationValueClass.class)
+ int field;
+
+ @BadAnnotationValue(BadAnnotationValueClass.class)
+ public ClassInfoBadAnnotationValue(@BadAnnotationValue(BadAnnotationValueClass.class) int i)
+ {
+ }
+
+ @BadAnnotationValue(BadAnnotationValueClass.class)
+ public void method(@BadAnnotationValue(BadAnnotationValueClass.class) int i)
+ {
+ }
+}
Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/BadAnnotationValueClassInfoTest.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/BadAnnotationValueClassInfoTest.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/BadAnnotationValueClassInfoTest.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -0,0 +1,273 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.ConstructorInfo;
+import org.jboss.reflect.spi.FieldInfo;
+import org.jboss.reflect.spi.MethodInfo;
+import org.jboss.reflect.spi.TypeInfoFactory;
+import org.jboss.test.ContainerTest;
+import org.jboss.test.classinfo.support.ClassInfoBadAnnotationValue;
+
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class BadAnnotationValueClassInfoTest extends ContainerTest
+{
+ public BadAnnotationValueClassInfoTest(String name)
+ {
+ super(name);
+ }
+
+ public void testTestClassLoaderAndClassInfo() throws Exception
+ {
+ ClassLoader loader = createLoader();
+ Class<?> clazz = loader.loadClass(ClassInfoBadAnnotationValue.class.getName());
+ assertNotNull(clazz);
+ assertSame(loader, clazz.getClassLoader());
+ assertInstanceOf(getTypeInfoFactory().getTypeInfo(clazz), getClassInfoClass());
+ }
+
+ public void testBadClassInfoAnnotations() throws Exception
+ {
+ ClassLoader loader = createLoader();
+ Class<?> clazz = loader.loadClass(ClassInfoBadAnnotationValue.class.getName());
+ ClassInfo cinfo = assertInstanceOf(getTypeInfoFactory().getTypeInfo(clazz), ClassInfo.class);
+ try
+ {
+ cinfo.getAnnotations();
+ fail("Should not have been able to load class annotations");
+ }
+ catch (Exception expected)
+ {
+ checkException("Could not read", expected);
+ }
+ }
+
+ public void testBadFieldInfoAnnotations() throws Exception
+ {
+ ClassLoader loader = createLoader();
+ Class<?> clazz = loader.loadClass(ClassInfoBadAnnotationValue.class.getName());
+ ClassInfo cinfo = assertInstanceOf(getTypeInfoFactory().getTypeInfo(clazz), ClassInfo.class);
+ try
+ {
+ FieldInfo field = cinfo.getDeclaredFields()[0];
+ field.getAnnotations();
+ fail("Should not have been able to load field annotations");
+ }
+ catch (Exception expected)
+ {
+ checkException("Could not read", expected);
+ }
+ }
+
+ public void testBadConstructorInfoAnnotations() throws Exception
+ {
+ ClassLoader loader = createLoader();
+ Class<?> clazz = loader.loadClass(ClassInfoBadAnnotationValue.class.getName());
+ ClassInfo cinfo = assertInstanceOf(getTypeInfoFactory().getTypeInfo(clazz), ClassInfo.class);
+ try
+ {
+ ConstructorInfo ctor = cinfo.getDeclaredConstructors()[0];
+ ctor.getAnnotations();
+ fail("Should not have been able to load constructor annotations");
+ }
+ catch (Exception expected)
+ {
+ checkException("Could not read", expected);
+ }
+ }
+
+ public void testBadConstructorInfoParameterAnnotations() throws Exception
+ {
+ ClassLoader loader = createLoader();
+ Class<?> clazz = loader.loadClass(ClassInfoBadAnnotationValue.class.getName());
+ ClassInfo cinfo = assertInstanceOf(getTypeInfoFactory().getTypeInfo(clazz), ClassInfo.class);
+ try
+ {
+ ConstructorInfo ctor = cinfo.getDeclaredConstructors()[0];
+ ctor.getParameters()[0].getAnnotations();
+ fail("Should not have been able to load parameter annotations");
+ }
+ catch (Exception expected)
+ {
+ checkException("Could not read", expected);
+ }
+ }
+
+ public void testBadMethodInfoAnnotations() throws Exception
+ {
+ ClassLoader loader = createLoader();
+ Class<?> clazz = loader.loadClass(ClassInfoBadAnnotationValue.class.getName());
+ ClassInfo cinfo = assertInstanceOf(getTypeInfoFactory().getTypeInfo(clazz), ClassInfo.class);
+ try
+ {
+ MethodInfo method = cinfo.getDeclaredMethods()[0];
+ method.getAnnotations();
+ fail("Should not have been able to load method annotations");
+ }
+ catch (Exception expected)
+ {
+ checkException("Could not read", expected);
+ }
+ }
+
+ public void testBadMethodInfoParameterAnnotations() throws Exception
+ {
+ ClassLoader loader = createLoader();
+ Class<?> clazz = loader.loadClass(ClassInfoBadAnnotationValue.class.getName());
+ ClassInfo cinfo = assertInstanceOf(getTypeInfoFactory().getTypeInfo(clazz), ClassInfo.class);
+ try
+ {
+ MethodInfo method = cinfo.getDeclaredMethods()[0];
+ method.getParameters()[0].getAnnotations();
+ fail("Should not have been able to load parameter annotations");
+ }
+ catch (Exception expected)
+ {
+ checkException("Could not read", expected);
+ }
+ }
+
+ protected abstract TypeInfoFactory getTypeInfoFactory();
+
+ protected abstract Class<? extends ClassInfo> getClassInfoClass();
+
+ private void checkException(String s, Throwable t)
+ {
+ if (t.getMessage().contains(s))
+ return;
+
+ Throwable cause = t.getCause();
+ if (cause == null || cause == t)
+ throw new RuntimeException("Could not find error message in trace", t);
+ checkException(s, cause);
+ }
+
+ protected TestClassLoader createLoader()
+ {
+ return new TestClassLoader(Thread.currentThread().getContextClassLoader());
+ }
+
+ static class TestClassLoader extends ClassLoader
+ {
+ Map<String, Class<?>> loadedClasses = new HashMap<String, Class<?>>();
+ ClassLoader parent;
+
+ public TestClassLoader(ClassLoader parent)
+ {
+ this.parent = parent;
+ }
+
+ @Override
+ public Class<?> loadClass(String name) throws ClassNotFoundException
+ {
+ if (name.startsWith("java."))
+ return parent.loadClass(name);
+
+ if (name.contains("BadAnnotationValueClass"))
+ throw new ClassNotFoundException("BadAnnotationValueClass not found");
+
+ if (loadedClasses.containsKey(name))
+ return loadedClasses.get(name);
+
+ String resource = name.replace('.', '/') + ".class";
+ if (parent.getResource(resource) == null)
+ throw new ClassNotFoundException("Could not find " + name);
+
+ InputStream in = parent.getResourceAsStream(resource);
+ if (in == null)
+ throw new ClassNotFoundException("Could not find stream for" + name);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ byte[] bytes = null;
+ try
+ {
+ int i = in.read();
+ while (i != -1)
+ {
+ out.write((byte)i);
+ i = in.read();
+ }
+ bytes = out.toByteArray();
+ }
+ catch (IOException e)
+ {
+ throw new ClassNotFoundException("Error reading class " + name);
+ }
+ finally
+ {
+ close(in);
+ close(out);
+ }
+
+ Class<?> clazz = super.defineClass(name/*.replace('.', '/')*/, bytes, 0, bytes.length, null);
+ resolveClass(clazz);
+
+ loadedClasses.put(name, clazz);
+
+ return clazz;
+ }
+
+ @Override
+ public URL getResource(String name)
+ {
+ if (name.contains("BadAnnotationValueClass"))
+ return null;
+ return parent.getResource(name);
+ }
+
+ @Override
+ public InputStream getResourceAsStream(String name)
+ {
+ if (name.contains("BadAnnotationValueClass"))
+ return null;
+ return parent.getResourceAsStream(name);
+ }
+
+ private void close(Closeable c)
+ {
+ if (c == null)
+ return;
+
+ try
+ {
+ c.close();
+ }
+ catch (IOException ignore)
+ {
+ }
+ }
+
+ }
+}
Copied: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/BytecodeBadAnnotationValueClassInfoTestCase.java (from rev 107349, projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java)
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/BytecodeBadAnnotationValueClassInfoTestCase.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/BytecodeBadAnnotationValueClassInfoTestCase.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -0,0 +1,58 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+import junit.framework.TestSuite;
+
+import org.jboss.reflect.plugins.bytecode.BytecodeTypeInfo;
+import org.jboss.reflect.plugins.bytecode.BytecodeTypeInfoFactory;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.TypeInfoFactory;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class BytecodeBadAnnotationValueClassInfoTestCase extends BadAnnotationValueClassInfoTest
+{
+ public BytecodeBadAnnotationValueClassInfoTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static TestSuite suite()
+ {
+ return new TestSuite(BytecodeBadAnnotationValueClassInfoTestCase.class);
+ }
+
+ protected TypeInfoFactory getTypeInfoFactory()
+ {
+ return new BytecodeTypeInfoFactory();
+ }
+
+ @Override
+ protected Class<? extends ClassInfo> getClassInfoClass()
+ {
+ return BytecodeTypeInfo.class;
+ }
+}
Copied: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionBadAnnotationValueClassInfoTestCase.java (from rev 107349, projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/bytecode/bytes/MemberBytes.java)
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionBadAnnotationValueClassInfoTestCase.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/IntrospectionBadAnnotationValueClassInfoTestCase.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -0,0 +1,58 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
+import org.jboss.reflect.plugins.introspection.ReflectClassInfoImpl;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.TypeInfoFactory;
+
+import junit.framework.TestSuite;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class IntrospectionBadAnnotationValueClassInfoTestCase extends BadAnnotationValueClassInfoTest
+{
+ public IntrospectionBadAnnotationValueClassInfoTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static TestSuite suite()
+ {
+ return new TestSuite(IntrospectionBadAnnotationValueClassInfoTestCase.class);
+ }
+
+ protected TypeInfoFactory getTypeInfoFactory()
+ {
+ return new IntrospectionTypeInfoFactory();
+ }
+
+ @Override
+ protected Class<? extends ClassInfo> getClassInfoClass()
+ {
+ return ReflectClassInfoImpl.class;
+ }
+}
Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistBadAnnotationValueClassInfoTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistBadAnnotationValueClassInfoTestCase.java (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/classinfo/test/JavassistBadAnnotationValueClassInfoTestCase.java 2010-08-03 16:53:29 UTC (rev 107362)
@@ -0,0 +1,163 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.LoaderClassPath;
+import javassist.NotFoundException;
+import junit.framework.TestSuite;
+
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfo;
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactory;
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactoryImpl;
+import org.jboss.reflect.plugins.javassist.classpool.ClassPoolFactory;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.TypeInfoFactory;
+import org.jboss.test.AbstractTestDelegate;
+import org.jboss.test.classinfo.support.BadAnnotationValueClass;
+import org.jboss.test.classinfo.test.BadAnnotationValueClassInfoTest.TestClassLoader;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class JavassistBadAnnotationValueClassInfoTestCase extends BadAnnotationValueClassInfoTest
+{
+
+ public JavassistBadAnnotationValueClassInfoTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static TestSuite suite()
+ {
+ return new TestSuite(JavassistBadAnnotationValueClassInfoTestCase.class);
+ }
+
+ public static AbstractTestDelegate getDelegate(Class<?> clazz) throws Exception
+ {
+ AbstractTestDelegate delegate = new ClassPoolTestDelegate(clazz);
+ return delegate;
+ }
+
+
+ protected TypeInfoFactory getTypeInfoFactory()
+ {
+ return new JavassistTypeInfoFactory();
+ }
+
+ @Override
+ protected Class<? extends ClassInfo> getClassInfoClass()
+ {
+ return JavassistTypeInfo.class;
+ }
+
+ @Override
+ protected TestClassLoader createLoader()
+ {
+ TestClassLoader loader = super.createLoader();
+ TestClassPoolFactory.INSTANCE.registerLoader(loader);
+ return loader;
+ }
+
+
+ private static class TestClassPool extends ClassPool
+ {
+ final ClassLoader cl;
+
+ TestClassPool(ClassLoader cl)
+ {
+ this.cl = cl;
+ appendClassPath(new LoaderClassPath(cl));
+ }
+
+ @Override
+ public CtClass get(String classname) throws NotFoundException
+ {
+ if (classname.startsWith("java."))
+ return ClassPool.getDefault().get(classname);
+
+ if (classname.contains(BadAnnotationValueClass.class.getName()))
+ throw new NotFoundException("Could not find " + classname);
+ return super.get(classname);
+ }
+
+ @Override
+ public ClassLoader getClassLoader()
+ {
+ return cl;
+ }
+ }
+
+ private static class TestClassPoolFactory implements ClassPoolFactory
+ {
+ static TestClassPoolFactory INSTANCE = new TestClassPoolFactory();
+
+ Map<ClassLoader, ClassPool> pools = new HashMap<ClassLoader, ClassPool>();
+
+ void registerLoader(ClassLoader loader)
+ {
+ pools.put(loader, new TestClassPool(loader));
+ }
+
+ public ClassPool getPoolForLoader(ClassLoader classLoader)
+ {
+ ClassPool pool = pools.get(classLoader);
+ if (pool != null)
+ return pool;
+ return pool.getDefault();
+ }
+ }
+
+ protected static class ClassPoolTestDelegate extends AbstractTestDelegate
+ {
+ ClassPoolFactory factory = JavassistTypeInfoFactoryImpl.getPoolFactory();
+
+ public ClassPoolTestDelegate(Class<?> clazz)
+ {
+ super(clazz);
+ }
+
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ JavassistTypeInfoFactoryImpl.setPoolFactory(TestClassPoolFactory.INSTANCE);
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ if (factory == null)
+ JavassistTypeInfoFactoryImpl.setPoolFactory(factory);
+ super.tearDown();
+ }
+
+ }
+}
More information about the jboss-cvs-commits
mailing list