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

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

Introduction

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

Prototype

public static ASTNode copySubtree(AST target, ASTNode node) 

Source Link

Document

Returns a deep copy of the subtree of AST nodes rooted at the given node.

Usage

From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.AbstractStateMachineTestStrategy.java

License:Open Source License

protected MethodDeclaration cloneMethodDeclaration(MethodDeclaration declaration) {
    MethodDeclaration clone = (MethodDeclaration) ASTNode.copySubtree(declaration.getAST(), declaration);
    clone.setName(declaration.getAST().newSimpleName(declaration.getName().getIdentifier() + "_clone"));
    return clone;
}

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

License:Open Source License

/**
 * Returns a deep copy of the subtree of AST nodes rooted at the given node. The resulting nodes are owned by the
 * same AST as the given node. Even if the given node has a parent, the result node will be unparented.
 * <p>/*  w  ww.  ja va2 s  . co m*/
 * Source range information on the original nodes is automatically copied to the new nodes. Client properties (
 * <code>properties</code>) are not carried over.
 * </p>
 * <p>
 * The node's <code>AST</code> and the target <code>AST</code> must support the same API level.
 * </p>
 *
 * @param node The node to copy, or <code>null</code> if none.
 *
 * @return The copied node, or <code>null</code> if <code>node</code> is <code>null</code>
 */
@SuppressWarnings("unchecked")
public static <T extends ASTNode> T copy(final T node) {
    return (T) ASTNode.copySubtree(node.getAST(), node);
}

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

protected void setJavaDoc(Object element, Object docComment) {
    if (element instanceof BodyDeclaration) {
        BodyDeclaration node = (BodyDeclaration) element;
        ASTParser parser = ASTParser.newParser(AST.JLS4);
        parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
        parser.setSource(("/** " + docComment + "*/ int x;").toCharArray());
        TypeDeclaration type = (TypeDeclaration) parser.createAST(null);
        BodyDeclaration x = (BodyDeclaration) type.bodyDeclarations().get(0);
        Javadoc javadoc = x.getJavadoc();
        node.setJavadoc((Javadoc) ASTNode.copySubtree(node.getAST(), javadoc));
    }//from  w w  w .  j a  v  a  2s  .  c om
}

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

/**
 * Creates an {@link Expression} from the given string, owned by the given AST. 
 *//*w ww .j ava  2 s.  c  o m*/
protected Expression getExpressionFromString(AST ast, String expression) {
    if (expression == null) {
        return null;
    }
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setKind(ASTParser.K_EXPRESSION);
    parser.setSource(expression.toCharArray());
    ASTNode node = parser.createAST(null);
    return (Expression) ASTNode.copySubtree(ast, node);
}

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

/**
 * Creates a {@link Type} from the given name, owned by the given AST.
 *///  w  w  w.j  a  v a  2 s.c  o  m
protected Type getTypeFromString(AST ast, String name) {
    if (name == null) {
        return ast.newPrimitiveType(PrimitiveType.VOID);
    }
    PrimitiveType.Code primitiveTypeCode = PrimitiveType.toCode(name);
    if (primitiveTypeCode != null) {
        return ast.newPrimitiveType(primitiveTypeCode);
    }

    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setKind(ASTParser.K_STATEMENTS);
    parser.setSource((name + " a;").toCharArray());

    Block block = (Block) parser.createAST(null);
    VariableDeclarationStatement declaration = (VariableDeclarationStatement) block.statements().get(0);
    return (Type) ASTNode.copySubtree(ast, declaration.getType());
}

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAnnotationModelAdapter.java

License:Open Source License

@Override
public Iterable<?> getContainmentFeatureIterable(Object element, Object feature,
        Iterable<?> correspondingIterable) {
    if (AstCacheCodePackage.eINSTANCE.getAnnotation_Values().equals(feature)) {
        if (element instanceof NormalAnnotation) {
            return ((NormalAnnotation) element).values();
        }/*from  w  w w .j a  va 2 s.c  om*/
        if (element instanceof SingleMemberAnnotation) {
            AST ast = AST.newAST(AST.JLS4);
            MemberValuePair pair = ast.newMemberValuePair();
            Expression value = ((SingleMemberAnnotation) element).getValue();
            ASTNode newValue = ASTNode.copySubtree(ast, value);
            pair.setName(ast.newSimpleName(CodeSyncCodePlugin.SINGLE_MEMBER_ANNOTATION_VALUE_NAME));
            pair.setValue((Expression) newValue);
            return Collections.singletonList(pair);
        }
        return Collections.emptyList();
    }

    return super.getContainmentFeatureIterable(element, feature, correspondingIterable);
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaField.java

License:Open Source License

@Override
protected void setASTFeatureValue(EStructuralFeature feature, FieldDeclaration astElement, Object value)
        throws CodeSyncException {
    if (astElement == null)
        throw new IllegalArgumentException("astElement null ");
    AST ast = astElement.getAST();/*  www . j a  v a 2 s .  c  om*/
    switch (feature.getFeatureID()) {
    case UMLPackage.NAMED_ELEMENT__NAME:
        if (astElement.fragments().size() == 0)
            throw new IllegalArgumentException("bad assumption size always >0");
        if (value == null)
            throw new IllegalArgumentException("setting name to null value ");
        ((VariableDeclarationFragment) astElement.fragments().get(0))
                .setName(ast.newSimpleName((String) value));
        break;
    case UMLPackage.TYPED_ELEMENT__TYPE:
        String newType = value != null ? ((Type) value).getName() : null;
        if (newType == null)
            throw new IllegalArgumentException("Field has type null: " + astElement);
        parentForwardJavaClass_OwnedFields.parentForwardJavaType.parentForwardJavaSrcDir_Files
                .createImportDeclarationIfNeeded((Type) value);
        astElement.setType(JavaSyncUtils.getJavaTypeFromString(ast, newType, true));
        break;
    case UMLPackage.PROPERTY__DEFAULT_VALUE:
        String newDefaultValue = (LiteralString) value == null ? null : ((LiteralString) value).getValue();
        Expression e = null;
        if (newDefaultValue != null && !newDefaultValue.equals("")) {
            ASTParser expressionParser = ASTParser.newParser(AST.JLS3);
            expressionParser.setKind(ASTParser.K_EXPRESSION);
            expressionParser.setSource(newDefaultValue.toCharArray());
            e = (Expression) expressionParser.createAST(null);
            e = (Expression) ASTNode.copySubtree(ast, e);
        }
        ((VariableDeclarationFragment) astElement.fragments().get(0)).setInitializer(e);
        break;
    default:
        super.setASTFeatureValue(feature, astElement, value);
    }
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java

License:Open Source License

/**
 * Parse the given {@link String value} into a java expression.
 * /*from w ww  .  j av  a2 s . c om*/
 * @author Luiza
 */
private static Expression makeExpression(AST ast, String value) {
    ASTParser expressionParser = ASTParser.newParser(AST.JLS3);
    expressionParser.setKind(ASTParser.K_EXPRESSION);
    expressionParser.setSource(value.toCharArray());
    Expression expression = (Expression) expressionParser.createAST(null);
    return (Expression) ASTNode.copySubtree(ast, expression);
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java

License:Open Source License

/**
 * Creates a new appropriate {@link Type} from the given <code>value</code>.
 * <p>/*from w ww.j a  va 2 s.c  o  m*/
 * Note that if <code>value</code> is <code>null</code> returns
 * {@link PrimitiveType#VOID}.
 * 
 * @param ast
 *            {@link AST} of the {@link ASTNode ASTElement} needing a new
 *            Type
 * @param value
 *            the name of the {@link Type} to be created.
 * @param canBePrimitiveType
 *            if <code>true</code> try to create a primitive from the
 *            given <code>value</code> if possible, otherwise create a new
 *            Type without checking primitives
 * @author Luiza
 * 
 * @flowerModelElementId _zVs8hZiOEd6aNMdNFvR5WQ
 */
public static Type getJavaTypeFromString(AST ast, String value, boolean canBePrimitiveType) {
    if (canBePrimitiveType) {
        PrimitiveType.Code primitiveTypeCode = null;
        if (value == null)
            primitiveTypeCode = PrimitiveType.VOID;
        else {
            primitiveTypeCode = PrimitiveType.toCode(value);
        }
        if (primitiveTypeCode != null)
            return ast.newPrimitiveType(primitiveTypeCode);
    }

    // not a primitive
    ASTParser statementParser = ASTParser.newParser(AST.JLS3);
    statementParser.setKind(ASTParser.K_STATEMENTS);
    statementParser.setSource((value + " a;").toCharArray()); // try to parse a variable declaration

    Block block = (Block) statementParser.createAST(null);
    VariableDeclarationStatement declaration = (VariableDeclarationStatement) block.statements().get(0);
    return (Type) ASTNode.copySubtree(ast, declaration.getType()); // detach the type from the parent node
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java

License:Open Source License

/**
 * Creates a new block and adds it as body to the given {@link MethodDeclaration}.
 * //w  w  w .j  a va 2  s.co  m
 * @param md - the {@link MethodDeclaration} that requires a new body block.
 * @param blockString - the {@link String} that will form the block content.
 * 
 * @author Luiza
 */
public static void generateBodyForMethod(MethodDeclaration md, String blockString) {
    AST ast = md.getAST();
    ASTParser statementParser = ASTParser.newParser(AST.JLS3);
    statementParser.setKind(ASTParser.K_STATEMENTS);
    // parse the block
    statementParser.setSource(blockString.toCharArray());
    Block block = (Block) statementParser.createAST(null);
    block = (Block) ASTNode.copySubtree(ast, block);

    md.setBody(block);
}