Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ASTRewrite#createCopyTarget()
The following examples show how to use
org.eclipse.jdt.core.dom.rewrite.ASTRewrite#createCopyTarget() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PromoteTempToFieldRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) { AST ast= getAST(); VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment(); SimpleName variableName= ast.newSimpleName(fFieldName); fragment.setName(variableName); addLinkedName(rewrite, variableName, false); List<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fTempDeclarationNode.extraDimensions(), rewrite); fragment.extraDimensions().addAll(extraDimensions); if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()){ Expression initializer= (Expression)rewrite.createCopyTarget(getTempInitializer()); fragment.setInitializer(initializer); } FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(fragment); VariableDeclarationStatement vds= getTempDeclarationStatement(); Type type= (Type)rewrite.createCopyTarget(vds.getType()); fieldDeclaration.setType(type); fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers())); return fieldDeclaration; }
Example 2
Source File: GenerateForLoopAssistProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Helper to generate an index based <code>for</code> loop to iterate over a {@link List} * implementation. * * @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for * @return an applicable {@link ASTRewrite} instance */ private ASTRewrite generateIndexBasedForRewrite(AST ast) { ASTRewrite rewrite= ASTRewrite.create(ast); ForStatement loopStatement= ast.newForStatement(); SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); //$NON-NLS-1$ loopStatement.initializers().add(getForInitializer(ast, loopVariableName)); MethodInvocation listSizeExpression= ast.newMethodInvocation(); listSizeExpression.setName(ast.newSimpleName("size")); //$NON-NLS-1$ Expression listExpression= (Expression) rewrite.createCopyTarget(fCurrentExpression); listSizeExpression.setExpression(listExpression); loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), listSizeExpression, InfixExpression.Operator.LESS)); loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier())); Block forLoopBody= ast.newBlock(); forLoopBody.statements().add(ast.newExpressionStatement(getIndexBasedForBodyAssignment(rewrite, loopVariableName))); forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite)); loopStatement.setBody(forLoopBody); rewrite.replace(fCurrentNode, loopStatement, null); return rewrite; }
Example 3
Source File: QuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static ASTNode getCopyOfInner(ASTRewrite rewrite, ASTNode statement, boolean toControlStatementBody) { if (statement.getNodeType() == ASTNode.BLOCK) { Block block= (Block) statement; List<Statement> innerStatements= block.statements(); int nStatements= innerStatements.size(); if (nStatements == 1) { return rewrite.createCopyTarget(innerStatements.get(0)); } else if (nStatements > 1) { if (toControlStatementBody) { return rewrite.createCopyTarget(block); } ListRewrite listRewrite= rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY); ASTNode first= innerStatements.get(0); ASTNode last= innerStatements.get(nStatements - 1); return listRewrite.createCopyTarget(first, last); } return null; } else { return rewrite.createCopyTarget(statement); } }
Example 4
Source File: DimensionRewrite.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Creates a {@link ASTRewrite#createCopyTarget(ASTNode) copy} of <code>type</code> * and adds <code>extraDimensions</code> to it. * * @param type the type to copy * @param extraDimensions the dimensions to add * @param rewrite the ASTRewrite with which to create new nodes * @return the copy target with added dimensions */ public static Type copyTypeAndAddDimensions(Type type, List<Dimension> extraDimensions, ASTRewrite rewrite) { AST ast= rewrite.getAST(); if (extraDimensions.isEmpty()) { return (Type) rewrite.createCopyTarget(type); } ArrayType result; if (type instanceof ArrayType) { ArrayType arrayType= (ArrayType) type; Type varElementType= (Type) rewrite.createCopyTarget(arrayType.getElementType()); result= ast.newArrayType(varElementType, 0); result.dimensions().addAll(copyDimensions(extraDimensions, rewrite)); result.dimensions().addAll(copyDimensions(arrayType.dimensions(), rewrite)); } else { Type elementType= (Type) rewrite.createCopyTarget(type); result= ast.newArrayType(elementType, 0); result.dimensions().addAll(copyDimensions(extraDimensions, rewrite)); } return result; }
Example 5
Source File: AssociativeInfixExpressionFragment.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public Expression createCopyTarget(ASTRewrite rewrite, boolean removeSurroundingParenthesis) throws JavaModelException { List<Expression> allOperands= findGroupMembersInOrderFor(fGroupRoot); if (allOperands.size() == fOperands.size()) { return (Expression) rewrite.createCopyTarget(fGroupRoot); } CompilationUnit root= (CompilationUnit) fGroupRoot.getRoot(); ICompilationUnit cu= (ICompilationUnit) root.getJavaElement(); String source= cu.getBuffer().getText(getStartPosition(), getLength()); return (Expression) rewrite.createStringPlaceholder(source, ASTNode.INFIX_EXPRESSION); // //Todo: see whether we could copy bigger chunks of the original selection // // (probably only possible from extendedOperands list or from nested InfixExpressions) // InfixExpression result= rewrite.getAST().newInfixExpression(); // result.setOperator(getOperator()); // Expression first= (Expression) fOperands.get(0); // Expression second= (Expression) fOperands.get(1); // result.setLeftOperand((Expression) rewrite.createCopyTarget(first)); // result.setRightOperand((Expression) rewrite.createCopyTarget(second)); // for (int i= 2; i < fOperands.size(); i++) { // Expression next= (Expression) fOperands.get(i); // result.extendedOperands().add(rewrite.createCopyTarget(next)); // } // return result; }
Example 6
Source File: AssociativeInfixExpressionFragment.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Override public Expression createCopyTarget(ASTRewrite rewrite, boolean removeSurroundingParenthesis) throws JavaModelException { List<Expression> allOperands = findGroupMembersInOrderFor(fGroupRoot); if (allOperands.size() == fOperands.size()) { return (Expression) rewrite.createCopyTarget(fGroupRoot); } CompilationUnit root = (CompilationUnit) fGroupRoot.getRoot(); ICompilationUnit cu = (ICompilationUnit) root.getJavaElement(); String source = cu.getBuffer().getText(getStartPosition(), getLength()); return (Expression) rewrite.createStringPlaceholder(source, ASTNode.INFIX_EXPRESSION); // //Todo: see whether we could copy bigger chunks of the original selection // // (probably only possible from extendedOperands list or from nested InfixExpressions) // InfixExpression result= rewrite.getAST().newInfixExpression(); // result.setOperator(getOperator()); // Expression first= (Expression) fOperands.get(0); // Expression second= (Expression) fOperands.get(1); // result.setLeftOperand((Expression) rewrite.createCopyTarget(first)); // result.setRightOperand((Expression) rewrite.createCopyTarget(second)); // for (int i= 2; i < fOperands.size(); i++) { // Expression next= (Expression) fOperands.get(i); // result.extendedOperands().add(rewrite.createCopyTarget(next)); // } // return result; }
Example 7
Source File: UseValueOfResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
protected MethodInvocation createValueOfInvocation(ASTRewrite rewrite, CompilationUnit compilationUnit, ClassInstanceCreation primitiveTypeCreation) { Assert.isNotNull(rewrite); Assert.isNotNull(primitiveTypeCreation); final AST ast = rewrite.getAST(); MethodInvocation valueOfInvocation = ast.newMethodInvocation(); valueOfInvocation.setName(ast.newSimpleName(VALUE_OF_METHOD_NAME)); ITypeBinding binding = primitiveTypeCreation.getType().resolveBinding(); if (isStaticImport()) { addStaticImports(rewrite, compilationUnit, binding.getQualifiedName() + "." + VALUE_OF_METHOD_NAME); } else { valueOfInvocation.setExpression(ast.newSimpleName(binding.getName())); } List<?> arguments = primitiveTypeCreation.arguments(); List<Expression> newArguments = valueOfInvocation.arguments(); for (Object argument : arguments) { Expression expression = (Expression) rewrite.createCopyTarget((ASTNode) argument); newArguments.add(expression); } return valueOfInvocation; }
Example 8
Source File: CreateDoPrivilegedBlockResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
private MethodDeclaration createRunMethodDeclaration(ASTRewrite rewrite, ClassInstanceCreation classLoaderCreation) { AST ast = rewrite.getAST(); MethodDeclaration methodDeclaration = ast.newMethodDeclaration(); SimpleName methodName = ast.newSimpleName("run"); Type returnType = (Type) rewrite.createCopyTarget(classLoaderCreation.getType()); Block methodBody = createRunMethodBody(rewrite, classLoaderCreation); List<Modifier> modifiers = checkedList(methodDeclaration.modifiers()); modifiers.add(ast.newModifier(PUBLIC_KEYWORD)); methodDeclaration.setName(methodName); methodDeclaration.setReturnType2(returnType); methodDeclaration.setBody(methodBody); return methodDeclaration; }
Example 9
Source File: CreateDoPrivilegedBlockResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
private ParameterizedType createPrivilegedActionType(ASTRewrite rewrite, ClassInstanceCreation classLoaderCreation) { AST ast = rewrite.getAST(); Name privilegedActionName; if (isUpdateImports()) { privilegedActionName = ast.newSimpleName(PrivilegedAction.class.getSimpleName()); } else { privilegedActionName = ast.newName(PrivilegedAction.class.getName()); } SimpleType rawPrivilegedActionType = ast.newSimpleType(privilegedActionName); ParameterizedType privilegedActionType = ast.newParameterizedType(rawPrivilegedActionType); Type typeArgument = (Type) rewrite.createCopyTarget(classLoaderCreation.getType()); List<Type> typeArguments = checkedList(privilegedActionType.typeArguments()); typeArguments.add(typeArgument); return privilegedActionType; }
Example 10
Source File: DimensionRewrite.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * Creates a {@link ASTRewrite#createCopyTarget(ASTNode) copy} of <code>type</code> * and adds <code>extraDimensions</code> to it. * * @param type the type to copy * @param extraDimensions the dimensions to add * @param rewrite the ASTRewrite with which to create new nodes * @return the copy target with added dimensions */ public static Type copyTypeAndAddDimensions(Type type, List<Dimension> extraDimensions, ASTRewrite rewrite) { AST ast= rewrite.getAST(); if (extraDimensions.isEmpty()) { return (Type) rewrite.createCopyTarget(type); } ArrayType result; if (type instanceof ArrayType) { ArrayType arrayType= (ArrayType) type; Type varElementType= (Type) rewrite.createCopyTarget(arrayType.getElementType()); result= ast.newArrayType(varElementType, 0); result.dimensions().addAll(copyDimensions(extraDimensions, rewrite)); result.dimensions().addAll(copyDimensions(arrayType.dimensions(), rewrite)); } else { Type elementType= (Type) rewrite.createCopyTarget(type); result= ast.newArrayType(elementType, 0); result.dimensions().addAll(copyDimensions(extraDimensions, rewrite)); } return result; }
Example 11
Source File: PromoteTempToFieldRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private Expression getTempInitializerCopy(ASTRewrite rewrite) { final Expression initializer= (Expression) rewrite.createCopyTarget(getTempInitializer()); if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(fTempDeclarationNode) > 0) { ArrayCreation arrayCreation= rewrite.getAST().newArrayCreation(); arrayCreation.setType((ArrayType) ASTNodeFactory.newType(rewrite.getAST(), fTempDeclarationNode)); arrayCreation.setInitializer((ArrayInitializer) initializer); return arrayCreation; } return initializer; }
Example 12
Source File: SimpleExpressionFragment.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public Expression createCopyTarget(ASTRewrite rewrite, boolean removeSurroundingParenthesis) { Expression node= getAssociatedExpression(); if (removeSurroundingParenthesis && node instanceof ParenthesizedExpression) { node= ((ParenthesizedExpression) node).getExpression(); } return (Expression) rewrite.createCopyTarget(node); }
Example 13
Source File: InvertBooleanUtility.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static Expression getRenamedNameCopy(SimpleNameRenameProvider provider, ASTRewrite rewrite, Expression expression) { if (provider != null) { if (expression instanceof SimpleName) { SimpleName name = (SimpleName) expression; SimpleName newName = provider.getRenamed(name); if (newName != null) { return newName; } } } return (Expression) rewrite.createCopyTarget(expression); }
Example 14
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void splitUpDeclarations(ASTRewrite rewrite, TextEditGroup group, VariableDeclarationFragment frag, VariableDeclarationStatement originalStatement, List<Expression> sideEffects) { if (sideEffects.size() > 0) { ListRewrite statementRewrite= rewrite.getListRewrite(originalStatement.getParent(), (ChildListPropertyDescriptor) originalStatement.getLocationInParent()); Statement previousStatement= originalStatement; for (int i= 0; i < sideEffects.size(); i++) { Expression sideEffect= sideEffects.get(i); Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect); ExpressionStatement wrapped= rewrite.getAST().newExpressionStatement(movedInit); statementRewrite.insertAfter(wrapped, previousStatement, group); previousStatement= wrapped; } VariableDeclarationStatement newDeclaration= null; List<VariableDeclarationFragment> fragments= originalStatement.fragments(); int fragIndex= fragments.indexOf(frag); ListIterator<VariableDeclarationFragment> fragmentIterator= fragments.listIterator(fragIndex+1); while (fragmentIterator.hasNext()) { VariableDeclarationFragment currentFragment= fragmentIterator.next(); VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment); if (newDeclaration == null) { newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment); Type copiedType= (Type) rewrite.createCopyTarget(originalStatement.getType()); newDeclaration.setType(copiedType); } else { newDeclaration.fragments().add(movedFragment); } } if (newDeclaration != null){ statementRewrite.insertAfter(newDeclaration, previousStatement, group); if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1){ rewrite.remove(originalStatement, group); } } } }
Example 15
Source File: UnresolvedElementsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression, Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes, Collection<ICommandAccess> proposals) { ITypeBinding castType= expression.getType().resolveBinding(); if (castType == null) { return false; } if (paramTypes != null) { if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) { return false; } } else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) { return false; } ITypeBinding bindingToCast= accessExpression.resolveTypeBinding(); if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) { return false; } IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes); if (res != null) { AST ast= expression.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); CastExpression newCast= ast.newCastExpression(); newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType())); newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression)); ParenthesizedExpression parents= ast.newParenthesizedExpression(); parents.setExpression(newCast); ASTNode node= rewrite.createCopyTarget(expression.getExpression()); rewrite.replace(expression, node, null); rewrite.replace(accessExpression, parents, null); String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_PARENTHESES_AROUND_CAST, image); proposals.add(proposal); return true; } return false; }
Example 16
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static Expression getRenamedNameCopy(SimpleNameRenameProvider provider, ASTRewrite rewrite, Expression expression) { if (provider != null) { if (expression instanceof SimpleName) { SimpleName name= (SimpleName) expression; SimpleName newName= provider.getRenamed(name); if (newName != null) { return newName; } } } return (Expression) rewrite.createCopyTarget(expression); }
Example 17
Source File: SimpleExpressionFragment.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Override public Expression createCopyTarget(ASTRewrite rewrite, boolean removeSurroundingParenthesis) { Expression node = getAssociatedExpression(); if (removeSurroundingParenthesis && node instanceof ParenthesizedExpression) { node = ((ParenthesizedExpression) node).getExpression(); } return (Expression) rewrite.createCopyTarget(node); }
Example 18
Source File: UnresolvedElementsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression, Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes, Collection<ChangeCorrectionProposal> proposals) { ITypeBinding castType= expression.getType().resolveBinding(); if (castType == null) { return false; } if (paramTypes != null) { if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) { return false; } } else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) { return false; } ITypeBinding bindingToCast= accessExpression.resolveTypeBinding(); if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) { return false; } IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes); if (res != null) { AST ast= expression.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); CastExpression newCast= ast.newCastExpression(); newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType())); newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression)); ParenthesizedExpression parents= ast.newParenthesizedExpression(); parents.setExpression(newCast); ASTNode node= rewrite.createCopyTarget(expression.getExpression()); rewrite.replace(expression, node, null); rewrite.replace(accessExpression, parents, null); String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description; ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.ADD_PARENTHESES_AROUND_CAST); proposals.add(proposal); return true; } return false; }
Example 19
Source File: GetterSetterUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Converts an assignment, postfix expression or prefix expression into an assignable equivalent expression using the getter. * * @param node the assignment/prefix/postfix node * @param astRewrite the astRewrite to use * @param getterExpression the expression to insert for read accesses or <code>null</code> if such an expression does not exist * @param variableType the type of the variable that the result will be assigned to * @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used * @return an expression that can be assigned to the type variableType with node being replaced by a equivalent expression using the getter */ public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) { InfixExpression.Operator op= null; AST ast= astRewrite.getAST(); if (isNotInBlock(node)) return null; if (node.getNodeType() == ASTNode.ASSIGNMENT) { Assignment assignment= ((Assignment) node); Expression rightHandSide= assignment.getRightHandSide(); Expression copiedRightOp= (Expression) astRewrite.createCopyTarget(rightHandSide); if (assignment.getOperator() == Operator.ASSIGN) { ITypeBinding rightHandSideType= rightHandSide.resolveTypeBinding(); copiedRightOp= createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher); return copiedRightOp; } if (getterExpression != null) { InfixExpression infix= ast.newInfixExpression(); infix.setLeftOperand(getterExpression); infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator())); ITypeBinding infixType= infix.resolveTypeBinding(); if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) { ParenthesizedExpression p= ast.newParenthesizedExpression(); p.setExpression(copiedRightOp); copiedRightOp= p; } infix.setRightOperand(copiedRightOp); return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher); } } else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) { PostfixExpression po= (PostfixExpression) node; if (po.getOperator() == PostfixExpression.Operator.INCREMENT) op= InfixExpression.Operator.PLUS; if (po.getOperator() == PostfixExpression.Operator.DECREMENT) op= InfixExpression.Operator.MINUS; } else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) { PrefixExpression pe= (PrefixExpression) node; if (pe.getOperator() == PrefixExpression.Operator.INCREMENT) op= InfixExpression.Operator.PLUS; if (pe.getOperator() == PrefixExpression.Operator.DECREMENT) op= InfixExpression.Operator.MINUS; } if (op != null && getterExpression != null) { return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher); } return null; }