Java Bytecode DUP

前端 未结 2 1598
孤街浪徒
孤街浪徒 2021-02-14 16:46

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

NEW java/lang/IllegalArgumentException
DUP
INVOKESPECIAL j         


        
2条回答
  •  甜味超标
    2021-02-14 17:21

    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 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) []

提交回复
热议问题