I am using the BCEL library to analyze some code. I came across a method (getAllNames()) whose return type is List< Name >. I want to be able to obtain the return type of this method.
I want to be able to obtain the full class of "Name".
I have tried using the < Instruction.getReturnType() > method in my method visitor class but for this specific method I get the result "java.util.List". I want the generic type "com.instant.Name" instead.
The signature for the method is like so:
public List<Name> getAllNames() {
...
}
I also have a org.apache.bcel.generic.MethodGen object that I create before visiting the method using org.apache.bcel.classfile.Method
When I try to get return type it again gives "java.util.List"
I expect the output of MethodGen.getReturnType() to be "com.instant.Name" but the actual output is "java.util.List"
Contrary to some comments the information is obviously there, type erasure does not happen on the interface level - just as Eclipse/NetBeans/IntelliJ/etc. can get the exact type/return type of class members, you can do that too:
BCELis another story, I am not familiar with it, however the very end ofFieldOrMethod.java, there is a method calledgetGenericSignature(). You may find it useful already (though presumably it produces a signature), or you can replicate the loop inside, over theattributes(you can get them viagetAttributes()), checking for an occurrence ofinstanceof Signature:The real code suggests that there can be only one such attribute and the loop could exit afterwards, but it made me think about types having multiple parameters, like
Map-s...getGenericSignature()itself does not occur anywhere else (neither tested, nor used), so I can only hope that it (and consequently, the approach described above) really works.