Generated constructor returns from line number of other method

Alex Buckley alex.buckley at oracle.com
Tue Oct 21 22:52:12 UTC 2014


It's rather odd that an <init> method corresponding to a default 
constructor has a LineNumberTable attribute in the first place. After 
all, the default constructor doesn't correspond to any lines of code in 
the original source file.

The fact that <init>'s LineNumberTable attribute changes based on the 
body of another method is even more odd.

Alex

On 10/21/2014 3:19 PM, Eirik Bjørsnøs wrote:
> Hi,
>
> Given a class containing a method switching on an enum, like this:
>
> 1:  public class WeirdConstructorLinenumber
> 2:  {
> 3:     public void consider(java.math.RoundingMode mode) {
> 4:         switch ( mode ) {}
> 5:     }
> 6:  }
>
> javac generates a default constructor as expected.
>
> But the generated constructor returns from line 4 (which really belongs
> to the "consider" method).
>
> I could only make this happen by having my consider method switc on an
> enum. If I remove the switch or switch on something different, like an
> int, the constructor is generated with a single line and returns at line
> 1 as expected.
>
> Could this be a javac bug? Or is there something I'm just completely
> missing?
>
> $ javac WeirdConstructorLinenumber.java
> $ javap -c -l -classpath . WeirdConstructorLinenumber
> Compiled from "WeirdConstructorLinenumber.java"
> public class WeirdConstructorLinenumber {
>    public WeirdConstructorLinenumber();
>      Code:
>         0: aload_0
>         1: invokespecial #1                  // Method
> java/lang/Object."<init>":()V
>         4: return
>      LineNumberTable:
>        line 1: 0
>        line 4: 4
>
>     [...]
> }
>
>
> Thanks,
> Eirik Bjørsnøs
>


More information about the compiler-dev mailing list