List of usage examples for org.eclipse.jdt.core.dom.rewrite ASTRewrite createMoveTarget
public final ASTNode createMoveTarget(ASTNode node)
From source file:com.github.parzonka.ccms.engine.SortElementsOperation.java
License:Open Source License
private ASTRewrite sortCompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit ast, final TextEditGroup group) { ast.accept(new ASTVisitor() { @Override/* www .j av a 2 s .com*/ public boolean visit(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit) { final List<AbstractTypeDeclaration> types = compilationUnit.types(); for (final Iterator<AbstractTypeDeclaration> iter = types.iterator(); iter.hasNext();) { final AbstractTypeDeclaration typeDeclaration = iter.next(); typeDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(typeDeclaration.getStartPosition())); compilationUnit.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(typeDeclaration))); } return true; } @Override public boolean visit(AnnotationTypeDeclaration annotationTypeDeclaration) { final List bodyDeclarations = annotationTypeDeclaration.bodyDeclarations(); for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next(); bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(bodyDeclaration.getStartPosition())); annotationTypeDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(bodyDeclaration))); } return true; } @Override public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) { final List bodyDeclarations = anonymousClassDeclaration.bodyDeclarations(); for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next(); bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(bodyDeclaration.getStartPosition())); anonymousClassDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(bodyDeclaration))); } return true; } @Override public boolean visit(TypeDeclaration typeDeclaration) { final List bodyDeclarations = typeDeclaration.bodyDeclarations(); for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next(); bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(bodyDeclaration.getStartPosition())); typeDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(bodyDeclaration))); } return true; } @Override public boolean visit(EnumDeclaration enumDeclaration) { final List bodyDeclarations = enumDeclaration.bodyDeclarations(); for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next(); bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(bodyDeclaration.getStartPosition())); enumDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(bodyDeclaration))); } final List enumConstants = enumDeclaration.enumConstants(); for (final Iterator iter = enumConstants.iterator(); iter.hasNext();) { final EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) iter.next(); enumConstantDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(enumConstantDeclaration.getStartPosition())); enumDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(enumConstantDeclaration))); } return true; } }); final ASTRewrite rewriter = ASTRewrite.create(ast.getAST()); final boolean[] hasChanges = new boolean[] { false }; ast.accept(new ASTVisitor() { /** * This * * @param elements * @param listRewrite */ private void sortElements(List<BodyDeclaration> elements, ListRewrite listRewrite) { if (elements.size() == 0) return; final List<BodyDeclaration> myCopy = new ArrayList<BodyDeclaration>(); myCopy.addAll(elements); // orderexperiment final List<Signature> methods = new ArrayList<Signature>(); for (final BodyDeclaration bd : myCopy) { if (bd.getNodeType() == ASTNode.METHOD_DECLARATION) { methods.add(new Signature((MethodDeclaration) bd)); } } Collections.sort(methods, ((BodyDeclarationComparator) SortElementsOperation.this.comparator) .getMethodDeclarationComparator()); logger.info("Sorting of methods based on appearance:"); int j = 0; for (final Signature sig : methods) { logger.info("Method [{}] : {}", ++j, sig); } Collections.sort(myCopy, SortElementsOperation.this.comparator); logger.info("Final sorting order just before the AST-Rewrite:"); for (final BodyDeclaration bd : myCopy) { if (bd.getNodeType() == ASTNode.METHOD_DECLARATION) { logger.info("{}", new Signature((MethodDeclaration) bd)); } else { logger.info("{}", bd.toString()); } } for (int i = 0; i < elements.size(); i++) { final BodyDeclaration oldNode = elements.get(i); final BodyDeclaration newNode = myCopy.get(i); if (oldNode != newNode) { if (oldNode.getNodeType() == ASTNode.METHOD_DECLARATION && newNode.getNodeType() == ASTNode.METHOD_DECLARATION) { final Signature oldMethodSignature = new Signature((MethodDeclaration) oldNode); final Signature newMethodSignature = new Signature((MethodDeclaration) newNode); logger.trace("Swapping [{}]for [{}]", oldMethodSignature, newMethodSignature); } else { logger.trace("Swapping [{}]for [{}]", oldNode.getNodeType(), newNode.getNodeType()); } listRewrite.replace(oldNode, rewriter.createMoveTarget(newNode), group); hasChanges[0] = true; } } } @Override public boolean visit(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit) { if (checkMalformedNodes(compilationUnit)) { logger.warn("Malformed nodes. Aborting sorting of current element."); return true; } sortElements(compilationUnit.types(), rewriter.getListRewrite(compilationUnit, org.eclipse.jdt.core.dom.CompilationUnit.TYPES_PROPERTY)); return true; } @Override public boolean visit(AnnotationTypeDeclaration annotationTypeDeclaration) { if (checkMalformedNodes(annotationTypeDeclaration)) { logger.warn("Malformed nodes. Aborting sorting of current element."); return true; } sortElements(annotationTypeDeclaration.bodyDeclarations(), rewriter.getListRewrite( annotationTypeDeclaration, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY)); return true; } @Override public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) { if (checkMalformedNodes(anonymousClassDeclaration)) { logger.warn("Malformed nodes. Aborting sorting of current element."); return true; } sortElements(anonymousClassDeclaration.bodyDeclarations(), rewriter.getListRewrite( anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY)); return true; } @Override public boolean visit(TypeDeclaration typeDeclaration) { if (checkMalformedNodes(typeDeclaration)) { logger.warn("Malformed nodes. Aborting sorting of current element."); return true; } sortElements(typeDeclaration.bodyDeclarations(), rewriter.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY)); return true; } @Override public boolean visit(EnumDeclaration enumDeclaration) { if (checkMalformedNodes(enumDeclaration)) { return true; // abort sorting of current element } sortElements(enumDeclaration.bodyDeclarations(), rewriter.getListRewrite(enumDeclaration, EnumDeclaration.BODY_DECLARATIONS_PROPERTY)); sortElements(enumDeclaration.enumConstants(), rewriter.getListRewrite(enumDeclaration, EnumDeclaration.ENUM_CONSTANTS_PROPERTY)); return true; } }); if (!hasChanges[0]) return null; return rewriter; }
From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateAndOddnessCheckResolution.java
License:Open Source License
/** * Creates the new <CODE>InfixExpression</CODE> <CODE>(x & 1) == 1</CODE>. *//*ww w. ja v a 2 s . co m*/ @Override protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) { Assert.isNotNull(rewrite); Assert.isNotNull(numberExpression); final AST ast = rewrite.getAST(); InfixExpression andOddnessCheck = ast.newInfixExpression(); ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression(); InfixExpression andExpression = ast.newInfixExpression(); andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression)); andExpression.setOperator(AND); andExpression.setRightOperand(ast.newNumberLiteral("1")); parenthesizedExpression.setExpression(andExpression); andOddnessCheck.setLeftOperand(parenthesizedExpression); andOddnessCheck.setOperator(EQUALS); andOddnessCheck.setRightOperand(ast.newNumberLiteral("1")); return andOddnessCheck; }
From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateRemainderOddnessCheckResolution.java
License:Open Source License
/** * Creates the new <CODE>InfixExpression</CODE> <CODE>x % 2 != 0</CODE> *///from w w w . jav a 2 s. c o m @Override protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) { Assert.isNotNull(rewrite); Assert.isNotNull(numberExpression); final AST ast = rewrite.getAST(); InfixExpression correctOddnessCheck = ast.newInfixExpression(); InfixExpression remainderExp = ast.newInfixExpression(); correctOddnessCheck.setLeftOperand(remainderExp); correctOddnessCheck.setOperator(NOT_EQUALS); correctOddnessCheck.setRightOperand(ast.newNumberLiteral("0")); remainderExp.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression)); remainderExp.setOperator(REMAINDER); remainderExp.setRightOperand(ast.newNumberLiteral("2")); return correctOddnessCheck; }
From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.UseEqualsResolution.java
License:Open Source License
private Expression createLeftOperand(ASTRewrite rewrite, Expression leftOperand) { Expression leftExp = (Expression) rewrite.createMoveTarget(leftOperand); if (leftOperand instanceof Name || leftOperand instanceof ParenthesizedExpression) { return leftExp; }//from www . j a v a 2 s . c om final AST ast = rewrite.getAST(); ParenthesizedExpression parExp = ast.newParenthesizedExpression(); parExp.setExpression(leftExp); return parExp; }
From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.UseEqualsResolution.java
License:Open Source License
private Expression createRightOperand(ASTRewrite rewrite, Expression rightOperand) { if ((rightOperand instanceof ParenthesizedExpression)) { return createRightOperand(rewrite, ((ParenthesizedExpression) rightOperand).getExpression()); }//w w w . java 2s. co m return (Expression) rewrite.createMoveTarget(rightOperand); }
From source file:edu.umd.cs.findbugs.quickfix.resolution.CreateAndOddnessCheckResolution.java
License:Open Source License
/** * Creates the new <CODE>InfixExpression</CODE> <CODE>(x & 1) == 1</CODE>. *///w w w . j a v a 2s . c o m @Override protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) { assert rewrite != null; assert numberExpression != null; final AST ast = rewrite.getAST(); InfixExpression andOddnessCheck = ast.newInfixExpression(); ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression(); InfixExpression andExpression = ast.newInfixExpression(); andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression)); andExpression.setOperator(AND); andExpression.setRightOperand(ast.newNumberLiteral("1")); parenthesizedExpression.setExpression(andExpression); andOddnessCheck.setLeftOperand(parenthesizedExpression); andOddnessCheck.setOperator(EQUALS); andOddnessCheck.setRightOperand(ast.newNumberLiteral("1")); return andOddnessCheck; }
From source file:edu.umd.cs.findbugs.quickfix.resolution.CreateRemainderOddnessCheckResolution.java
License:Open Source License
/** * Creates the new <CODE>InfixExpression</CODE> <CODE>x % 2 != 0</CODE> *//*from www . j a v a2s . co m*/ @Override protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) { assert rewrite != null; assert numberExpression != null; final AST ast = rewrite.getAST(); InfixExpression correctOddnessCheck = ast.newInfixExpression(); InfixExpression remainderExp = ast.newInfixExpression(); correctOddnessCheck.setLeftOperand(remainderExp); correctOddnessCheck.setOperator(NOT_EQUALS); correctOddnessCheck.setRightOperand(ast.newNumberLiteral("0")); remainderExp.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression)); remainderExp.setOperator(REMAINDER); remainderExp.setRightOperand(ast.newNumberLiteral("2")); return correctOddnessCheck; }
From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java
License:Open Source License
/** * Updates the constructor call./*from www . j a va 2 s.c om*/ * * @param ctorCall the ClassInstanceCreation to be marked as replaced * @param unitRewriter the AST rewriter * @param gd the edit group to use */ private void rewriteFactoryMethodCall(ClassInstanceCreation ctorCall, ASTRewrite unitRewriter, TextEditGroup gd) { AST ast = unitRewriter.getAST(); MethodInvocation factoryMethodCall = ast.newMethodInvocation(); ASTNode ctorCallParent = ctorCall.getParent(); StructuralPropertyDescriptor ctorCallLocation = ctorCall.getLocationInParent(); if (ctorCallLocation instanceof ChildListPropertyDescriptor) { ListRewrite ctorCallParentListRewrite = unitRewriter.getListRewrite(ctorCallParent, (ChildListPropertyDescriptor) ctorCallLocation); int index = ctorCallParentListRewrite.getOriginalList().indexOf(ctorCall); ctorCall = (ClassInstanceCreation) ctorCallParentListRewrite.getRewrittenList().get(index); } else { ctorCall = (ClassInstanceCreation) unitRewriter.get(ctorCallParent, ctorCallLocation); } ListRewrite actualFactoryArgs = unitRewriter.getListRewrite(factoryMethodCall, MethodInvocation.ARGUMENTS_PROPERTY); ListRewrite actualCtorArgs = unitRewriter.getListRewrite(ctorCall, ClassInstanceCreation.ARGUMENTS_PROPERTY); // Need to use a qualified name for the factory method if we're not // in the context of the class holding the factory. AbstractTypeDeclaration callOwner = (AbstractTypeDeclaration) ASTNodes.getParent(ctorCall, AbstractTypeDeclaration.class); ITypeBinding callOwnerBinding = callOwner.resolveBinding(); if (callOwnerBinding == null || !Bindings.equals(callOwner.resolveBinding(), fFactoryOwningClass.resolveBinding())) { String qualifier = fImportRewriter.addImport(fFactoryOwningClass.resolveBinding()); factoryMethodCall.setExpression(ASTNodeFactory.newName(ast, qualifier)); } factoryMethodCall.setName(ast.newSimpleName(fNewMethodName)); List<Expression> actualCtorArgsList = actualCtorArgs.getRewrittenList(); for (int i = 0; i < actualCtorArgsList.size(); i++) { Expression actualCtorArg = actualCtorArgsList.get(i); ASTNode movedArg; if (ASTNodes.isExistingNode(actualCtorArg)) { movedArg = unitRewriter.createMoveTarget(actualCtorArg); } else { unitRewriter.remove(actualCtorArg, null); movedArg = actualCtorArg; } actualFactoryArgs.insertLast(movedArg, gd); } unitRewriter.replace(ctorCall, factoryMethodCall, gd); }
From source file:org.wloka.reflectify.internal.assist.ClassInstanceCreationProposalCreator.java
License:Open Source License
@SuppressWarnings("unchecked") public IJavaCompletionProposal createProposal(ASTNode node, IInvocationContext context) { if (node == null || node.getNodeType() != ASTNode.CLASS_INSTANCE_CREATION) { throw new IllegalArgumentException(ReflectifyMessages.NoOrIllegalNodeError); }//from www. j a va2s. c o m ClassInstanceCreation cicNode = (ClassInstanceCreation) node; AST ast = cicNode.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); IMethodBinding method = cicNode.resolveConstructorBinding(); if (method == null) { throw new IllegalArgumentException(ReflectifyMessages.BindingResolutionError); } ITypeBinding[] paramTyps = method.getParameterTypes(); MethodInvocation methFor = createClassForName(cicNode.getType().resolveBinding(), ast); MethodInvocation methNew = ast.newMethodInvocation(); methNew.setName(ast.newSimpleName("newInstance")); //$NON-NLS-1$ if (paramTyps == null || paramTyps.length == 0) { methNew.setExpression(methFor); } else { MethodInvocation methGet = createGetConstructorMethod(paramTyps, ast); methGet.setExpression(methFor); List<Expression> paramVars = cicNode.arguments(); for (Expression curParam : paramVars) { methNew.arguments().add(rewrite.createMoveTarget(curParam)); } methNew.setExpression(methGet); } rewrite.replace(node, methNew, null); // no edit group return new ASTRewriteCorrectionProposal(ReflectifyMessages.ReflectifyAssistProcessor_instance, context.getCompilationUnit(), rewrite, getRelevance(), JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL)); }