Example usage for org.eclipse.jdt.core.dom.rewrite ListRewrite getRewrittenList

List of usage examples for org.eclipse.jdt.core.dom.rewrite ListRewrite getRewrittenList

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom.rewrite ListRewrite getRewrittenList.

Prototype

public List getRewrittenList() 

Source Link

Document

Returns the nodes in the revised list property managed by this rewriter.

Usage

From source file:de.ovgu.cide.configuration.jdt.DeleteHiddenNodesVisitor.java

License:Open Source License

private void rewriteListChild(ASTNode node, ASTNode parent, ChildListPropertyDescriptor prop,
        List<ASTNode> replacements) {
    ListRewrite statementsListRewrite = getRewriteList(parent, prop);
    int position = statementsListRewrite.getRewrittenList().indexOf(node);
    statementsListRewrite.remove(node, null);
    // replacements?
    if (replacements.size() > 0) {
        boolean parentBlock = parent instanceof Block;
        for (ASTNode repl : replacements) {
            if (!parentBlock) {
                statementsListRewrite.insertAt(move(repl), ++position, null);
            } else {
                for (ASTNode s : resolveBlock(repl))
                    statementsListRewrite.insertAt(move(s), ++position, null);
            }//from  w w w . j a  v  a2 s  .  co  m
        }
    }

}

From source file:de.ovgu.cide.configuration.jdt.DeleteHiddenNodesVisitor.java

License:Open Source License

private List<ASTNode> resolveBlock(ASTNode replacement) {
    if (replacement instanceof Block) {
        ListRewrite rewrittenBlock = getRewriteList(replacement, Block.STATEMENTS_PROPERTY);
        List l = rewrittenBlock.getRewrittenList();
        if (replacement.getStartPosition() == -1)
            return new ArrayList<ASTNode>();// TODO debugging only
        return l;

    }/*from www . ja v  a 2 s  . c  o m*/
    return Collections.singletonList(replacement);
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.util.ASTUtil.java

License:Open Source License

private static void addImports(ListRewrite importRewrite, Comparator<? super ImportDeclaration> comparator,
        Iterator<ImportDeclaration> newImports) {
    try {/* w  w w. j av  a2 s  .  c o m*/
        ImportDeclaration newImport = newImports.next();
        List<?> imports = importRewrite.getRewrittenList();
        for (Object importObj : imports) {
            ImportDeclaration anImport = (ImportDeclaration) importObj;
            int comp = comparator.compare(newImport, anImport);
            if (comp > 0) {
                continue;
            }
            if (comp < 0) {
                importRewrite.insertBefore(newImport, anImport, null);
            }
            newImport = newImports.next();
        }
        importRewrite.insertLast(newImport, null);
        while (newImports.hasNext()) {
            importRewrite.insertLast(newImports.next(), null);
        }
    } catch (NoSuchElementException e) {
        // do nothing
    }
}

From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java

License:Open Source License

/**
 * Updates the constructor call.// w w w . j  a  v a  2  s .  c  o m
 *
 * @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.eclipse.emf.codegen.merge.java.facade.ast.ASTJAbstractType.java

License:Open Source License

protected List<JNode> getMembers() {
    List<JNode> members = new ArrayList<JNode>();
    ListRewrite listRewrite = rewriter.getListRewrite(getASTNode(), getASTNode().getBodyDeclarationsProperty());

    @SuppressWarnings("unchecked")
    List<BodyDeclaration> bodyDeclarations = listRewrite.getRewrittenList();

    for (BodyDeclaration declaration : bodyDeclarations) {
        // for field declarations use variable declaration fragments instead
        if (declaration instanceof FieldDeclaration) {
            FieldDeclaration fieldDeclaration = (FieldDeclaration) declaration;
            ListRewrite fragmentslistRewrite = rewriter.getListRewrite(fieldDeclaration,
                    FieldDeclaration.FRAGMENTS_PROPERTY);
            List<?> fragments = fragmentslistRewrite.getRewrittenList();
            for (Object fragment : fragments) {
                JNode node = getFacadeHelper().convertToNode(fragment);
                if (node != null) {
                    members.add(node);// www. ja va 2 s. c om
                }
            }
        } else {
            JNode node = getFacadeHelper().convertToNode(declaration);
            if (node != null) {
                members.add(node);
            }
        }
    }
    return members;
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJCompilationUnit.java

License:Open Source License

/**
 * Returns the list of children in order: package declaration (<code>JPackage</code>), imports (<code>JImport</code>), and types (<code>JType</code>).
 * //from w ww.j  a v  a2 s  .  c  om
 * @see org.eclipse.emf.codegen.merge.java.facade.AbstractJNode#getChildren()
 */
@Override
public List<JNode> getChildren() {
    if (!isDisposed()) {
        CompilationUnit astCompilationUnit = getASTNode();
        List<JNode> children = new ArrayList<JNode>();
        PackageDeclaration astPackage = astCompilationUnit.getPackage();
        if (astPackage != null) {
            JNode child = getFacadeHelper().convertToNode(astPackage);
            if (child != null) {
                children.add(child);
            }
        }

        ListRewrite importsListRewrite = rewriter.getListRewrite(astCompilationUnit,
                CompilationUnit.IMPORTS_PROPERTY);
        for (Object importDeclaration : importsListRewrite.getRewrittenList()) {
            JNode child = getFacadeHelper().convertToNode(importDeclaration);
            if (child != null) {
                children.add(child);
            }
        }

        ListRewrite typesListRewrite = rewriter.getListRewrite(astCompilationUnit,
                CompilationUnit.TYPES_PROPERTY);
        for (Object type : typesListRewrite.getRewrittenList()) {
            JNode child = getFacadeHelper().convertToNode(type);
            if (child != null) {
                children.add(child);
            }
        }
        if (!children.isEmpty()) {
            return Collections.unmodifiableList(children);
        }
    }
    return Collections.emptyList();
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJEnum.java

License:Open Source License

protected List<JNode> getEnumConstants() {
    List<JNode> constants = new ArrayList<JNode>();
    ListRewrite listRewrite = rewriter.getListRewrite(getASTNode(), EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
    for (Object enumConstant : listRewrite.getRewrittenList()) {
        JNode node = getFacadeHelper().convertToNode(enumConstant);
        if (node != null) {
            constants.add(node);/*  w  ww  . j a  v a2 s. c  om*/
        }
    }
    return constants;
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJField.java

License:Open Source License

/**
 * Ensures that the field wrapped by this ASTJField have only 1 variable in the declaration.
 * <p>//from  ww w.  ja v a  2  s  .  co  m
 * If required, this method creates a new field declaration, and sets rewritten AST node
 * to it. The ASTNode of all annotations of the new field is updated to use the original node.
 * <p>
 * Note that the no changes are added to the rewriter or wrapped object until {@link #performSplit()} is called.
 * <p>
 * This method must be called when field is created to ensure that annotations are unique for each <code>ASTJField</code>.
 * 
 * @see #performSplit()
 */
protected void prepareSplit() {
    // check number of fragments
    ListRewrite listRewrite = rewriter.getListRewrite(originalFieldDeclaration,
            FieldDeclaration.FRAGMENTS_PROPERTY);
    List<?> fragments = listRewrite.getRewrittenList();
    if (splitPerformed || fragments.size() <= 1) {
        splitPerformed = true;
        return;
    }

    // create a copy of declaration
    FieldDeclaration newDeclaration = (FieldDeclaration) ASTNode.copySubtree(originalFieldDeclaration.getAST(),
            originalFieldDeclaration);

    // set original node of annotations (to allow get methods to work correctly)
    @SuppressWarnings("unchecked")
    Iterator<IExtendedModifier> newModifiersIterator = newDeclaration.modifiers().iterator();
    @SuppressWarnings("unchecked")
    Iterator<IExtendedModifier> originalModifiersIterator = originalFieldDeclaration.modifiers().iterator();

    for (; newModifiersIterator.hasNext() && originalModifiersIterator.hasNext();) {
        IExtendedModifier modifier = newModifiersIterator.next();
        IExtendedModifier originalModifier = originalModifiersIterator.next();
        if (originalModifier.isAnnotation()) {
            Annotation annotation = (Annotation) modifier;
            Annotation originalAnnotation = (Annotation) originalModifier;
            ASTJAnnotation astjAnnotation = (ASTJAnnotation) getFacadeHelper().convertToNode(annotation);
            astjAnnotation.setRewriter(getRewriter());
            astjAnnotation.setASTNode(originalAnnotation);
        }
    }

    // new declaration will not have fragments until performSplit() is called
    newDeclaration.fragments().clear();
    setASTNode(newDeclaration);
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJField.java

License:Open Source License

/**
 * If required, separates the variable declaration fragment into a new {@link FieldDeclaration}
 * object. If this declaration does not need to be split, reverts the changes made by {@link #prepareSplit()}.
 * <p>/*from w  ww  .j  a va 2s.c o m*/
 * New field declaration will have only one variable declaration fragment. 
 * New declaration is added to the {@link ASTRewrite}.
 * The attributes of this ASTJField are updated to reference elements of the new declaration.
 * Only the javadoc, variable initializer, and annotations are copied as String, all other attributes are copied
 * using {@link ASTNode#copySubtree(org.eclipse.jdt.core.dom.AST, ASTNode)}. All
 * formatting except for Javadoc, initializer, and annotations is lost.
 * If field declaration wrapped by ASTJField has only one variable declaration
 * fragment left, no changes are made.
 * <p>
 * Note that this method must be called after {@link #prepareSplit()} and before any
 * changes are made to the nodes.
 * 
 * @see #prepareSplit()
 */
protected void performSplit() {
    if (!splitPerformed && getASTNode() != originalFieldDeclaration) {
        ListRewrite listRewrite = rewriter.getListRewrite(originalFieldDeclaration,
                FieldDeclaration.FRAGMENTS_PROPERTY);
        List<?> fragments = listRewrite.getRewrittenList();

        // perform split if there is more than 1 fragment
        if (fragments.size() > 1) {
            FieldDeclaration newDeclaration = getASTNode();

            // set javadoc
            if (originalFieldDeclaration.getJavadoc() != null) {
                String javadocString = getFacadeHelper()
                        .applyFormatRules(getFacadeHelper().toString(originalFieldDeclaration.getJavadoc()));
                setTrackedNodeProperty(newDeclaration, javadocString, newDeclaration.getJavadocProperty(),
                        ASTNode.JAVADOC);
            }

            // set initializer
            if (variableDeclarationFragment.getInitializer() != null) {
                if (initializer == UNITIALIZED_STRING) {
                    initializer = getFacadeHelper().applyFormatRules(
                            getFacadeHelper().toString(variableDeclarationFragment.getInitializer()));
                }
                setTrackedNodeProperty(variableDeclarationFragment, initializer,
                        VariableDeclarationFragment.INITIALIZER_PROPERTY, ASTNode.JAVADOC);
            }

            // set annotations contents
            @SuppressWarnings("unchecked")
            Iterator<IExtendedModifier> newModifiersIterator = newDeclaration.modifiers().iterator();
            @SuppressWarnings("unchecked")
            Iterator<IExtendedModifier> originalModifiersIterator = originalFieldDeclaration.modifiers()
                    .iterator();

            for (; newModifiersIterator.hasNext() && originalModifiersIterator.hasNext();) {
                IExtendedModifier modifier = newModifiersIterator.next();
                IExtendedModifier originalModifier = originalModifiersIterator.next();
                if (originalModifier.isAnnotation()) {
                    ASTJAnnotation astjAnnotation = (ASTJAnnotation) getFacadeHelper().convertToNode(modifier);
                    astjAnnotation.trackAndReplace(astjAnnotation.getRewrittenASTNode(),
                            getFacadeHelper().applyFormatRules(getFacadeHelper().toString(originalModifier)));
                }
            }

            // insert new declaration before this one
            listRewrite = rewriter.getListRewrite(originalFieldDeclaration.getParent(),
                    (ChildListPropertyDescriptor) originalFieldDeclaration.getLocationInParent());
            listRewrite.insertBefore(newDeclaration, originalFieldDeclaration, null);

            // update the wrapped object
            setWrappedObject(newDeclaration);

            // delete variable fragment from old declaration
            removeNodeFromListProperty(originalFieldDeclaration, variableDeclarationFragment,
                    FieldDeclaration.FRAGMENTS_PROPERTY);

            // add variable fragment to new declaration
            ListRewrite newListRewrite = rewriter.getListRewrite(newDeclaration,
                    FieldDeclaration.FRAGMENTS_PROPERTY);
            newListRewrite.insertFirst(variableDeclarationFragment, null);
        } else {
            // only 1 fragment left - revert the changes
            revertPrepareSplit();
        }
    }
    // split is performed
    splitPerformed = true;
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJMember.java

License:Open Source License

/**
 * Sets the flags of the member./*  ww w . ja v a2  s . c  o m*/
 * <p>
 * Note that <code>getFlags()</code> will not return the new value.
 * 
 * @see org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode#setFlags(int)
 */
@Override
public void setFlags(int flags) {
    BodyDeclaration bodyDeclaration = getASTNode();
    ListRewrite listRewrite = rewriter.getListRewrite(bodyDeclaration, bodyDeclaration.getModifiersProperty());

    // remove all existing modifiers
    @SuppressWarnings("unchecked")
    List<IExtendedModifier> existingModifiers = listRewrite.getRewrittenList();
    for (IExtendedModifier modifier : existingModifiers) {
        if (modifier.isModifier()) {
            listRewrite.remove((ASTNode) modifier, null);
        }
    }

    // create new modifiers and add to rewrite
    @SuppressWarnings("unchecked")
    List<Modifier> newModifiers = bodyDeclaration.getAST().newModifiers(flags);
    for (Modifier modifier : newModifiers) {
        listRewrite.insertLast(modifier, null);
    }
}