Search code examples
javajavac

Why did the target type for invokevirtual change from javac target 1.1 to 1.2?


I know these are ancient Java versions but I am still curious. Given the following snippet:

public void test(java.awt.event.MouseEvent e)
{
    System.out.println(e.getID());
}

When compiled with javac -source 1.3 -target 1.1 this generates the following:

public void test(java.awt.event.MouseEvent);
 Code:
   0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
   3: aload_1       
   4: invokevirtual #3                  // Method java/awt/AWTEvent.getID:()I
   7: invokevirtual #4                  // Method java/io/PrintStream.println:(I)V
  10: return 

When compiled with javac -source 1.3 -target 1.2 this generates the following:

public void test(java.awt.event.MouseEvent);
 Code:
   0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
   3: aload_1       
   4: invokevirtual #3                  // Method java/awt/event/MouseEvent.getID:()I
   7: invokevirtual #4                  // Method java/io/PrintStream.println:(I)V
  10: return

Why did the target type for invokevirtual (see line 4) change from target 1.1 to target 1.2 ?


Solution

  • After some research, it looks like this change was introduced in javac as of Java 1.2 in order to comply with the JLS binary compatibility rules. The following quote comes directly from the javac source code:

    Beginning with -target 1.2 we obey the JLS rules for binary compatibility, emitting as the qualifying type of a reference to a method or field the type of the qualifier. In earlier targets we use as the qualifying type the class in which the member was found.