Re: Unexpected InternalName format on Enum classes
Eugene Kuleshov <[email protected]>
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
I wonder how did you got such bytecode for values() method... When
compiled with Java 1.6 it doesn't have clone() and there is an array
copy is used.
Anyways, your test can be shortened to something like this:
public final class TestClass {
public class MyEnum {
MyEnum[] values;
public MyEnum[] values() {
return values.clone();
}
}
}
The corresponding ASM code for this method look like this:
mv = cw.visitMethod(ACC_PUBLIC, "values",
"()[Ltest/TestClass$MyEnum;", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "test/TestClass$MyEnum", "values",
"[Ltest/TestClass$MyEnum;");
mv.visitMethodInsn(INVOKEVIRTUAL, "[Ltest/TestClass$MyEnum;", "clone",
"()Ljava/lang/Object;");
mv.visitTypeInsn(CHECKCAST, "[Ltest/TestClass$MyEnum;");
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
The owner in INVOKEVIRTUAL is not test/TestClass$MyEnum, but an
array of test/TestClass$MyEnum, so when trying to get this class you
need to check if argument is an array and use Type.getElementType() to
extract the actual array element type. Try something like
Type t = Type.getType("[[Ltest/TestClass$
if(t.getSort()==Type.ARRAY) {
System.err.println(t.getElementType().getInternalName());
}
regards,
Eugene
On Thu, Jul 22, 2010 at 8:14 AM, Freddy Mallet <[email protected]> wrote:
> Hi all,
> I'm one of the developers of the open source Sonar quality platform
> (www.sonarsource.org) and we use ASM to generate some O.O. metrics and
> understand the global architecture of a project.
> I'm currently facing a little issue when analyzing java enumerations (see
> http://jira.codehaus.org/browse/SONAR-1638). Indeed, the bytecode contains
> some use of INVOKEVIRTUAL instruction with an unexpected format (at least
> for me) :
>
> INVOKEVIRTUAL [LspecialCases/ClassWithEnum$MyEnum;.clone
> ()Ljava/lang/Object;
>
> In that case, I expect to get :
>
> INVOKEVIRTUAL specialCases/ClassWithEnum$MyEnum.clone ()Ljava/lang/Object
>
> Indeed, when my implementation of the VisitMethod interface is called back
> on the method visitMethodInsn(int opcode, String owner, String name, String
> desc);
> and in that case the argument "owner" equals to
> "[LspecialCases/ClassWithEnum$MyEnum;" where I expect to get the internal
> name "specialCases/ClassWithEnum$MyEnum"
> Does it means that I need to always check the format of the "owner" argument
> before doing anything ?
> Thanks for your help,
> Freddy
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