Sunday 15 February 2015

opcode - Java & Javap: How to Determine Which Object Receives Invokevirtual -



opcode - Java & Javap: How to Determine Which Object Receives Invokevirtual -

i looking through output of javap. example:

this code

final foo foo = new foo(1,2); ... new callable<integer>() { @override public integer call() throws exception { homecoming foo.dosomething(); }

generates:

jvmoperations": [{ "byteoffset": 0, "constantpoolindex": null, "opcode": 42, "opcodename": "aload_0", "type": null, "tagname": null, "tagvalue": null }, { "byteoffset": 1, "constantpoolindex": null, "opcode": 180, "opcodename": "getfield", "type": null, "tagname": "field", "tagvalue": "val$foo:lcom/example/graph/demo/foo;" }, { "byteoffset": 4, "constantpoolindex": null, "opcode": 182, "opcodename": "invokevirtual", "type": null, "tagname": "method", "tagvalue": "com/example/graph/demo/foo.dosomething:()ljava/lang/integer;" }, { "byteoffset": 7, "constantpoolindex": null, "opcode": 176, "opcodename": "areturn", "type": null, "tagname": null, "tagvalue": null }]

so see object identified in case val$foo. , in class metadata

"classmetadata": { "classid": "com/example/main$1.class", "sourcename": "main.java", "isinterface": false, "isclass": true, "accessmodifiers": ["final"], "superclassname": "java/lang/object", "implementedinterfaces": ["java/util/concurrent/callable"], "jretargetversion": "51.0", "fields": ["val$foo"], "fieldmodifiers": { "val$foo": ["final"] }, "methodinformationmap": {}, "interface": false, "class": true },

but want find out more original object foo. example, know has info in 1 of fields:

{ "byteoffset": 37, "constantpoolindex": null, "opcode": 18, "opcodename": "ldc", "type": null, "tagname": "string", "tagvalue": "node-1" },

how jvm know val$foo points to?

you need little more context track value jvm stores foo.

assuming foo local variable

new foo(1,2); invoked the result, value of reference instance, copied , stored in local variable foo ...stuff... the anonymous class constructor invoked create new instance as part of constructor, re-create of value of local variable foo retrieved , pushed on stack that value popped stack , assigned val$foo field of anonymous class (this closing on variable) ...stuff... when foo.something() invoked, jvm retrieves value of field val$foo of anonymous class instance the jvm dereferences value object , invokes method on it

java opcode javap

No comments:

Post a Comment