Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2012 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2013 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 46-51
Link Here
|
46 |
import org.eclipse.jdt.core.dom.ConstructorInvocation; |
46 |
import org.eclipse.jdt.core.dom.ConstructorInvocation; |
47 |
import org.eclipse.jdt.core.dom.ContinueStatement; |
47 |
import org.eclipse.jdt.core.dom.ContinueStatement; |
48 |
import org.eclipse.jdt.core.dom.DoStatement; |
48 |
import org.eclipse.jdt.core.dom.DoStatement; |
|
|
49 |
import org.eclipse.jdt.core.dom.EnhancedForStatement; |
49 |
import org.eclipse.jdt.core.dom.EnumConstantDeclaration; |
50 |
import org.eclipse.jdt.core.dom.EnumConstantDeclaration; |
50 |
import org.eclipse.jdt.core.dom.Expression; |
51 |
import org.eclipse.jdt.core.dom.Expression; |
51 |
import org.eclipse.jdt.core.dom.ExpressionStatement; |
52 |
import org.eclipse.jdt.core.dom.ExpressionStatement; |
Lines 129-135
Link Here
|
129 |
ASTNode coveringNode= context.getCoveringNode(); |
130 |
ASTNode coveringNode= context.getCoveringNode(); |
130 |
if (coveringNode != null) { |
131 |
if (coveringNode != null) { |
131 |
ArrayList<ASTNode> coveredNodes= getFullyCoveredNodes(context, coveringNode); |
132 |
ArrayList<ASTNode> coveredNodes= getFullyCoveredNodes(context, coveringNode); |
132 |
return getInverseIfProposals(context, coveringNode, null) |
133 |
return getConvertToIfReturnProposals(context, coveringNode, null) |
|
|
134 |
|| getInverseIfProposals(context, coveringNode, null) |
133 |
|| getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, null) |
135 |
|| getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, null) |
134 |
|| getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, null) |
136 |
|| getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, null) |
135 |
|| getInverseIfIntoContinueInLoopsProposals(context, coveringNode, null) |
137 |
|| getInverseIfIntoContinueInLoopsProposals(context, coveringNode, null) |
Lines 173-178
Link Here
|
173 |
getReplaceConditionalWithIfElseProposals(context, coveringNode, resultingCollections); |
175 |
getReplaceConditionalWithIfElseProposals(context, coveringNode, resultingCollections); |
174 |
|
176 |
|
175 |
if (QuickAssistProcessor.noErrorsAtLocation(locations)) { |
177 |
if (QuickAssistProcessor.noErrorsAtLocation(locations)) { |
|
|
178 |
getConvertToIfReturnProposals(context, coveringNode, resultingCollections); |
176 |
getInverseIfProposals(context, coveringNode, resultingCollections); |
179 |
getInverseIfProposals(context, coveringNode, resultingCollections); |
177 |
getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, resultingCollections); |
180 |
getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, resultingCollections); |
178 |
getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, resultingCollections); |
181 |
getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, resultingCollections); |
Lines 206-211
Link Here
|
206 |
return null; |
209 |
return null; |
207 |
} |
210 |
} |
208 |
|
211 |
|
|
|
212 |
private static boolean getConvertToIfReturnProposals(IInvocationContext context, ASTNode coveringNode, ArrayList<ICommandAccess> resultingCollections) { |
213 |
if (!(coveringNode instanceof IfStatement)) { |
214 |
return false; |
215 |
} |
216 |
IfStatement ifStatement= (IfStatement) coveringNode; |
217 |
if (ifStatement.getElseStatement() != null) { |
218 |
return false; |
219 |
} |
220 |
MethodDeclaration coveringMetod= ASTResolving.findParentMethodDeclaration(ifStatement); |
221 |
if (coveringMetod == null) { |
222 |
return false; |
223 |
} |
224 |
// method should return 'void' |
225 |
Type returnType= coveringMetod.getReturnType2(); |
226 |
if (!(returnType instanceof PrimitiveType) || ((PrimitiveType) returnType).getPrimitiveTypeCode() != PrimitiveType.VOID) { |
227 |
return false; |
228 |
} |
229 |
// should be present in a block |
230 |
if (!(ifStatement.getParent() instanceof Block)) { |
231 |
return false; |
232 |
} |
233 |
// should have at least one statement in 'then' part other than 'return' |
234 |
Statement thenStatement= ifStatement.getThenStatement(); |
235 |
if (thenStatement instanceof ReturnStatement) { |
236 |
return false; |
237 |
} |
238 |
if (thenStatement instanceof Block) { |
239 |
List<Statement> thenStatements= ((Block) thenStatement).statements(); |
240 |
if (thenStatements.isEmpty() || (thenStatements.size() == 1 && (thenStatements.get(0) instanceof ReturnStatement))) { |
241 |
return false; |
242 |
} |
243 |
} |
244 |
// should have no further executable statement |
245 |
if (!isLastExecutableStatementInMethod(ifStatement)) { |
246 |
return false; |
247 |
} |
248 |
// we could produce quick assist |
249 |
if (resultingCollections == null) { |
250 |
return true; |
251 |
} |
252 |
|
253 |
AST ast= coveringNode.getAST(); |
254 |
ASTRewrite rewrite= ASTRewrite.create(ast); |
255 |
|
256 |
// create inverted 'if' statement |
257 |
Expression inversedExpression= getInversedExpression(rewrite, ifStatement.getExpression()); |
258 |
IfStatement newIf= ast.newIfStatement(); |
259 |
newIf.setExpression(inversedExpression); |
260 |
newIf.setThenStatement(ast.newReturnStatement()); |
261 |
ListRewrite listRewriter= rewrite.getListRewrite(ifStatement.getParent(), (ChildListPropertyDescriptor) ifStatement.getLocationInParent()); |
262 |
listRewriter.replace(ifStatement, newIf, null); |
263 |
// remove last 'return' in 'then' block |
264 |
ArrayList<Statement> statements= getUnwrappedStatements(ifStatement.getThenStatement()); |
265 |
Statement lastStatement= statements.get(statements.size() - 1); |
266 |
if (lastStatement instanceof ReturnStatement) { |
267 |
statements.remove(lastStatement); |
268 |
} |
269 |
// add statements from 'then' to the end of block |
270 |
for (Statement statement : statements) { |
271 |
listRewriter.insertLast(rewrite.createMoveTarget(statement), null); |
272 |
} |
273 |
|
274 |
// add correction proposal |
275 |
String label= CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn; |
276 |
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); |
277 |
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_TO_IF_RETURN, image); |
278 |
resultingCollections.add(proposal); |
279 |
return true; |
280 |
} |
281 |
|
282 |
private static boolean isLastExecutableStatementInMethod(IfStatement ifStatement) { |
283 |
ASTNode currentIfStructure= ifStatement; |
284 |
ASTNode currentIfParent= ifStatement.getParent(); |
285 |
while (!(currentIfParent instanceof MethodDeclaration)) { |
286 |
// should not be in a loop |
287 |
if (currentIfParent instanceof ForStatement || currentIfParent instanceof EnhancedForStatement |
288 |
|| currentIfParent instanceof WhileStatement || currentIfParent instanceof DoStatement) { |
289 |
return false; |
290 |
} |
291 |
if (currentIfParent instanceof Block) { |
292 |
Block ifParentBlock= (Block) currentIfParent; |
293 |
if (ifParentBlock.statements().indexOf(currentIfStructure) != ifParentBlock.statements().size() - 1) { // not last statement in the block |
294 |
return false; |
295 |
} |
296 |
} |
297 |
currentIfStructure= currentIfParent; |
298 |
currentIfParent= currentIfParent.getParent(); |
299 |
} |
300 |
return true; |
301 |
} |
302 |
|
209 |
private static boolean getIfReturnIntoIfElseAtEndOfVoidMethodProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { |
303 |
private static boolean getIfReturnIntoIfElseAtEndOfVoidMethodProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { |
210 |
if (!(covering instanceof IfStatement)) { |
304 |
if (!(covering instanceof IfStatement)) { |
211 |
return false; |
305 |
return false; |