Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 330169 - [refactoring] Extract method throws NPE if declared lifting arg is involved
Summary: [refactoring] Extract method throws NPE if declared lifting arg is involved
Status: VERIFIED FIXED
Alias: None
Product: Objectteams
Classification: Tools
Component: OTDT (show other bugs)
Version: 0.8   Edit
Hardware: Other Linux
: P3 normal (vote)
Target Milestone: 0.8 M4   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-13 10:05 EST by Stephan Herrmann CLA
Modified: 2010-11-15 16:04 EST (History)
0 users

See Also:


Attachments
test & fix (12.15 KB, patch)
2010-11-14 10:04 EST, Stephan Herrmann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Herrmann CLA 2010-11-13 10:05:14 EST
If an extract method refactoring affects an argument with a declared
lifting type, the refactoring throws the following exception:

java.lang.NullPointerException
	at org.eclipse.jdt.internal.corext.dom.Selection.covers(Selection.java:133)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodAnalyzer.removeSelectedDeclarations(ExtractMethodAnalyzer.java:494)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodAnalyzer.computeInput(ExtractMethodAnalyzer.java:484)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodAnalyzer.analyzeSelection(ExtractMethodAnalyzer.java:357)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodAnalyzer.checkInitialConditions(ExtractMethodAnalyzer.java:207)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring.checkInitialConditions(ExtractMethodRefactoring.java:297)
	at org.eclipse.ltk.core.refactoring.CheckConditionsOperation.run(CheckConditionsOperation.java:83)



This was observed while working on
   OTQuickFixes.registerNewMethodCorrectionProposal(..)
and has been reproduced in ExtractMethodTests.testDeclaredLifting1()

Additionally, if no local variables are declared this exception may be 
thrown:

java.lang.ArrayIndexOutOfBoundsException: 1
	at org.eclipse.jdt.internal.corext.refactoring.code.flow.LocalFlowInfo.<init>(LocalFlowInfo.java:24)
	at org.eclipse.jdt.internal.corext.refactoring.code.flow.FlowAnalyzer.endVisit(FlowAnalyzer.java:766)
	at org.eclipse.jdt.core.dom.SimpleName.accept0(SimpleName.java:152)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2615)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChild(ASTNode.java:2663)
	at org.eclipse.jdt.core.dom.MethodInvocation.accept0(MethodInvocation.java:240)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2615)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChild(ASTNode.java:2663)
	at org.eclipse.jdt.core.dom.ExpressionStatement.accept0(ExpressionStatement.java:144)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2615)
	at org.eclipse.jdt.internal.corext.refactoring.code.flow.InOutFlowAnalyzer.perform(InOutFlowAnalyzer.java:40)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodAnalyzer.analyzeSelection(ExtractMethodAnalyzer.java:335)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodAnalyzer.checkInitialConditions(ExtractMethodAnalyzer.java:207)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring.checkInitialConditions(ExtractMethodRefactoring.java:297)
Comment 1 Stephan Herrmann CLA 2010-11-14 10:04:58 EST
Created attachment 183094 [details]
test & fix

Several locations didn't respect the fact that an argument with declared
lifting is mapped to two variables internally (arg + local), because the
lifting local variable declaration is marked as generated and thus not
converted to DOM AST.

Resolved by the following steps:
+ DefaultBindingResolver registers both variables, creating a faked
  SingleVariableDeclaration for the hidden local, yet binding this to
  the existing compiler binding.
+ LocalVariableIndex respects lifting args with the help of
  + new role DOMAdaptor.LocalVariableIndex
  + new method VariableBinding.getVariableIdMax()
+ JDT/UI's ASTFlattener is adapted for flattening declared lifting

Note, that the problem in LocalVariableIndex was easily masked if the 
method declares real local variables with higher IDs than the lifting arg.
Comment 2 Stephan Herrmann CLA 2010-11-14 10:07:13 EST
Committed as r1029 ff.
Comment 3 Stephan Herrmann CLA 2010-11-15 16:04:10 EST
Verified for M4 using build 201011141250.