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

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

Introduction

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

Prototype

public final StructuralPropertyDescriptor getLocationInParent() 

Source Link

Document

Returns the location of this node within its parent, or null if this is a root node.

Usage

From source file:astview.ASTViewContentProvider.java

License:Open Source License

public Object getParent(Object child) {
    if (child instanceof ASTNode) {
        ASTNode node = (ASTNode) child;
        ASTNode parent = node.getParent();
        if (parent != null) {
            StructuralPropertyDescriptor prop = node.getLocationInParent();
            return new NodeProperty(parent, prop);
        }//from w  w  w  . j a v a2s. com
    } else if (child instanceof ASTAttribute) {
        return ((ASTAttribute) child).getParent();
    }
    return null;
}

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

License:Open Source License

/**
 * Replaces a node in an AST with another node. If the replacement is successful the original node is deleted.
 *
 * @param node The node to replace./*from   ww w. j a  v a  2s . c  om*/
 * @param replacement The replacement node.
 * @return <code>true</code> if the node was successfully replaced.
 */
public static boolean replace(final ASTNode node, final ASTNode replacement) {
    final ASTNode parent = node.getParent();
    final StructuralPropertyDescriptor descriptor = node.getLocationInParent();
    if (descriptor != null) {
        if (descriptor.isChildProperty()) {
            parent.setStructuralProperty(descriptor, replacement);
            node.delete();
            return true;
        } else if (descriptor.isChildListProperty()) {
            @SuppressWarnings("unchecked")
            final List<ASTNode> children = (List<ASTNode>) parent.getStructuralProperty(descriptor);
            children.set(children.indexOf(node), replacement);
            node.delete();
            return true;
        }
    }
    return false;
}

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

License:Open Source License

/**
 * Replaces a node in an AST with other nodes. If the replacement is successful the original node is deleted.
 *
 * @param node The node to replace./*  w ww .j  a v  a  2 s  . com*/
 * @param replacement The replacement nodes.
 * @return <code>true</code> if the node was successfully replaced.
 */
public static boolean replace(final ASTNode node, final List<? extends ASTNode> replacement) {
    final ASTNode parent = node.getParent();
    final StructuralPropertyDescriptor descriptor = node.getLocationInParent();
    if (descriptor != null && descriptor.isChildListProperty()) {
        @SuppressWarnings("unchecked")
        final List<ASTNode> children = (List<ASTNode>) parent.getStructuralProperty(descriptor);
        children.addAll(children.indexOf(node), replacement);
        node.delete();
        return true;
    }
    return false;
}

From source file:com.codenvy.ide.ext.java.server.JavadocFinder.java

License:Open Source License

private static IBinding resolveBinding(ASTNode node) {
    if (node instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) node;
        // workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
        ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
        if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
            ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
            IMethodBinding constructorBinding = cic.resolveConstructorBinding();
            if (constructorBinding == null)
                return null;
            ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
            if (!declaringClass.isAnonymous())
                return constructorBinding;
            ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
            return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
        }//from ww w  .ja va2s  .c  o  m
        return simpleName.resolveBinding();

    } else if (node instanceof SuperConstructorInvocation) {
        return ((SuperConstructorInvocation) node).resolveConstructorBinding();
    } else if (node instanceof ConstructorInvocation) {
        return ((ConstructorInvocation) node).resolveConstructorBinding();
    } else {
        return null;
    }
}

From source file:com.google.devtools.j2cpp.translate.ClassConverter.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void setProperty(ASTNode node, Expression expr) {
    ASTNode parent = node.getParent();/* w w w .j  av a 2  s .co  m*/
    StructuralPropertyDescriptor locator = node.getLocationInParent();
    if (locator instanceof ChildPropertyDescriptor) {
        parent.setStructuralProperty(locator, expr);
    } else {
        // JDT doesn't directly support ChildListProperty replacement.
        List<Expression> args;
        if (parent instanceof MethodInvocation) {
            args = ((MethodInvocation) parent).arguments();
        } else if (parent instanceof ClassInstanceCreation) {
            args = ((ClassInstanceCreation) parent).arguments();
        } else if (parent instanceof InfixExpression) {
            args = ((InfixExpression) parent).extendedOperands();
        } else if (parent instanceof SynchronizedStatement) {
            SynchronizedStatement stmt = (SynchronizedStatement) parent;
            if (node.equals(stmt.getExpression())) {
                stmt.setExpression((Expression) node);
            }
            return;
        } else if (parent instanceof SuperConstructorInvocation) {
            args = ((SuperConstructorInvocation) parent).arguments();
        } else if (parent instanceof ArrayCreation) {
            args = ((ArrayCreation) parent).dimensions();
        } else {
            throw new AssertionError("unknown parent node type: " + parent.getClass().getSimpleName());
        }
        for (int i = 0; i < args.size(); i++) {
            if (node.equals(args.get(i))) {
                args.set(i, expr);
            }
        }
    }
}

From source file:com.google.devtools.j2cpp.translate.ClassConverter.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void setProperty(ASTNode node, Statement stmt) {
    ASTNode parent = node.getParent();//from   w  ww. ja  v a  2  s  . c o m
    StructuralPropertyDescriptor locator = node.getLocationInParent();
    if (locator instanceof ChildPropertyDescriptor) {
        parent.setStructuralProperty(locator, stmt);
    } else {
        // JDT doesn't directly support ChildListProperty replacement.
        List<Statement> args;
        if (parent instanceof Block) {
            args = ((Block) parent).statements();
        } else {
            throw new AssertionError("unknown parent node type: " + parent.getClass().getSimpleName());
        }
        for (int i = 0; i < args.size(); i++) {
            if (node.equals(args.get(i))) {
                args.set(i, stmt);
            }
        }
    }
}

From source file:com.google.devtools.j2objc.util.ASTUtil.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void setProperty(ASTNode node, ASTNode newNode) {
    ASTNode parent = node.getParent();//  w  w w .j  a  v  a2 s .  c o m
    StructuralPropertyDescriptor locator = node.getLocationInParent();
    if (locator instanceof ChildPropertyDescriptor) {
        parent.setStructuralProperty(locator, newNode);
    } else {
        // JDT doesn't directly support ChildListProperty replacement.
        List args;
        if (parent instanceof ArrayCreation) {
            args = ((ArrayCreation) parent).dimensions();
        } else if (parent instanceof ArrayInitializer) {
            args = ((ArrayInitializer) parent).expressions();
        } else if (parent instanceof Block) {
            args = ((Block) parent).statements();
        } else if (parent instanceof ClassInstanceCreation) {
            args = ((ClassInstanceCreation) parent).arguments();
        } else if (parent instanceof ConstructorInvocation) {
            args = ((ConstructorInvocation) parent).arguments();
        } else if (parent instanceof EnumConstantDeclaration) {
            args = ((EnumConstantDeclaration) parent).arguments();
        } else if (parent instanceof InfixExpression) {
            args = ((InfixExpression) parent).extendedOperands();
        } else if (parent instanceof MethodInvocation) {
            args = ((MethodInvocation) parent).arguments();
        } else if (parent instanceof SuperConstructorInvocation) {
            args = ((SuperConstructorInvocation) parent).arguments();
        } else if (parent instanceof SuperMethodInvocation) {
            args = ((SuperMethodInvocation) parent).arguments();
        } else if (parent instanceof SwitchStatement) {
            args = ((SwitchStatement) parent).statements();
        } else if (parent instanceof TypeDeclaration) {
            args = ((TypeDeclaration) parent).superInterfaceTypes();
        } else {
            throw new AssertionError("unknown parent node type: " + parent.getClass().getSimpleName());
        }
        for (int i = 0; i < args.size(); i++) {
            if (node.equals(args.get(i))) {
                args.set(i, newNode);
            }
        }
    }
}

From source file:com.google.gdt.eclipse.designer.gxt.model.widgets.GridInfo.java

License:Open Source License

/**
 * @return {@link MethodInvocation} used for associating given "column" with {@link List} of model
 *         columns./*  w w  w . j  a  v  a2  s  . com*/
 */
private MethodInvocation getInvocationOfAssociationWithThisGrid(SimpleName columnsList,
        ColumnConfigInfo column) {
    for (ASTNode node : column.getRelatedNodes()) {
        if (node.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
            MethodInvocation invocation = (MethodInvocation) node.getParent();
            if (invocation.getName().getIdentifier().equals("add")
                    && isSameColumnsList(columnsList, invocation.getExpression())) {
                return invocation;
            }
        }
    }
    return null;
}

From source file:com.google.gdt.eclipse.designer.model.widgets.panels.grid.FlexTableInfo.java

License:Open Source License

/**
 * @return <code>true</code> if this {@link FlexTableInfo} already "fixed".
 *///from ww w.j a  va2  s  .com
private boolean hasInvocation_fixRowSpan() {
    for (ASTNode relatedNode : getRelatedNodes()) {
        if (relatedNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
            MethodInvocation invocation = (MethodInvocation) relatedNode.getParent();
            if (invocation.toString().contains("FlexTableHelper.fixRowSpan(")) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.google.gwt.eclipse.core.refactoring.regionupdater.EquivalentNodeFinder.java

License:Open Source License

@SuppressWarnings("unchecked")
private int getIndex(ASTNode node) {
    StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
    if (locationInParent != null && locationInParent.isChildListProperty()) {
        List<ASTNode> parentsChildren = (List<ASTNode>) node.getParent()
                .getStructuralProperty(locationInParent);
        if (parentsChildren != null) {
            return parentsChildren.indexOf(node);
        }/*from w  w w  .j  av a  2 s.com*/
    }

    // The node is not contained within a list-based property on the parent
    return NOT_FROM_LIST;
}