4

I'm trying to instantiate a class in Jasmin like this:

new Ljava/lang/Object;
dup
invokespecial java/lang/Object/<init>()V

This does compile but the class vierifier complains:

"VerifyError: Call to wrong initialization method."

What's the correct way to instantiate a class?

oddin
  • 391
  • 3
  • 11
  • I'm no jasmine expert but are you trying to build an instance of Object? that class is abstract and therefore you cannot initialize it directly – Juan Alberto López Cavallotti May 29 '12 at 20:12
  • @JuanAlbertoLópezCavallotti: You mean this class: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html ? It's not abstract. You can safely call `new Object()` from Java and it compiles. – npe May 29 '12 at 20:14
  • Indeed, it's not abstract. It's possible to instantiate it but I'm unsure about the syntax. – oddin May 29 '12 at 20:18
  • @oddin look at that, everyday you learn something new, I guess that it had no sense to me before to try and instantiate Object. Do you get the same error if you try to run the app? – Juan Alberto López Cavallotti May 29 '12 at 20:18
  • Instantiating Object is common to use them for locks in synchronized blocks. And I actually get the error when I try to run the class. VerifyError is thrown at runtime. – oddin May 29 '12 at 20:20

2 Answers2

5

Ahh, I found the f***ing problem:

The parameter of 'new' should be the plain qualified name of the type, not it's decorated name. I. e. instead of

new Ljava/lang/Object;

you should write:

new java/lang/Object

The working code is:

new java/lang/Object
dup
invokespecial java/lang/Object/<init>()V
oddin
  • 391
  • 3
  • 11
1

Try with invokespecial java/lang/Object/()V.

npe
  • 15,395
  • 1
  • 56
  • 55
  • 1
    Wow, why not ()V? Btw, this gives me another error: "Expecting to find object/array on stack." The reference pushed by 'new' should be on the top of the stack, shouldn't it? – oddin May 29 '12 at 21:08