Java Bytecode DUP

允我心安 提交于 2019-12-05 20:22:37

问题


I am wondering why the Exception in the following bytecode (used to throw an Exception) is duplicated.

NEW java/lang/IllegalArgumentException
DUP
INVOKESPECIAL java/lang/IllegalArgumentException <init> ()V
ATHROW

回答1:


I'll analyze this line by line where [] = new stack after that op is used:

  1. NEW puts a new IllegalArgumentException onto the stack [SomeIllegalArgumentException]
  2. DUP duplicates it [SomeIllegalArgumentException, SomeIllegalArgumentException]
  3. INVOKESPECIAL pops off the top one and initializes it by calling it's <init> method [SomeIllegalArgumentException] (The init method will not return the object to put back onto the stack, so the object must first be duplicated so as to keep it on the stack)
  4. ATHROW Throws the other (a duplicate off the one we initialized) []



回答2:


In byte code, an object is first created by class, and then a constructor is called on that object. The signature of a constructor ends with V for void as it does return anything. This means a copy of the original reference to the object must be kept on the stack (or in a variable) so it can be thrown after the constructor is called.

BTW The internal name for a constructor is <init> and the internal name for a static initialiser code is <clinit>



来源:https://stackoverflow.com/questions/12438567/java-bytecode-dup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!