Re: Problems getting generic signatures of generated classes to show via Reflection

Eugene Kuleshov <[email protected]>
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
  Worked for me. The following code prints java.util.List<java.lang.String>

    final byte[] buff = TestDump.dump();
    Class c = new ClassLoader() { Class c = defineClass("test.Test", 
buff, 0, buff.length); }.c;
   
    Method[] methods = c.getDeclaredMethods();
    System.err.println(methods[0].getGenericReturnType());

  where TestDump class were generated by ASMifier from the following 
source code compiled with Java 1.5:

package test;
import java.util.List;

public class Test {
  private List<String> list;
  public List<String> getList() {
    return list;
  }
}

  regards,
  Eugene


Tatu Saloranta wrote:
> (note: apologies if this email gets sent twice -- I had some issues
> subscribing to the list)
>
> I am trying to generate simple setters and getters, but such that
> return type and arguments would contain proper generic signature. This
> is necessary as classes are further introspected by another library
> which needs to know generic type information for its handling.
> As far as I know, code should work (I have sample later); and when
> using ASM's ClassVisitor, things look ok.
> However, when trying to access generic type information via reflection
> (Class.getDeclaredMethods(), getDeclaredFields()), no generic type
> information is seen.
> It would seem as if the way ASM adds attributes was somehow not
> compatible with regular JDK's expectations.
>
> Code I use is quite simple, along lines of:
>
> ---
>        final String className = "TestClass";
>        final String internalClass = "TestClass";
>
>        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
>
>        cw.visit(V1_2, ACC_PUBLIC + ACC_SUPER, internalClass, null,
> "java/lang/Object", new String[0]);
>        cw.visitSource("TestClass.java", null);
>
>        // default constructor:
>        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V",
> null, null);
>        mv.visitCode();
>        mv.visitVarInsn(ALOAD, 0);
>        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
>        mv.visitInsn(RETURN);
>        mv.visitMaxs(0, 0); // don't care (real values: 1,1)
>        mv.visitEnd();
>
>        // one field
>        String fieldDesc = "Ljava/util/List;";
>        String fieldSig = "Ljava/util/List<Ljava/lang/String;>;";
>        final String fieldName = "foo";
>        FieldVisitor fv = cw.visitField(ACC_PUBLIC, fieldName,
> fieldDesc, fieldSig, null);
>        fv.visitEnd();
>
>        // then one getter (removed for now)
>
>        cw.visitEnd();
>
> ---
>
> and I even checked with utility class to see what ASM thinks is proper
> way to generate classes (using a JDK compiled similar bean as base).
> But types that Method and Field show are raw type erased classes.
>
> Any guesses as to what I might be doing wrong?
>
> -+ Tatu +-
>
message-footer.txt (text/plain, 238 B)
-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.