The Artima Developer Community
Sponsored Link

Java Answers Forum
javac: inconsistency in created code

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Gunter Heuckeroth

Posts: 1
Nickname: heuckeg
Registered: Jan, 2014

javac: inconsistency in created code Posted: Jan 30, 2014 12:25 AM
Reply to this message Reply
Advertisement
maybe there is a specialist for java internals (Bill?). I have a problem understanding the compiled code for the following java code

public class TestException
{
    public static void main(String[] args)
    {
        StringBuilder sb = null;
        try
        {
            sb = new StringBuilder(null);
        }
        catch (NullPointerException t)
        {
 
        }
        if (sb != null)
        {
            System.out.print(sb.toString());
        }
    }
}


This is compiled to:

stack=3, locals=3, args_size=1
 
 0: aconst_null   
 1: astore_1 
 2: new           #2                  // class java/lang/StringBuilder
 5: dup           
 6: aconst_null   
 7: invokespecial #3                  // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
10: astore_1      
11: goto          15
14: astore_2  
15: aload_1       
16: ifnull        29
19: getstatic     #5                  // Field java/lang/System.out:Ljava/io/OutStream;
22: aload_1       
23: invokevirtual #6                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
26: invokevirtual #7                  // Method java/io/OutStream.print:(Ljava/lang/String;)V
29: return
 
Exception table:
 from    to  target type
     2    11    14   Class java/lang/NullPointerException


My problem: at line 7, the stack content is

(top-2) ref to StringBuilder instance
(top-1) ref to StringBuilder instance
(top) null

After invoking StringBuilder(String), the last two arguments will be removed, so stack contains only

(top) ref to StringBuilder instance

This is ok for normal execution, but when having an exception (catch starts at address 14), stack will look like

(top-1) ref to StringBuilder instance
(top) ref to Exception instance

After storing the exception ref in local var 2, so at end of catch, there is still the ref to the stringbuilder left on stack.

Can someone point out where I go wrong?

Background:
I try to write a jvm, and this let grow the stack over it's estimated size (with side effects to data behind this stack frame...

Or do I really need to do a special handing for this case (new, dup, <init>) in the exception handling?

Thanks, Gunter

Topic: Problem in runnig java code Previous Topic   Next Topic Topic: servlet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use