package test; import java.awt.event.ActionEvent; // Bob Hanson note 11/28/16 // This test finds two bugs in j2slib: // 1) initializsers are being run twice // 2) incorrect object assignment is seen in inner classes that // extend a class that the outer object also extends. // // Report should read: // test_9 running IdealGasmodule initialization block // test_9 running anon JButton initialization block // running checkMe in JButton2 [test.Test_9$2 object] // runnable 1 // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton2 [test.Test_9$2 object] // running checkMe in JButton3 [test.Test_9$2$1$1 object] // test_9 finished anon JButton initialization block // test_9 running actionPerformed // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton2 [test.Test_9$2 object] // test_9 JButton checkMe2 // test_9 running anon JButton initialization block // running checkMe in JButton4 [test.Test_9$2 object] // runnable 1 // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton4 [test.Test_9$2 object] // running checkMe in JButton5 [test.Test_9$2$1$1 object] // test_9 finished anon JButton initialization block // running checkMe in JButton4 [test.Test_9$2 object] // running checkMe in JButton1 [test.Test_9 object] // runnable 1 // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // exit main // However, it reads the following, where the incorrect button numbers // are due to too many times running the initialization, and // the incorrect objects (marked with *) are due to some sort // of error in Clazz.prepareCallbacks. // Note that this is working properly in j2sSwingJS (11/28/16). // test_9 running IdealGasmodule static initialization block // test_9 running IdealGasmodule initialization block // test_9 running anon JButton initialization block // running checkMe in JButton0 [test.Test_9$2 object] // runnable 1 // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] //* running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton2 [test.Test_9$2$1$1 object] // test_9 finished anon JButton initialization block //[start improper duplication] // test_9 running anon JButton initialization block // running checkMe in JButton3 [test.Test_9$2 object] // runnable 1 // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton4 [test.Test_9$2$1$1 object] // test_9 finished anon JButton initialization block //[end improper duplication] // test_9 running actionPerformed // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] //* running checkMe in JButton1 [test.Test_9 object] // test_9 JButton checkMe2 // test_9 running anon JButton initialization block // running checkMe in JButton0 [test.Test_9$2 object] // runnable 1 // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] //* running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton5 [test.Test_9$2$1$1 object] // test_9 finished anon JButton initialization block //[start improper duplication] // test_9 running anon JButton initialization block // running checkMe in JButton6 [test.Test_9$2 object] // runnable 1 // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton7 [test.Test_9$2$1$1 object] // test_9 finished anon JButton initialization block //[end improper duplication] // running checkMe in JButton6 [test.Test_9$2 object] // running checkMe in JButton1 [test.Test_9 object] // runnable 1 // test_9 running IdealGasmodule resetChamber3[test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // running checkMe in JButton1 [test.Test_9 object] // exit main interface ActionListener { void actionPerformed(ActionEvent event); } class JButton { ActionListener actionListener; private static int VALUE; int jbuttonval; { jbuttonval = ++VALUE; } public void addActionListener(ActionListener actionListener) { this.actionListener = actionListener; } public void fireAction() { actionListener.actionPerformed(null); } public void checkMe() { System.out.println("running checkMe in JButton" + jbuttonval + " " + this); } } /** * To run test_4 in JavaScript, * * @author RM * */ public class Test_9 extends JButton { static { // see http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html System.out.println("test_9 running IdealGasmodule static initialization block "); } { // see http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html System.out.println("test_9 running IdealGasmodule initialization block"); } int myval = 3; void resetChamber() { System.out.println("test_9 running IdealGasmodule resetChamber" + myval + this); checkMe(); } JButton createAnonymousWithInitializer() { JButton button = new JButton() { public void checkMe2() { System.out.println("test_9 JButton checkMe2"); } { System.out.println("test_9 running anon JButton initialization block"); checkMe(); Runnable r = new Runnable() { public void run() { System.out.println("runnable 1"); resetChamber(); checkMe(); JButton j2 = new JButton() { }; j2.checkMe(); } }; r.run(); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("test_9 running actionPerformed"); resetChamber(); checkMe(); checkMe2(); } }); System.out.println("test_9 finished anon JButton initialization block"); } }; return button; } public void testMe9() { System.out.println("testing testMe"); } Runnable r = new Runnable() { public void run() { System.out.println("runnable 1"); resetChamber(); checkMe(); } }; public static void main(String[] args) { Test_9 tester = new Test_9(); tester.createAnonymousWithInitializer().fireAction(); tester.createAnonymousWithInitializer().checkMe(); tester.checkMe(); tester.r.run(); System.out.println("exit main"); } }