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 239665 Details for
Bug 427169
[1.8][quick assist] lambda body: convert expression to block
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
WIP Patch
ChangeLambdaBodyQAs.patch (text/plain), 11.16 KB, created by
Noopur Gupta
on 2014-02-05 11:13:08 EST
(
hide
)
Description:
WIP Patch
Filename:
MIME Type:
Creator:
Noopur Gupta
Created:
2014-02-05 11:13:08 EST
Size:
11.16 KB
patch
obsolete
>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 a8b7d79..11aed76 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 >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2000, 2013 IBM Corporation and others. >+ * Copyright (c) 2000, 2014 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 >@@ -257,6 +257,8 @@ > public static String AssignToVariableAssistProposal_assigntofield_description; > public static String AssignToVariableAssistProposal_assignparamtofield_description; > public static String QuickAssistProcessor_catchclausetothrows_description; >+ public static String QuickAssistProcessor_change_lambda_body_to_block; >+ public static String QuickAssistProcessor_change_lambda_body_to_expression; > public static String QuickAssistProcessor_removecatchclause_description; > public static String QuickAssistProcessor_removeexception_description; > public static String QuickAssistProcessor_unwrap_ifstatement; >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 a02bdf7..51449e3 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 >@@ -1,5 +1,5 @@ > ############################################################################### >-# Copyright (c) 2000, 2013 IBM Corporation and others. >+# Copyright (c) 2000, 2014 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 >@@ -309,6 +309,8 @@ > AssignToVariableAssistProposal_assigntoexistingfield_description=Assign parameter to field ''{0}'' > > QuickAssistProcessor_catchclausetothrows_description=Replace catch clause with throws >+QuickAssistProcessor_change_lambda_body_to_block=Change body expression to block >+QuickAssistProcessor_change_lambda_body_to_expression=Change body block to expression > QuickAssistProcessor_removecatchclause_description=Remove catch clause > QuickAssistProcessor_removeexception_description=Remove exception > QuickAssistProcessor_name_extension_from_interface={0}Extension >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 6648d44..aa0e263 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, 2013 IBM Corporation and others. >+ * Copyright (c) 2012, 2014 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 >@@ -279,6 +279,8 @@ > public static final int NEW_TYPE= 0; > public static final int SIMILAR_VARIABLE_PROPOSAL= 0; > public static final int EXTRACT_LOCAL_ALL_ZERO_SELECTION= 0; >+ public static final int CHANGE_LAMBDA_BODY_TO_BLOCK= 0; >+ public static final int CHANGE_LAMBDA_BODY_TO_EXPRESSION= 0; > > public static final int EXTRACT_LOCAL_ZERO_SELECTION= -1; > >diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java >index 2cfcf9c..aa8e00a 100644 >--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java >+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java >@@ -87,13 +87,17 @@ > import org.eclipse.jdt.core.dom.ParameterizedType; > import org.eclipse.jdt.core.dom.ParenthesizedExpression; > import org.eclipse.jdt.core.dom.PostfixExpression; >+import org.eclipse.jdt.core.dom.PrefixExpression; >+import org.eclipse.jdt.core.dom.PrefixExpression.Operator; > import org.eclipse.jdt.core.dom.PrimitiveType; >+import org.eclipse.jdt.core.dom.ReturnStatement; > import org.eclipse.jdt.core.dom.SimpleName; > import org.eclipse.jdt.core.dom.SimpleType; > import org.eclipse.jdt.core.dom.SingleVariableDeclaration; > import org.eclipse.jdt.core.dom.Statement; > import org.eclipse.jdt.core.dom.StringLiteral; > import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; >+import org.eclipse.jdt.core.dom.SuperMethodInvocation; > import org.eclipse.jdt.core.dom.SwitchCase; > import org.eclipse.jdt.core.dom.SwitchStatement; > import org.eclipse.jdt.core.dom.SynchronizedStatement; >@@ -223,6 +227,8 @@ > || getConvertAnonymousToNestedProposal(context, coveringNode, null) > || getConvertAnonymousClassCreationsToLambdaProposals(context, coveringNode, null) > || getConvertLambdaToAnonymousClassCreationsProposals(context, coveringNode, null) >+ || getChangeLambdaBodyToBlockProposal(context, coveringNode, null) >+ || getChangeLambdaBodyToExpressionProposal(context, coveringNode, null) > || getRemoveBlockProposals(context, coveringNode, null) > || getMakeVariableDeclarationFinalProposals(context, null) > || getMissingCaseStatementProposals(context, coveringNode, null) >@@ -268,6 +274,8 @@ > getConvertAnonymousToNestedProposal(context, coveringNode, resultingCollections); > getConvertAnonymousClassCreationsToLambdaProposals(context, coveringNode, resultingCollections); > getConvertLambdaToAnonymousClassCreationsProposals(context, coveringNode, resultingCollections); >+ getChangeLambdaBodyToBlockProposal(context, coveringNode, resultingCollections); >+ getChangeLambdaBodyToExpressionProposal(context, coveringNode, resultingCollections); > if (!getConvertForLoopProposal(context, coveringNode, resultingCollections)) > getConvertIterableLoopProposal(context, coveringNode, resultingCollections); > getConvertEnhancedForLoopProposal(context, coveringNode, resultingCollections); >@@ -575,6 +583,116 @@ > return true; > } > >+ private static boolean getChangeLambdaBodyToBlockProposal(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { >+ LambdaExpression lambda; >+ if (covering instanceof LambdaExpression) { >+ lambda= (LambdaExpression) covering; >+ } else if (covering.getLocationInParent() == LambdaExpression.BODY_PROPERTY) { >+ lambda= (LambdaExpression) covering.getParent(); >+ } else { >+ return false; >+ } >+ >+ if (!(lambda.getBody() instanceof Expression)) >+ return false; >+ if (lambda.resolveMethodBinding() == null) >+ return false; >+ >+ AST ast= lambda.getAST(); >+ ASTRewrite rewrite= ASTRewrite.create(ast); >+ >+ Statement statementInBlockBody; >+ Expression bodyExpr= (Expression) rewrite.createMoveTarget(lambda.getBody()); >+ if (ast.resolveWellKnownType("void").isEqualTo(lambda.resolveMethodBinding().getReturnType())) { //$NON-NLS-1$ >+ ExpressionStatement expressionStatement= ast.newExpressionStatement(bodyExpr); >+ statementInBlockBody= expressionStatement; >+ } else { >+ ReturnStatement returnStatement= ast.newReturnStatement(); >+ returnStatement.setExpression(bodyExpr); >+ statementInBlockBody= returnStatement; >+ } >+ Block blockBody= ast.newBlock(); >+ blockBody.statements().add(statementInBlockBody); >+ >+ rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, blockBody, null); >+ >+ if (resultingCollections == null) >+ return true; >+ >+ // add proposal >+ String label= CorrectionMessages.QuickAssistProcessor_change_lambda_body_to_block; >+ Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); >+ ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_LAMBDA_BODY_TO_BLOCK, image); >+ resultingCollections.add(proposal); >+ return true; >+ } >+ >+ private static boolean getChangeLambdaBodyToExpressionProposal(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { >+ LambdaExpression lambda; >+ if (covering instanceof LambdaExpression) { >+ lambda= (LambdaExpression) covering; >+ } else if (covering.getLocationInParent() == LambdaExpression.BODY_PROPERTY) { >+ lambda= (LambdaExpression) covering.getParent(); >+ } else { >+ return false; >+ } >+ >+ if (!(lambda.getBody() instanceof Block)) >+ return false; >+ >+ Block lambdaBody= (Block) lambda.getBody(); >+ if (lambdaBody.statements().size() != 1) >+ return false; >+ >+ AST ast= lambda.getAST(); >+ ASTRewrite rewrite= ASTRewrite.create(ast); >+ >+ Expression exprBody; >+ Statement singleStatement= (Statement) lambdaBody.statements().get(0); >+ if (singleStatement instanceof ReturnStatement) { >+ Expression returnExpr= ((ReturnStatement) singleStatement).getExpression(); >+ if (returnExpr == null) >+ return false; >+ exprBody= (Expression) rewrite.createMoveTarget(returnExpr); >+ } else if (singleStatement instanceof ExpressionStatement) { >+ Expression expression= ((ExpressionStatement) singleStatement).getExpression(); >+ if (isValidExpressionBody(expression)) { >+ exprBody= (Expression) rewrite.createMoveTarget(expression); >+ } else { >+ return false; >+ } >+ } else { >+ return false; >+ } >+ >+ rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, exprBody, null); >+ >+ if (resultingCollections == null) >+ return true; >+ >+ // add proposal >+ String label= CorrectionMessages.QuickAssistProcessor_change_lambda_body_to_expression; >+ Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); >+ ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_LAMBDA_BODY_TO_EXPRESSION, image); >+ resultingCollections.add(proposal); >+ return true; >+ } >+ >+ private static boolean isValidExpressionBody(Expression expression) { >+ boolean isValidExpressionBody= expression instanceof Assignment >+ || expression instanceof ClassInstanceCreation >+ || expression instanceof MethodInvocation >+ || expression instanceof PostfixExpression >+ || expression instanceof SuperMethodInvocation; >+ if (expression instanceof PrefixExpression) { >+ Operator operator= ((PrefixExpression) expression).getOperator(); >+ if (operator == Operator.INCREMENT || operator == Operator.DECREMENT) { >+ isValidExpressionBody= true; >+ } >+ } >+ return isValidExpressionBody; >+ } >+ > public static boolean getInferDiamondArgumentsProposal(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) { > ParameterizedType createdType= null; >
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
Actions:
View
|
Diff
Attachments on
bug 427169
: 239665