use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class QuickAssistProcessor method getChangeLambdaBodyToExpressionProposal.
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;
Expression exprBody;
Statement singleStatement = (Statement) lambdaBody.statements().get(0);
if (singleStatement instanceof ReturnStatement) {
Expression returnExpr = ((ReturnStatement) singleStatement).getExpression();
if (returnExpr == null)
return false;
exprBody = returnExpr;
} else if (singleStatement instanceof ExpressionStatement) {
Expression expression = ((ExpressionStatement) singleStatement).getExpression();
if (isValidExpressionBody(expression)) {
exprBody = expression;
} else {
return false;
}
} else {
return false;
}
if (resultingCollections == null)
return true;
AST ast = lambda.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
Expression movedBody = (Expression) rewrite.createMoveTarget(exprBody);
rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, movedBody, null);
// 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;
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class GetterSetterCorrectionSubProcessor method addGetterProposal.
/**
* Proposes a getter for this field.
*
* @param context the proposal parameter
* @param relevance relevance of this proposal
* @return the proposal if available or null
*/
private static ChangeCorrectionProposal addGetterProposal(ProposalParameter context, int relevance) {
IMethodBinding method = findGetter(context);
if (method != null) {
Expression mi = createMethodInvocation(context, method, null);
context.astRewrite.replace(context.accessNode, mi, null);
String label = Messages.format(CorrectionMessages.GetterSetterCorrectionSubProcessor_replacewithgetter_description, BasicElementLabels.getJavaCodeString(ASTNodes.asString(context.accessNode)));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.compilationUnit, context.astRewrite, relevance, image);
return proposal;
} else {
// IJavaElement element= context.variableBinding.getJavaElement();
// if (element instanceof IField) {
// IField field= (IField) element;
// try {
// if (RefactoringAvailabilityTester.isSelfEncapsulateAvailable(field))
// return new SelfEncapsulateFieldProposal(relevance, field);
// } catch (JavaModelException e) {
// JavaPlugin.log(e);
// }
// }
}
return null;
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method createNoSideEffectProposal.
private static ASTRewriteCorrectionProposal createNoSideEffectProposal(IInvocationContext context, SimpleName nodeToQualify, IVariableBinding fieldBinding, String label, int relevance) {
AST ast = nodeToQualify.getAST();
Expression qualifier;
if (Modifier.isStatic(fieldBinding.getModifiers())) {
ITypeBinding declaringClass = fieldBinding.getDeclaringClass();
qualifier = ast.newSimpleName(declaringClass.getTypeDeclaration().getName());
} else {
qualifier = ast.newThisExpression();
}
ASTRewrite rewrite = ASTRewrite.create(ast);
FieldAccess access = ast.newFieldAccess();
access.setName((SimpleName) rewrite.createCopyTarget(nodeToQualify));
access.setExpression(qualifier);
rewrite.replace(nodeToQualify, access, null);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
return new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, relevance, image);
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method addRemoveProposal.
private static void addRemoveProposal(IInvocationContext context, ASTRewrite rewrite, String label, Collection<ICommandAccess> proposals) {
//JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, 10, image);
proposals.add(proposal);
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class ReturnTypeQuickFixTest method testReturnTypeMissingWithArrayType.
@Test
public void testReturnTypeMissingWithArrayType() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public foo() {\n");
buf.append(" return new int[][] { { 1, 2 }, { 2, 3 } };\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
CompilationUnit astRoot = getASTRoot(cu);
ArrayList proposals = collectCorrections(cu, astRoot);
assertNumberOfProposals(proposals, 2);
assertCorrectLabels(proposals);
boolean addReturnType = true, doRename = true;
for (int i = 0; i < proposals.size(); i++) {
Object elem = proposals.get(i);
if (elem instanceof ASTRewriteCorrectionProposal) {
ASTRewriteCorrectionProposal proposal = (ASTRewriteCorrectionProposal) elem;
assertTrue("duplicated entries", addReturnType);
addReturnType = false;
String preview = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public int[][] foo() {\n");
buf.append(" return new int[][] { { 1, 2 }, { 2, 3 } };\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
} else {
assertTrue("duplicated entries", doRename);
doRename = false;
CUCorrectionProposal proposal = (CUCorrectionProposal) elem;
String preview = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public E() {\n");
buf.append(" return new int[][] { { 1, 2 }, { 2, 3 } };\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
}
}
Aggregations