Example usage for org.eclipse.jdt.core.dom ASTNode copySubtrees

List of usage examples for org.eclipse.jdt.core.dom ASTNode copySubtrees

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ASTNode copySubtrees.

Prototype

public static List copySubtrees(AST target, List nodes) 

Source Link

Document

Returns a deep copy of the subtrees of AST nodes rooted at the given list of nodes.

Usage

From source file:ch.acanda.eclipse.pmd.java.resolution.ASTUtil.java

License:Open Source License

/**
 * Returns a deep copy of the subtrees of AST nodes rooted at the given list of nodes. The resulting nodes are owned
 * by the given AST, which may be different from the ASTs of the nodes in the list. Even if the nodes in the list
 * have parents, the nodes in the result will be unparented.
 * <p>/*www .j av a 2 s.  com*/
 * Source range information on the original nodes is automatically copied to the new nodes. Client properties (
 * <code>properties</code>) are not carried over.
 * </p>
 *
 * @param nodes The node to copy, or <code>null</code> if none.
 *
 * @return The copied nodes, or <code>null</code> if <code>node</code> is <code>null</code>
 */
@SuppressWarnings("unchecked")
public static <T extends ASTNode> List<T> copy(final List<T> nodes) {
    if (nodes == null || nodes.isEmpty()) {
        return nodes;
    }
    return ASTNode.copySubtrees(nodes.get(0).getAST(), nodes);
}

From source file:de.ovgu.cide.export.physical.ahead.ast.JakClassRefinement.java

License:Open Source License

public void initializeFromType(TypeDeclaration type) {
    modifiers.clear();// w w  w  .j  a  va  2 s .  c  om
    modifiers.addAll(ASTNode.copySubtrees(ast, type.modifiers()));
    isInterface = type.isInterface();
    superInterfaceTypes.clear();
    superType = null;
    // superInterfaceTypes.addAll(ASTNode.copySubtrees(ast, type
    // .superInterfaceTypes()));
    typeParameters.clear();
    typeParameters.addAll(ASTNode.copySubtrees(ast, type.typeParameters()));
    name = (SimpleName) ASTNode.copySubtree(ast, type.getName());
}

From source file:de.ovgu.cide.export.physical.ahead.ast.JakClassRefinement.java

License:Open Source License

JakClassRefinement clone(AST target) {
    JakClassRefinement result = new JakClassRefinement(parent, target, options);
    // result.setSourceRange(this.getStartPosition(), this.getLength());
    result.javadoc = (this.javadoc);
    result.name = ast.newSimpleName(name.getIdentifier());
    result.isInterface = isInterface;//w  ww  . ja  v a2  s  .c  o m
    result.modifiers.addAll(ASTNode.copySubtrees(target, modifiers));
    result.typeParameters().addAll(ASTNode.copySubtrees(target, typeParameters()));
    result.superInterfaceTypes().addAll(ASTNode.copySubtrees(target, superInterfaceTypes()));
    result.superType = (Type) ASTNode.copySubtree(target, result.superType);
    result.refinements.addAll(ASTNode.copySubtrees(target, refinements));
    result.introductions.addAll(ASTNode.copySubtrees(target, introductions));
    result.fields.addAll(ASTNode.copySubtrees(target, fields));
    return result;
}

From source file:de.ovgu.cide.export.physical.ahead.ExportJavaFileJob.java

License:Open Source License

private void createFilesForInnerClasses(List<BodyDeclaration> bodyDeclarations, PackageDeclaration packageDecl,
        List<ImportDeclaration> imports, IContainer targetDirectory, String targetLayer) throws CoreException {
    List<TypeDeclaration> innerClasses = extractInnerClasses(bodyDeclarations);
    for (TypeDeclaration innerClass : innerClasses) {
        CompilationUnit container = innerClass.getAST().newCompilationUnit();
        if (packageDecl != null)
            container.setPackage((PackageDeclaration) ASTNode.copySubtree(innerClass.getAST(), packageDecl));
        container.imports().addAll(ASTNode.copySubtrees(innerClass.getAST(), imports));
        container.types().add(innerClass);
        MethodObjectHelper.stripMethodObjectAnnotation(innerClass);
        MethodObjectHelper.stripStaticModifier(innerClass.modifiers());

        createFileForBase(innerClass, container, targetDirectory, targetLayer);
    }/*from  www.  j av a  2 s  .c om*/

}

From source file:de.ovgu.cide.export.physical.ahead.JakHookMethodHelper.java

License:Open Source License

/**
 * skeleton used for hook method and its refinements. declares parameters,
 * name, return types, but not the body/*from   www  .j a v  a2s . c om*/
 * 
 * @return
 */
private MethodDeclaration createHookMethodSkeleton() {
    MethodDeclaration skeleton = ast.newMethodDeclaration();
    skeleton.setConstructor(false);
    skeleton.setName(ast.newSimpleName(name));
    skeleton.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD));
    if (isStatic)
        skeleton.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    skeleton.thrownExceptions().addAll(ASTNode.copySubtrees(ast, thrownExceptions));
    Block block = ast.newBlock();
    skeleton.setBody(block);
    for (Formal formal : parameters) {
        List<SingleVariableDeclaration> args = skeleton.parameters();
        SingleVariableDeclaration paramDecl = ast.newSingleVariableDeclaration();
        paramDecl.setName(ast.newSimpleName(formal.name));
        paramDecl.setType(copyType(formal.type));
        args.add(paramDecl);
    }

    if (returnValues.size() == 1) {
        Formal formal = returnValues.get(0);
        skeleton.setReturnType2(copyType(formal.type));
    } else if (returnValues.size() > 0) {
        assert false : "unimplemented yet";
    }
    return skeleton;
}

From source file:de.ovgu.cide.export.physical.ahead.MethodObjectHelper.java

License:Open Source License

public static TypeDeclaration moveMethodToMethodObject(MethodDeclaration method,
        RefactoringColorManager colorManager, boolean isStatic) {
    debug_numberOfMessageObjects++;//w ww .j a  va  2s  .  c om

    AST ast = method.getAST();
    assert method.getParent() instanceof TypeDeclaration;
    TypeDeclaration containingClass = (TypeDeclaration) method.getParent();

    String moName = getMethodObjectName(containingClass, method);
    // System.out.println(moName);
    boolean isSourceMethodStatic = isMethodStatic(method);
    if (isStatic && !isSourceMethodStatic)
        makeThisAccessExplicit(method);

    // create inner class and execute method
    TypeDeclaration methodObjectClass = ast.newTypeDeclaration();
    methodObjectClass.setName(ast.newSimpleName(moName));
    if (isStatic) {
        Modifier staticModifier = ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD);
        methodObjectClass.modifiers().add(staticModifier);

    }

    MethodDeclaration executeMethod = ast.newMethodDeclaration();
    executeMethod.setName(ast.newSimpleName("execute"));
    executeMethod.setReturnType2((Type) ASTNode.copySubtree(ast, method.getReturnType2()));
    executeMethod.thrownExceptions().addAll(ASTNode.copySubtrees(ast, method.thrownExceptions()));

    methodObjectClass.bodyDeclarations().add(executeMethod);
    containingClass.bodyDeclarations().add(methodObjectClass);

    createConstructor(methodObjectClass, method, isStatic && !isSourceMethodStatic);

    // move body to MO
    Block oldMethodBody = method.getBody();
    Block newMethodBody = ast.newBlock();
    method.setBody(newMethodBody);

    executeMethod.setBody(oldMethodBody);

    if (isStatic && !isSourceMethodStatic)
        changeThisAccessToField(oldMethodBody);

    // call method object
    ClassInstanceCreation moConstructorCall = ast.newClassInstanceCreation();
    moConstructorCall.setType(ast.newSimpleType(ast.newSimpleName(moName)));
    addParamters(moConstructorCall, method, isStatic && !isSourceMethodStatic);
    MethodInvocation moCall = ast.newMethodInvocation();
    moCall.setExpression(moConstructorCall);
    moCall.setName(ast.newSimpleName("execute"));
    if (RefactoringUtils.isVoid(method.getReturnType2())) {
        newMethodBody.statements().add(ast.newExpressionStatement(moCall));
    } else {
        ReturnStatement returnStmt = ast.newReturnStatement();
        returnStmt.setExpression(moCall);
        newMethodBody.statements().add(returnStmt);
    }

    // adjustColors
    colorManager.setColors(methodObjectClass, colorManager.getOwnColors(method));

    moveLocalVariableDeclarationsToFields(executeMethod, methodObjectClass, colorManager);

    addMethodObjectAnnotation(methodObjectClass);

    return methodObjectClass;
}

From source file:net.sf.eclipsecs.ui.quickfixes.blocks.AvoidNestedBlocksQuickfix.java

License:Open Source License

/**
 * {@inheritDoc}/*from  ww w  .j  a  va2 s. co  m*/
 */
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {

    return new ASTVisitor() {

        public boolean visit(Block node) {

            if (containsPosition(lineInfo, node.getStartPosition())) {

                if (node.getParent() instanceof Block) {

                    List statements = ((Block) node.getParent()).statements();
                    int index = statements.indexOf(node);

                    statements.remove(node);
                    statements.addAll(index, ASTNode.copySubtrees(node.getAST(), node.statements()));

                } else if (node.getParent() instanceof SwitchStatement) {

                    List statements = ((SwitchStatement) node.getParent()).statements();
                    int index = statements.indexOf(node);

                    statements.remove(node);
                    statements.addAll(index, ASTNode.copySubtrees(node.getAST(), node.statements()));
                }
            }
            return true;
        }
    };
}

From source file:org.autorefactor.refactoring.ASTBuilder.java

License:Open Source License

/**
 * Returns a copy of the provided {@link ASTNode} list.
 * This method loses code comments. Prefer using {@link #copyRange(List)}.
 *
 * @param <T> the actual node's type
 * @param nodes the node list to copy//from  www. j  a  v a  2s . c o  m
 * @return a copy of the node list
 */
@SuppressWarnings("unchecked")
public <T extends ASTNode> List<T> copySubtrees(List<T> nodes) {
    return ASTNode.copySubtrees(ast, nodes);
}

From source file:org.eclipse.objectteams.otdt.internal.ui.util.OTStubUtility.java

License:Open Source License

/** This method is inspired by createImplementationStub(..), but much leaner. */
public static CalloutMappingDeclaration createCallout(ICompilationUnit unit, ASTRewrite rewrite,
        ImportRewrite imports, IMethodBinding binding, String type, CodeGenerationSettings settings)
        throws CoreException {
    AST ast = rewrite.getAST();/* w ww.  ja va2  s . c  om*/

    CalloutMappingDeclaration decl = ast.newCalloutMappingDeclaration();
    decl.setSignatureFlag(true);
    // no modifiers

    MethodSpec spec = ast.newMethodSpec();
    spec.setSignatureFlag(true);
    spec.setName(ast.newSimpleName(binding.getName()));

    // no type parameters

    spec.setReturnType2(imports.addImport(binding.getReturnType(), ast));

    // this helps to reuse some code regarding methods:
    MethodDeclaration method = ast.newMethodDeclaration();

    List parameters = getDefault().createParameters(unit.getJavaProject(), imports, null, ast, binding, method);

    spec.parameters().addAll(ASTNode.copySubtrees(ast, parameters)); // copy to re-parent

    decl.setRoleMappingElement(spec);
    decl.setBaseMappingElement((MethodSpec) ASTNode.copySubtree(ast, spec));
    // no thrown exceptions

    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    // no body

    if (settings.createComments) {
        // TODO(SH): this reuses the template for overridden methods, should actually define our own template.
        String string = CodeGeneration.getMethodComment(unit, type, method, binding, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }

    // no override annotation

    return decl;

}

From source file:org.eclipse.objectteams.otdt.internal.ui.util.OTStubUtility.java

License:Open Source License

/** This method is inspired by createImplementationStub(..), but much leaner. */
public static CallinMappingDeclaration createCallin(ICompilationUnit unit, ASTRewrite rewrite,
        ImportRewrite imports, IMethodBinding binding, String type, ModifierKeyword callinModifier,
        CodeGenerationSettings settings) throws CoreException {
    AST ast = rewrite.getAST();/*from  www  .  j av  a  2s.co  m*/

    CallinMappingDeclaration decl = ast.newCallinMappingDeclaration();
    decl.setBindingOperator(ast.newMethodBindingOperator(callinModifier, MethodBindingOperator.KIND_CALLIN));
    // no regular modifiers

    MethodSpec spec = ast.newMethodSpec();
    spec.setSignatureFlag(true);
    spec.setName(ast.newSimpleName(binding.getName()));

    // no type parameters

    spec.setReturnType2(imports.addImport(binding.getReturnType(), ast));

    // this helps to reuse some code regarding methods:
    MethodDeclaration method = ast.newMethodDeclaration();

    List parameters = getDefault().createParameters(unit.getJavaProject(), imports, null, ast, binding, method);

    spec.parameters().addAll(ASTNode.copySubtrees(ast, parameters)); // copy to re-parent

    decl.setRoleMappingElement(spec);
    decl.getBaseMappingElements().add(ASTNode.copySubtree(ast, spec));
    // no thrown exceptions

    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    // no body

    if (settings.createComments) {
        // TODO(SH): this reuses the template for overridden methods, should actually define our own template.
        String string = CodeGeneration.getMethodComment(unit, type, method, binding, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }

    // no override annotation

    return decl;

}