Bug 111703 - Static initialization block in anonymous inner class causes compiler to fail
Summary: Static initialization block in anonymous inner class causes compiler to fail
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1.1   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.1.2   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-10-05 18:01 EDT by Jeramyn Feucht CLA
Modified: 2006-01-10 10:24 EST (History)
0 users

See Also:


Attachments
Most recent log file (646.72 KB, text/plain)
2005-10-05 18:01 EDT, Jeramyn Feucht CLA
no flags Details
second log file (1002.84 KB, text/plain)
2005-10-05 18:03 EDT, Jeramyn Feucht CLA
no flags Details
First log file (1001.29 KB, text/plain)
2005-10-05 18:04 EDT, Jeramyn Feucht CLA
no flags Details
Proposed fix (1.51 KB, patch)
2005-10-06 12:18 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jeramyn Feucht CLA 2005-10-05 18:01:00 EDT
Overview:
The following incorrect code causes the compiler to fail and will prevent the
class from being opened in the java editor. 

Steps to reproduce:
Create a java class called BreakEclipse in the default package
Copy paste following code

/*
Begin test file
*/
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class BreakEclipse {
    JButton myButton = new JButton();
    JTree myTree = new JTree();
    ActionListener action;

    BreakEclipse() {
        action = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (true) {
                    // unlock document
                    final Object document = new Object();

                    myButton.addActionListener(new ActionListener() {
                        private static boolean selectionChanged;
                        static TreeSelectionListener list = new
TreeSelectionListener() {
                            public void valueChanged(TreeSelectionEvent e) {
                                selectionChanged = true;
                            }
                        };

                      static {
                      myTree.addTreeSelectionListener(list);
                      }

                        public void actionPerformed(ActionEvent e) {
                            if(!selectionChanged)
                            myButton.removeActionListener(this);
                        }

                    });
                }
            }
        };
    }

    public static void main(String[] args) {
        new BreakEclipse();
    }

}
/*
End test file
*/

Actual Results:
When a save is attempted an error: Save
Failed:org.eclipse.jdt.internal.compiler.ast.TrueLiteral
Similar code in our application caused an error:
Save Failed:org.eclipse.jdt.internal.compiler.ast.MessageSend

When I restarted eclipse after getting the second error the open type did not
function, the Package explorer didn't function and the file could not be opened.

Will attach eclipse logs

Tested in eclipse 3.1 final and 3.1.1 (Build id: M20050929-0840) final releases.
 Did not test in a 3.2 release.

Expected Results:
Should show a compile error in the java editor
Comment 1 Jeramyn Feucht CLA 2005-10-05 18:01:46 EDT
Created attachment 27915 [details]
Most recent log file
Comment 2 Jeramyn Feucht CLA 2005-10-05 18:03:14 EDT
Created attachment 27916 [details]
second log file
Comment 3 Jeramyn Feucht CLA 2005-10-05 18:04:25 EDT
Created attachment 27917 [details]
First log file
Comment 4 Olivier Thomann CLA 2005-10-05 19:29:12 EDT
I will investigate.
Comment 5 Philipe Mulet CLA 2005-10-06 04:44:02 EDT
The constructor is misparsed, something like:

	BreakEclipse() {
		super();
		new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (new ActionListener() {
					private static boolean selectionChanged;

					static TreeSelectionListener list = new TreeSelectionListener() {
						public void valueChanged(TreeSelectionEvent e) {
							selectionChanged = true;
						}
					};
					static {
						myTree.addTreeSelectionListener(list);
					}

					public void actionPerformed(ActionEvent e) {
						if ((!selectionChanged))
							myButton.removeActionListener(this);
					}
				}) {
					final Object document = new Object();
					myButton.addActionListener();
				}
			}
		} = true;
	}
Comment 6 Philipe Mulet CLA 2005-10-06 04:46:16 EDT
Once addressed, pls backport fix to 3.1 maintenance branch.
Comment 7 Olivier Thomann CLA 2005-10-06 08:34:54 EDT
My guess for now is that we miss the consumption of the true literal on the way
in the SourceElementParser.
Comment 8 Olivier Thomann CLA 2005-10-06 12:18:25 EDT
I am testing a fix.
The presence of the static initializer was the cause of the failure. Now the
compiler properly reports wrong usage of static initializer inside an anonymous
class.
Comment 9 Olivier Thomann CLA 2005-10-06 12:18:50 EDT
Created attachment 27951 [details]
Proposed fix
Comment 10 Philipe Mulet CLA 2005-10-06 13:05:47 EDT
+1 for 3.1.2
Comment 11 Olivier Thomann CLA 2005-10-06 20:09:41 EDT
Fixed and released in HEAD.
Regression test added in
org.eclipse.jdt.core.tests.compiler.regression.AssignmentTest.test038.
Comment 12 Olivier Thomann CLA 2005-10-06 20:34:54 EDT
Backported to 3.1 maintenance stream.
Same regression test.
Comment 13 Maxime Daniel CLA 2006-01-10 04:45:26 EST
Verified for 3.1.2 using build M20060109-1200.
Comment 14 Frederic Fusier CLA 2006-01-10 05:24:50 EST
Verified for 3.1.2 using build M20060109-1200 (maxime)
Comment 15 Maxime Daniel CLA 2006-01-10 10:24:52 EST
Verified for 3.2 M4 using build I20051215-1506.