View | Details | Raw Unified | Return to bug 119181 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java (-2 / +96 lines)
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;
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java (+1 lines)
Lines 296-301 Link Here
296
	public static String TypeChangeCompletionProposal_method_name;
296
	public static String TypeChangeCompletionProposal_method_name;
297
	public static String ImplementInterfaceProposal_name;
297
	public static String ImplementInterfaceProposal_name;
298
	public static String AddUnimplementedMethodsOperation_AddMissingMethod_group;
298
	public static String AddUnimplementedMethodsOperation_AddMissingMethod_group;
299
	public static String AdvancedQuickAssistProcessor_convertToIfReturn;
299
	public static String AdvancedQuickAssistProcessor_combineSelectedStrings;
300
	public static String AdvancedQuickAssistProcessor_combineSelectedStrings;
300
	public static String AdvancedQuickAssistProcessor_convertToIfElse_description;
301
	public static String AdvancedQuickAssistProcessor_convertToIfElse_description;
301
	public static String AdvancedQuickAssistProcessor_inverseIf_description;
302
	public static String AdvancedQuickAssistProcessor_inverseIf_description;
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties (+1 lines)
Lines 376-381 Link Here
376
ImplementInterfaceProposal_name=Let ''{0}'' implement ''{1}''
376
ImplementInterfaceProposal_name=Let ''{0}'' implement ''{1}''
377
377
378
AddUnimplementedMethodsOperation_AddMissingMethod_group=Add missing method
378
AddUnimplementedMethodsOperation_AddMissingMethod_group=Add missing method
379
AdvancedQuickAssistProcessor_convertToIfReturn=Convert to 'if-return'
379
AdvancedQuickAssistProcessor_combineSelectedStrings=Combine to single String
380
AdvancedQuickAssistProcessor_combineSelectedStrings=Combine to single String
380
AdvancedQuickAssistProcessor_convertToIfElse_description=Convert to 'if-else'
381
AdvancedQuickAssistProcessor_convertToIfElse_description=Convert to 'if-else'
381
AdvancedQuickAssistProcessor_inverseIf_description=Invert 'if' statement
382
AdvancedQuickAssistProcessor_inverseIf_description=Invert 'if' statement
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/IProposalRelevance.java (-1 / +2 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2012 IBM Corporation and others.
2
 * Copyright (c) 2012, 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 255-260 Link Here
255
	public static final int ASSIGN_PARAM_TO_EXISTING_FIELD= 1;
255
	public static final int ASSIGN_PARAM_TO_EXISTING_FIELD= 1;
256
	public static final int INSERT_INFERRED_TYPE_ARGUMENTS_ERROR= 1;
256
	public static final int INSERT_INFERRED_TYPE_ARGUMENTS_ERROR= 1;
257
	public static final int RETURN_ALLOCATED_OBJECT_VOID= 1;
257
	public static final int RETURN_ALLOCATED_OBJECT_VOID= 1;
258
	public static final int CONVERT_TO_IF_RETURN= 1;
258
259
259
	public static final int CONVERT_TO_MESSAGE_FORMAT= 0;
260
	public static final int CONVERT_TO_MESSAGE_FORMAT= 0;
260
	public static final int COPY_ANNOTATION_JAR= 0;
261
	public static final int COPY_ANNOTATION_JAR= 0;

Return to bug 119181