Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 226020 Details for
Bug 119181
[quick assist] Convert to if-return
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
Patch
Bug-119181-quick-assist-Convert-to-ifreturn.patch (text/plain), 9.38 KB, created by
Noopur Gupta
on 2013-01-24 01:48:46 EST
(
hide
)
Description:
Patch
Filename:
MIME Type:
Creator:
Noopur Gupta
Created:
2013-01-24 01:48:46 EST
Size:
9.38 KB
patch
obsolete
>diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java >index 32d5ea9..c733825 100644 >--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java >+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2000, 2012 IBM Corporation and others. >+ * Copyright (c) 2000, 2013 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at >@@ -46,6 +46,7 @@ > import org.eclipse.jdt.core.dom.ConstructorInvocation; > import org.eclipse.jdt.core.dom.ContinueStatement; > import org.eclipse.jdt.core.dom.DoStatement; >+import org.eclipse.jdt.core.dom.EnhancedForStatement; > import org.eclipse.jdt.core.dom.EnumConstantDeclaration; > import org.eclipse.jdt.core.dom.Expression; > import org.eclipse.jdt.core.dom.ExpressionStatement; >@@ -129,7 +130,8 @@ > ASTNode coveringNode= context.getCoveringNode(); > if (coveringNode != null) { > ArrayList<ASTNode> coveredNodes= getFullyCoveredNodes(context, coveringNode); >- return getInverseIfProposals(context, coveringNode, null) >+ return getConvertToIfReturnProposals(context, coveringNode, null) >+ || getInverseIfProposals(context, coveringNode, null) > || getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, null) > || getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, null) > || getInverseIfIntoContinueInLoopsProposals(context, coveringNode, null) >@@ -173,6 +175,7 @@ > getReplaceConditionalWithIfElseProposals(context, coveringNode, resultingCollections); > > if (QuickAssistProcessor.noErrorsAtLocation(locations)) { >+ getConvertToIfReturnProposals(context, coveringNode, resultingCollections); > getInverseIfProposals(context, coveringNode, resultingCollections); > getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, resultingCollections); > getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, resultingCollections); >@@ -206,6 +209,97 @@ > return null; > } > >+ private static boolean getConvertToIfReturnProposals(IInvocationContext context, ASTNode coveringNode, ArrayList<ICommandAccess> resultingCollections) { >+ if (!(coveringNode instanceof IfStatement)) { >+ return false; >+ } >+ IfStatement ifStatement= (IfStatement) coveringNode; >+ if (ifStatement.getElseStatement() != null) { >+ return false; >+ } >+ MethodDeclaration coveringMetod= ASTResolving.findParentMethodDeclaration(ifStatement); >+ if (coveringMetod == null) { >+ return false; >+ } >+ // method should return 'void' >+ Type returnType= coveringMetod.getReturnType2(); >+ if (!(returnType instanceof PrimitiveType) || ((PrimitiveType) returnType).getPrimitiveTypeCode() != PrimitiveType.VOID) { >+ return false; >+ } >+ // should be present in a block >+ if (!(ifStatement.getParent() instanceof Block)) { >+ return false; >+ } >+ // should have at least one statement in 'then' part other than 'return' >+ Statement thenStatement= ifStatement.getThenStatement(); >+ if (thenStatement instanceof ReturnStatement) { >+ return false; >+ } >+ if (thenStatement instanceof Block) { >+ List<Statement> thenStatements= ((Block) thenStatement).statements(); >+ if (thenStatements.isEmpty() || (thenStatements.size() == 1 && (thenStatements.get(0) instanceof ReturnStatement))) { >+ return false; >+ } >+ } >+ // should have no further executable statement >+ if (!isLastExecutableStatementInMethod(ifStatement)) { >+ return false; >+ } >+ // we could produce quick assist >+ if (resultingCollections == null) { >+ return true; >+ } >+ >+ AST ast= coveringNode.getAST(); >+ ASTRewrite rewrite= ASTRewrite.create(ast); >+ >+ // create inverted 'if' statement >+ Expression inversedExpression= getInversedExpression(rewrite, ifStatement.getExpression()); >+ IfStatement newIf= ast.newIfStatement(); >+ newIf.setExpression(inversedExpression); >+ newIf.setThenStatement(ast.newReturnStatement()); >+ ListRewrite listRewriter= rewrite.getListRewrite(ifStatement.getParent(), (ChildListPropertyDescriptor) ifStatement.getLocationInParent()); >+ listRewriter.replace(ifStatement, newIf, null); >+ // remove last 'return' in 'then' block >+ ArrayList<Statement> statements= getUnwrappedStatements(ifStatement.getThenStatement()); >+ Statement lastStatement= statements.get(statements.size() - 1); >+ if (lastStatement instanceof ReturnStatement) { >+ statements.remove(lastStatement); >+ } >+ // add statements from 'then' to the end of block >+ for (Statement statement : statements) { >+ listRewriter.insertLast(rewrite.createMoveTarget(statement), null); >+ } >+ >+ // add correction proposal >+ String label= CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn; >+ Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); >+ ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_TO_IF_RETURN, image); >+ resultingCollections.add(proposal); >+ return true; >+ } >+ >+ private static boolean isLastExecutableStatementInMethod(IfStatement ifStatement) { >+ ASTNode currentIfStructure= ifStatement; >+ ASTNode currentIfParent= ifStatement.getParent(); >+ while (!(currentIfParent instanceof MethodDeclaration)) { >+ // should not be in a loop >+ if (currentIfParent instanceof ForStatement || currentIfParent instanceof EnhancedForStatement >+ || currentIfParent instanceof WhileStatement || currentIfParent instanceof DoStatement) { >+ return false; >+ } >+ if (currentIfParent instanceof Block) { >+ Block ifParentBlock= (Block) currentIfParent; >+ if (ifParentBlock.statements().indexOf(currentIfStructure) != ifParentBlock.statements().size() - 1) { // not last statement in the block >+ return false; >+ } >+ } >+ currentIfStructure= currentIfParent; >+ currentIfParent= currentIfParent.getParent(); >+ } >+ return true; >+ } >+ > private static boolean getIfReturnIntoIfElseAtEndOfVoidMethodProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { > if (!(covering instanceof IfStatement)) { > return false; >diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java >index a83f608..37f325c 100644 >--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java >+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java >@@ -296,6 +296,7 @@ > public static String TypeChangeCompletionProposal_method_name; > public static String ImplementInterfaceProposal_name; > public static String AddUnimplementedMethodsOperation_AddMissingMethod_group; >+ public static String AdvancedQuickAssistProcessor_convertToIfReturn; > public static String AdvancedQuickAssistProcessor_combineSelectedStrings; > public static String AdvancedQuickAssistProcessor_convertToIfElse_description; > public static String AdvancedQuickAssistProcessor_inverseIf_description; >diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties >index 7c4652d..eb1f9cf 100644 >--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties >+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties >@@ -376,6 +376,7 @@ > ImplementInterfaceProposal_name=Let ''{0}'' implement ''{1}'' > > AddUnimplementedMethodsOperation_AddMissingMethod_group=Add missing method >+AdvancedQuickAssistProcessor_convertToIfReturn=Convert to 'if-return' > AdvancedQuickAssistProcessor_combineSelectedStrings=Combine to single String > AdvancedQuickAssistProcessor_convertToIfElse_description=Convert to 'if-else' > AdvancedQuickAssistProcessor_inverseIf_description=Invert 'if' statement >diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/IProposalRelevance.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/IProposalRelevance.java >index 6a4af83..cbc39f7 100644 >--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/IProposalRelevance.java >+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/IProposalRelevance.java >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2012 IBM Corporation and others. >+ * Copyright (c) 2012, 2013 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at >@@ -255,6 +255,7 @@ > public static final int ASSIGN_PARAM_TO_EXISTING_FIELD= 1; > public static final int INSERT_INFERRED_TYPE_ARGUMENTS_ERROR= 1; > public static final int RETURN_ALLOCATED_OBJECT_VOID= 1; >+ public static final int CONVERT_TO_IF_RETURN= 1; > > public static final int CONVERT_TO_MESSAGE_FORMAT= 0; > public static final int COPY_ANNOTATION_JAR= 0;
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Flags:
daniel_megert
:
review-
Actions:
View
|
Diff
Attachments on
bug 119181
:
226020
|
226181
|
227137
|
227138
|
227175