Bug 78305 - Pasting Python code into default class causes Java AST creation error.
Summary: Pasting Python code into default class causes Java AST creation error.
Status: VERIFIED DUPLICATE of bug 78275
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1 M4   Edit
Assignee: David Audel CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-10 11:27 EST by Leo Yamamoto CLA
Modified: 2004-12-14 10:45 EST (History)
0 users

See Also:


Attachments
request.py from MoinMoin wiki v1.2.4 (42.99 KB, text/plain)
2004-11-10 11:28 EST, Leo Yamamoto CLA
no flags Details
Test case (43.08 KB, text/plain)
2004-11-10 17:33 EST, Olivier Thomann CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Leo Yamamoto CLA 2004-11-10 11:27:34 EST
Steps to reproduce:
1) Create a new class using the New Java Class toolbar option. It doesn't matter
what name it is or what package it's in. Be sure to check the "public static
void main(String[] args)" checkbox.

2) After the class comes up in the editor, hit enter a few times to make some
room at the top of the file.

3) Paste in a big chunk of Python. Not all chunks cause this problem, but I can
get repeatable results using request.py from the MoinMoin wiki distribution
(v1.2.4), available at http://moinmoin.wikiwikiweb.de/MoinMoinDownload. Save the
file to force compilation.

4) Eclipse pops up an error dialog that reads, "An internal error occurred
during: "Java AST creation". The .log contains:


!ENTRY org.eclipse.core.runtime 4 2 2004-11-10 10:02:31.340
!MESSAGE An internal error occurred during: "Java AST creation".
!STACK 0
java.lang.IllegalArgumentException
	at org.eclipse.jdt.core.dom.ASTNode.setSourceRange(ASTNode.java:2583)
	at
org.eclipse.jdt.core.dom.ASTConverter.checkAndAddMultipleFieldDeclaration(ASTConverter.java:497)
	at
org.eclipse.jdt.core.dom.ASTConverter.buildBodyDeclarations(ASTConverter.java:442)
	at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:2545)
	at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:1295)
	at
org.eclipse.jdt.core.dom.CompilationUnitResolver.convert(CompilationUnitResolver.java:219)
	at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:743)
	at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:574)
	at
org.eclipse.jdt.internal.ui.javaeditor.ASTProvider.createAST(ASTProvider.java:563)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider.getAST(ASTProvider.java:492)
	at
org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup.calculateASTandInform(SelectionListenerWithASTManager.java:165)
	at
org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$3.run(SelectionListenerWithASTManager.java:142)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:66)

!ENTRY org.eclipse.ui 4 4 2004-11-10 10:02:32.215
!MESSAGE Unhandled event loop exception

!ENTRY org.eclipse.ui 4 0 2004-11-10 10:02:32.231
!MESSAGE Failed to execute runnable (java.lang.NullPointerException)
!STACK 0
org.eclipse.swt.SWTException: Failed to execute runnable
(java.lang.NullPointerException)
	at org.eclipse.swt.SWT.error(SWT.java:2697)
	at org.eclipse.swt.SWT.error(SWT.java:2622)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:121)
	at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:2771)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2451)
	at org.eclipse.jface.window.Window.runEventLoop(Window.java:718)
	at org.eclipse.jface.window.Window.open(Window.java:696)
	at org.eclipse.jface.dialogs.ErrorDialog.open(ErrorDialog.java:274)
	at
org.eclipse.ui.internal.progress.ErrorNotificationManager.openErrorDialog(ErrorNotificationManager.java:153)
	at
org.eclipse.ui.internal.progress.ErrorNotificationManager.access$2(ErrorNotificationManager.java:139)
	at
org.eclipse.ui.internal.progress.ErrorNotificationManager$2.runInUIThread(ErrorNotificationManager.java:113)
	at org.eclipse.ui.progress.UIJob$1.run(UIJob.java:96)
	at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:118)
	at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:2771)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2451)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1529)
	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1500)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:276)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:144)
	at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:102)
	at
org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:335)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:273)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:129)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:324)
	at org.eclipse.core.launcher.Main.basicRun(Main.java:185)
	at org.eclipse.core.launcher.Main.run(Main.java:704)
	at org.eclipse.core.launcher.Main.main(Main.java:688)

!ENTRY org.eclipse.ui 4 4 2004-11-10 10:02:32.231
!MESSAGE *** SWT nested exception



The workaround is to ignore the dialog and edit the file behind it (since the
error dialog is not modal) and remove the offending Python code.
Comment 1 Leo Yamamoto CLA 2004-11-10 11:28:45 EST
Created attachment 15778 [details]
request.py from MoinMoin wiki v1.2.4

This file is a sort of "exploit" for this bug.
Comment 2 Olivier Thomann CLA 2004-11-10 17:31:32 EST
David, this is due to the initializer inserted in the
RecoveredUnit.add(AbstractMethodDeclaration methodDeclaration, int
bracketBalanceValue). I don't understand why we create this initializer in case
start is 0. This means that the current type has not yet been closed.
Moreover when we create the initializer we do:
			initializer.declarationSourceStart = end;
			initializer.declarationSourceEnd = start;
This looks suspicious. Its declarationSourceStart is greater than the
corresponding end.
I will attach a test case.
Could you please have a look?

If you replace that method with:
public RecoveredElement add(AbstractMethodDeclaration methodDeclaration, int
bracketBalanceValue) {

	/* attach it to last type - if any */
	if (this.typeCount > 0){
		RecoveredType type = this.types[this.typeCount -1];
		int start = type.bodyEnd;
		int end = type.typeDeclaration.bodyEnd;
		type.bodyEnd = 0; // reset position
		type.typeDeclaration.declarationSourceEnd = 0; // reset position
		type.typeDeclaration.bodyEnd = 0;
		
		if(start != 0 && start < end) {
			Initializer initializer = new Initializer(new Block(0), 0);
			initializer.bodyStart = end;
			initializer.bodyEnd = end;
			initializer.declarationSourceStart = end;
			initializer.declarationSourceEnd = start;
			type.add(initializer, bracketBalanceValue);
		}
		
		return type.add(methodDeclaration, bracketBalanceValue);
	}
	return this; // ignore
}

It fixes the problem. I tried to fix the positions:
public RecoveredElement add(AbstractMethodDeclaration methodDeclaration, int
bracketBalanceValue) {

	/* attach it to last type - if any */
	if (this.typeCount > 0){
		RecoveredType type = this.types[this.typeCount -1];
		int start = type.bodyEnd;
		int end = type.typeDeclaration.bodyEnd;
		type.bodyEnd = 0; // reset position
		type.typeDeclaration.declarationSourceEnd = 0; // reset position
		type.typeDeclaration.bodyEnd = 0;
		
		if(start != 0 && start < end) {
			Initializer initializer = new Initializer(new Block(0), 0);
			initializer.bodyStart = end;
			initializer.bodyEnd = end;
			initializer.declarationSourceStart = start;
			initializer.declarationSourceEnd = end;
			type.add(initializer, bracketBalanceValue);
		}
		
		return type.add(methodDeclaration, bracketBalanceValue);
	}
	return this; // ignore
}

but in this case, I broke tests in SourceElementParserTest and
ComplianceDiagnoseTest.
Comment 3 Olivier Thomann CLA 2004-11-10 17:33:20 EST
Created attachment 15786 [details]
Test case

I added this tests in ASTConverterTestAST3_2 as test0574.

	/**
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=78305
	 */
	public void test0574() throws JavaModelException {
		ICompilationUnit sourceUnit = getCompilationUnit("Converter",
"src", "test0574", "X.java"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
//$NON-NLS-4$
		ASTNode result = runConversion(AST.JLS3, sourceUnit, true);
		assertEquals("not a compilation unit",
ASTNode.COMPILATION_UNIT, result.getNodeType()); //$NON-NLS-1$
	}

I put the test case in the corresponding folder in the workspace resources.
Comment 4 David Audel CLA 2004-11-16 09:52:39 EST

*** This bug has been marked as a duplicate of 78275 ***
Comment 5 Frederic Fusier CLA 2004-12-14 10:45:10 EST
Verified for 3.1 M4 using build I200412140800.