Bug 39839

Summary: Compiler optimization breaks user code
Product: [Eclipse Project] JDT Reporter: David Whiteman <dlwhiteman>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: svandijk
Version: 3.0   
Target Milestone: 3.0 M2   
Hardware: PC   
OS: Linux-GTK   
Whiteboard:

Description David Whiteman CLA 2003-07-09 17:16:21 EDT
I have had code for a long time that, in a static initalizer, "touches" another 
class so that its static initializer will run.  Here is an example:

public class Foo {
  static {
    Bar.class.getName();
  }
  public Foo() {
  }
  public static void main(String[] args) {
     new Foo();
  }
}

public class Bar {
  static {
    System.out.println("in bar");
  }
}

In Eclipse 2.1, running main() on Foo would have resulted in the statement "in 
bar" printing.  It appears that in the M1 build, the Bar.class.getName() has 
been optimized such that the Bar static initializer doesn't get run.  This has 
broken my code since I was relying on this behavior.  Is there some other way 
via reflection to force Bar's initialization?
Comment 1 Philipe Mulet CLA 2003-07-10 07:12:00 EDT
Actually, our old behavior was wrong (see bug 37565). The class literal should 
not force to load the target class.

Using a straight Class.forName("Bar") will do the trick. What the compiler 
instead performs is: Class.forName("[LBar;").getComponentType() so as to avoid 
initializing the class (Jikes and future Javac will presumably do it so).