Thanks for suggestion. I did that: I can now get as far as seeing the @Author attribute, but cannot retrieve the name() element. In fact, getMemberNames() always is null. Benedict
Here's a self-contained test class that shows the problem . Thanks Benedict | | import java.lang.annotation.Retention; | import java.lang.annotation.RetentionPolicy; | import java.util.Arrays; | import java.util.List; | import java.util.Set; | | import javassist.ClassPool; | import javassist.CtClass; | import javassist.CtMethod; | import javassist.bytecode.AnnotationsAttribute; | import javassist.bytecode.AttributeInfo; | import javassist.bytecode.ClassFile; | import javassist.bytecode.MethodInfo; | import javassist.bytecode.SourceFileAttribute; | import javassist.bytecode.annotation.Annotation; | import javassist.bytecode.annotation.StringMemberValue; | import junit.framework.TestCase; | import junit.textui.TestRunner; | | /** | * This test demonstrates that when I try to access annotations using Javassist, | * I can get the annotation per se, but the membernames are always null, so I | * can't access the data within the annotation. | * <p> | * Output is: | * | * <pre> | . | | ---------------------testJavassistMethodAnnotations | raw [EMAIL PROTECTED] class ProblemTest$Thing fields=ProblemTest$Thing.this$0:LProblemTest;, [EMAIL PROTECTED] ProblemTest$Thing (LProblemTest;)V], [EMAIL PROTECTED] methodWithAuthor ()V], [EMAIL PROTECTED] methodWithoutAuthor ()V], ] | | -----CtMethod: [EMAIL PROTECTED] methodWithAuthor ()V] | attr:@ProblemTest$Author | Author annotation:@ProblemTest$Author | author.getMemberNames is null | F. | | ---------------------testClassAnnotations | demoCtClass: [EMAIL PROTECTED] class ProblemTest$Thing fields=ProblemTest$Thing.this$0:LProblemTest;, [EMAIL PROTECTED] ProblemTest$Thing (LProblemTest;)V], [EMAIL PROTECTED] methodWithAuthor ()V], [EMAIL PROTECTED] methodWithoutAuthor ()V], ] | classfile: [EMAIL PROTECTED] | interfaces: [] | attributes: [EMAIL PROTECTED], [EMAIL PROTECTED], @ProblemTest$Author] | | --- | attribute:[EMAIL PROTECTED] | an AttributeInfo is of class: class javassist.bytecode.SourceFileAttribute - [EMAIL PROTECTED] | | --- | attribute:[EMAIL PROTECTED] | an AttributeInfo is of class: class javassist.bytecode.InnerClassesAttribute - [EMAIL PROTECTED] | | --- | attribute:@ProblemTest$Author | annotations: [EMAIL PROTECTED] | | ++annotation: @ProblemTest$Author of class class javassist.bytecode.annotation.Annotation | interfaces: [] | typename:ProblemTest$Author | memberNames: null | We have found the Author annotation, but cannot look at the members! | memberValue-name: null | stringMemberValue: null | F | Time: 0.141 | There were 2 failures: | 1) testJavassistMethodAnnotations(ProblemTest)junit.framework.AssertionFailedError: stringmembervalue for name() is null | at ProblemTest.testJavassistMethodAnnotations(ProblemTest.java:166) | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | at ProblemTest.main(ProblemTest.java:274) | 2) testClassAnnotations(ProblemTest)junit.framework.AssertionFailedError: On Author annotation, memberNames is null! | at ProblemTest.dumpAnnotation(ProblemTest.java:261) | at ProblemTest.testClassAnnotations(ProblemTest.java:217) | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | at ProblemTest.main(ProblemTest.java:274) | | FAILURES!!! | Tests run: 2, Failures: 2, Errors: 0 | | | * </pre> | * | * @author bheal . | */ | public class ProblemTest extends TestCase { | | @Retention(RetentionPolicy.RUNTIME)// | public @interface Author { | | String name() default "me"; | | int age() default 999; | } | | @Author// | class Thing { | | @Author public void methodWithAuthor() { | } | | public void methodWithoutAuthor() { | } | } | | public void setUp() { | echo("\n\n---------------------" + getName()); | } | | /** | * * Output is: | * | * <pre> | * | * | * | * | * | * | * | * | * | * | * | * | * .classname:ProblemTest$Thing | * .raw [EMAIL PROTECTED] class ProblemTest$Thing fields=ProblemTest$Thing.this$0:LProblemTest;, [EMAIL PROTECTED] ProblemTest$Thing (LProblemTest;)V], [EMAIL PROTECTED] methodWithAuthor ()V], [EMAIL PROTECTED] methodWithoutAuthor ()V], ] | * CtMethod: [EMAIL PROTECTED] methodWithAuthor ()V] | * attr:null | * CtMethod: [EMAIL PROTECTED] methodWithoutAuthor ()V] | * attr:null | * | * | * | * | * | * | * | * | * | * | * | * | * </pre> | */ | public void testJavassistMethodAnnotations() throws Exception { | | ClassPool pool = ClassPool.getDefault(); | | String demoClassName = "ProblemTest$Thing"; | | CtClass demoClass = pool.get(demoClassName); | echo("raw demoClass=" + demoClass); | | CtMethod[] thingMethods = demoClass.getDeclaredMethods(); | | for (CtMethod ctMethod : thingMethods) { | echo("\n-----CtMethod: " + ctMethod); | | MethodInfo mInfo = ctMethod.getMethodInfo(); | | // AnnotationsAttribute attr = (AnnotationsAttribute) mInfo | // .getAttribute(AnnotationsAttribute.invisibleTag); | | // on suggestion from chiba, changed to 'visibleTag' | AnnotationsAttribute attr = (AnnotationsAttribute) mInfo | .getAttribute(AnnotationsAttribute.visibleTag); | | System.out.println("attr:" + attr); | | if (attr != null) { | // we never get here for invisible tag, but do for visible tag! | Annotation authorAnnotation = attr.getAnnotation("ProblemTest$Author"); | echo("Author annotation:" + authorAnnotation); | assertNotNull("no @Author annotation", authorAnnotation); | // we manage to get here, but cannot find the name() element | | if ( authorAnnotation.getMemberNames()==null) { | // it always is null! | echo("author.getMemberNames is null"); | } | StringMemberValue stringMemberValue = ((StringMemberValue) authorAnnotation | .getMemberValue("name")); | assertNotNull("stringmembervalue for name() is null", stringMemberValue); | String nameValue = stringMemberValue.getValue(); | System.out.println("@Author(name=" + nameValue + ")"); | } | | // assertNotNull("attr is null, so cannot get Author", attr); | } | } | | // remove working reflection tests, to avoid possible name clashes | | // on | // 'Annotation' | | // public void testAllAnnotations() throws Exception { | // Annotation[] allAnnotations = Thing.class.getAnnotations(); | // echo("all annotations: " + Arrays.asList(allAnnotations)); | // assertNotNull("no annotations at all", allAnnotations); | // } | // | // public void testFlagAnnotation() throws Exception { | // Flag flag = Thing.class.getAnnotation(Flag.class); | // assertNotNull("null flag", flag); | // assertEquals("wrong value", flag.f1(), 999); | // } | | public void testClassAnnotations() throws Exception { | ClassPool pool = ClassPool.getDefault(); | // pool.insertClassPath("."); | // echo("pool=" + pool); | | String demoClassName = "ProblemTest$Thing"; | | CtClass demoCtClass = pool.get(demoClassName); | echo("demoCtClass: " + demoCtClass); | | ClassFile classfile = demoCtClass.getClassFile(); | echo("classfile: " + classfile); | echo("interfaces: " + Arrays.asList(classfile.getInterfaces())); | echo("attributes: " + classfile.getAttributes()); | | for (AttributeInfo attributeInfo : (List<AttributeInfo>) classfile.getAttributes()) { | echo("\n---"); | echo("attribute:" + attributeInfo); | if (attributeInfo instanceof AnnotationsAttribute) { | AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) attributeInfo; | | // note these are javassist annotations, not java ones. | Annotation[] annotations = annotationsAttribute.getAnnotations(); | echo("annotations: " + Arrays.asList(annotations)); | for (Annotation annotation : annotations) { | dumpAnnotation(annotation); | } | } else { | echo("an AttributeInfo is of class: %s - %s %n", attributeInfo.getClass(), | attributeInfo); | } | } | } | | /* | * MethodInfo minfo = m.getMethodInfo(); AnnotationsAttribute attr = | * (AnnotationsAttribute) | * minfo.getAttribute(AnnotationsAttribute.invisibleTag); if (atr != null) { | * Annotation an = attr.getAnnotation("Author"); String s = | * ((StringMemberValue)a.getMemberValue("name")).getValue(); | * System.out.println("@Author(name=" + s + ")"); } | */ | void dumpAnnotation(Annotation a) { | echo("\n++annotation: " + a + " of class " + a.getClass()); | echo("interfaces: " + Arrays.asList(a.getClass().getInterfaces())); | echo("typename:" + a.getTypeName()); | | Set<String> memberNames = (Set<String>) a.getMemberNames(); | echo("memberNames: " + memberNames); | | if (memberNames != null) { | for (String memberName : memberNames) { | echo("member " + memberName); | } | } | | if (a.getTypeName().equals("ProblemTest$Author")) { | echo("We have found the Author annotation, but cannot look at the members!"); | | echo("memberValue-name: " + a.getMemberValue("name")); | | StringMemberValue stringMemberValue = ((StringMemberValue) a.getMemberValue("name")); | echo("stringMemberValue: " + stringMemberValue); | | if (stringMemberValue != null) { | String nameValue = stringMemberValue.getValue(); | echo("@Author(name=" + nameValue + ")"); | } | | assertNotNull("On Author annotation, memberNames is null!", memberNames); | } | } | | void echo(String format, Object... objects) { | System.out.format(format, objects); | } | | void echo(String line) { | echo("%s%n", line); | } | | public static void main(String[] args) { | TestRunner.run(ProblemTest.class); | } | } | | | View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3865405#3865405 Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3865405 ------------------------------------------------------- This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting Tool for open source databases. Create drag-&-drop reports. Save time by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc. Download a FREE copy at http://www.intelliview.com/go/osdn_nl _______________________________________________ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user