Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 349862

Summary: [1.7] NPE when trying to use UnionType as TryStatement resource
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: CoreAssignee: Srikanth Sankaran <srikanth_sankaran>
Status: VERIFIED FIXED QA Contact:
Severity: major    
Priority: P3 CC: deepakazad, Olivier_Thomann, srikanth_sankaran
Version: 3.7   
Target Milestone: 3.7.1   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
Proposed patch & tests none

Description Markus Keller CLA 2011-06-20 14:52:03 EDT
BETA_JAVA7

I tried try-with-resources with a resource of a UnionType, which busted the AST:


package tryresources;

public class TryWithResources2 {
	void foo() {
		try (Object | Integer res= null) {
			
		} catch (Exception e) {
		}
	}
}


java.lang.NullPointerException
	at org.eclipse.jdt.internal.compiler.problem.ProblemReporter.resourceHasToBeAutoCloseable(ProblemReporter.java:6268)
	at org.eclipse.jdt.internal.compiler.ast.TryStatement.resolve(TryStatement.java:964)
	at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolveStatements(AbstractMethodDeclaration.java:463)
	at org.eclipse.jdt.internal.compiler.ast.MethodDeclaration.resolveStatements(MethodDeclaration.java:252)
	at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolve(AbstractMethodDeclaration.java:422)
	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1148)
	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1258)
	at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.resolve(CompilationUnitDeclaration.java:539)
	at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:1181)
	at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:681)
	at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1183)
	at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:809)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider$1.run(ASTProvider.java:548)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider.createAST(ASTProvider.java:541)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider.getAST(ASTProvider.java:484)
	at org.eclipse.jdt.ui.SharedASTProvider.getAST(SharedASTProvider.java:132)
	at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup.calculateASTandInform(SelectionListenerWithASTManager.java:170)
	at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup$3.run(SelectionListenerWithASTManager.java:155)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Comment 1 Srikanth Sankaran CLA 2011-06-20 23:27:01 EDT
I'll fix it.
Comment 2 Srikanth Sankaran CLA 2011-06-21 01:44:02 EDT
The test case below triggers the NPE against the compiler proper:

public class X {
    void foo() {
        try (Object.Integer res = null) {
        } catch (Exception e) {
        }
    }
}


Basically, we are relying on the binding to supply the textual type name to
use in the message and run into trouble when binding is null due to malformed
code.
Comment 3 Srikanth Sankaran CLA 2011-06-21 02:02:37 EDT
Created attachment 198305 [details]
Proposed patch & tests
Comment 4 Srikanth Sankaran CLA 2011-06-21 02:10:46 EDT
Released in BETA_JAVA7 branch.
Comment 5 Olivier Thomann CLA 2011-06-21 12:29:36 EDT
I think we should also make sure we don't tag the method declaration as malformed. Only the variable declaration should be malformed. More specifically, it should be its type, but since the binding for the type is null anyway, tagging the variable declaration expression looks ok.
Markus, any comment? I'll release a patch that tags the variable declaration expression as malformed and not the method declaration itself.
Comment 6 Markus Keller CLA 2011-06-21 13:37:02 EDT
(In reply to comment #5)
> I think we should also make sure we don't tag the method declaration as
> malformed. Only the variable declaration should be malformed.

I don't think we should touch that. I made a few experiments, and it looks like the current strategy is to always mark the MethodDeclaration as MALFORMED, no matter where in the body the syntax error appears.

The only place where JDT UI frequently uses this property is in the SemanticHighlightingReconciler.

Inside the declaration, the node that actually contains the syntax error is marked as RECOVERED, so clients that want to find the erroneous node can already do this now.

> [..] the binding for the type is null anyway [..]

That's only the case if bindings recovery is not enabled.
Comment 7 Olivier Thomann CLA 2011-06-21 13:39:54 EDT
ok, then I'll revert the last commit.
Comment 8 Deepak Azad CLA 2011-07-19 07:55:32 EDT
Verified with v20110714-1300.