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

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

Introduction

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

Prototype

public final ASTNode getParent() 

Source Link

Document

Returns this node's parent node, or null if this is the 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 av  a2s. co m*/
    } else if (child instanceof ASTAttribute) {
        return ((ASTAttribute) child).getParent();
    }
    return null;
}

From source file:br.ufal.cideei.util.MethodDeclarationSootMethodBridge.java

License:Open Source License

/**
 * Gets the parent method.//from  ww w. j ava2 s . c  o m
 * 
 * @param node
 *            the node
 * @return the parent method
 */
public static MethodDeclaration getParentMethod(ASTNode node) {
    if (node == null) {
        return null;
    } else {
        if (node.getNodeType() == ASTNode.METHOD_DECLARATION) {
            return (MethodDeclaration) node;
        } else {
            return getParentMethod(node.getParent());
        }
    }
}

From source file:ca.mcgill.cs.swevo.jayfx.ASTCrawler.java

License:Open Source License

@SuppressWarnings({ "unchecked", "unused" })
// private static List<AdviceElement> getApplicableAdvice(final IJavaElement
// je) {//from  w  w  w  .ja  va  2 s  .  com
// final List<AdviceElement> advisedBy = new ArrayList<AdviceElement>();
// final List<AdviceElement> direct = AJModel.getInstance()
// .getRelatedElements(AJRelationshipManager.ADVISED_BY, je);
// if (direct != null)
// advisedBy.addAll(direct);
//
// // check for advised code elements
// final List extras = AJModel.getInstance().getExtraChildren(je);
// if (extras != null)
// for (final Iterator iter = extras.iterator(); iter.hasNext();) {
// final IJavaElement element = (IJavaElement) iter.next();
// final List<AdviceElement> indirect = AJModel.getInstance()
// .getRelatedElements(AJRelationshipManager.ADVISED_BY,
// element);
// if (indirect != null)
// advisedBy.addAll(indirect);
// }
// return advisedBy;
// }

private static Assignment getAssignment(final ASTNode node) {
    if (node == null)
        return null;

    if (node.getNodeType() == ASTNode.ASSIGNMENT)
        return (Assignment) node;

    else
        return ASTCrawler.getAssignment(node.getParent());
}

From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java

License:Open Source License

/**
 * <p>//from w  w w .  jav a 2s  .c om
 * Convenience method to exclude self and/or name.
 * </p>
 * 
 * @param node
 * @param includeSelf
 * @return
 */
public static ASTNode getFieldContainer(ASTNode node, boolean includeSpecial, boolean includeSelf) {
    ASTNode container = null;

    // XXX Special case: if you want the container of the first qualifier in
    // a qualified name,
    // You want the type declaration (or whatever is the top-level parent),
    // but not the
    // qualified name.
    if (!includeSelf && includeSpecial) {
        ASTNode parent = node.getParent();
        if (parent instanceof QualifiedName) {
            QualifiedName qName = (QualifiedName) parent;
            if (qName.getQualifier().equals(node)) {
                return getFieldContainer(parent, includeSpecial, includeSelf);
            }
        }
    }

    if (node == null) {
        container = null;
    } else if (includeSelf && isFieldContainer(node, includeSpecial)) {
        if (node instanceof QualifiedName) {
            container = ((QualifiedName) node).getQualifier();
        } else if (node instanceof SuperFieldAccess) {
            SuperFieldAccess sFieldAccess = (SuperFieldAccess) node;
            if (sFieldAccess.getQualifier() != null) {
                container = sFieldAccess.getQualifier();
            } else {
                container = getFieldContainer(node.getParent(), includeSpecial, true);
            }
        } else if (node instanceof FieldAccess) {
            container = ((FieldAccess) node).getExpression();
        } else {
            container = node;
        }
    } else {
        container = getFieldContainer(node.getParent(), includeSpecial, true);
    }

    return container;
}

From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java

License:Open Source License

public static ASTNode getMethodContainer(ASTNode node) {
    ASTNode container = null;/*from   w  w w.j a  va2  s  . c  o m*/

    if (node == null) {
        container = null;
    } else if (isMethodContainer(node)) {
        container = node;
    } else {
        container = getMethodContainer(node.getParent());
    }

    return container;
}

From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java

License:Open Source License

public static ASTNode getSpecificParentType(ASTNode node, int type) {
    ASTNode specificParent = null;//from w w w  .ja  v a  2 s. c om

    if (node != null) {
        if (node.getNodeType() == type) {
            specificParent = node;
        } else {
            specificParent = getSpecificParentType(node.getParent(), type);
        }
    }

    return specificParent;
}

From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java

License:Open Source License

public static boolean isIndicationOfField(Name node) {
    boolean isField = false;

    ASTNode parent = node.getParent();

    // The thing here is that if the parent is a qualified name, go one
    // level up.// w  w w.  ja v  a 2s. c o m
    // If the parent is still a qualified name, then, it means we were in
    // the middle, so this is
    // not necessarily a field.
    if (parent instanceof QualifiedName) {
        QualifiedName qName = (QualifiedName) parent;
        if (qName.getName().equals(node)) {
            node = (Name) parent;
            parent = parent.getParent();
        }
    }

    if (parent instanceof ArrayAccess) {
        isField = true;
        // } else if (parent instanceof ArrayCreation) {
        // isField = true;
    } else if (parent instanceof ArrayInitializer) {
        isField = true;
    } else if (parent instanceof Assignment) {
        isField = true;
    } else if (parent instanceof CastExpression) {
        CastExpression cExpression = (CastExpression) parent;
        isField = cExpression.getExpression().equals(node);
    } else if (parent instanceof ConditionalExpression) {
        isField = true;
    } else if (parent instanceof InfixExpression) {
        isField = true;
    } else if (parent instanceof WhileStatement) {
        isField = true;
    } else if (parent instanceof DoStatement) {
        isField = true;
    } else if (parent instanceof ForStatement) {
        ForStatement forStatement = (ForStatement) parent;
        isField = forStatement.getExpression().equals(node);
    } else if (parent instanceof PrefixExpression) {
        isField = true;
    } else if (parent instanceof PostfixExpression) {
        isField = true;
    } else if (parent instanceof ReturnStatement) {
        isField = true;
    } else if (parent instanceof InstanceofExpression) {
        InstanceofExpression ioe = (InstanceofExpression) parent;
        isField = ioe.getLeftOperand().equals(node);
    } else if (parent instanceof FieldAccess) {
        isField = true;
    } else if (parent instanceof SuperFieldAccess) {
        isField = true;
    } else if (parent instanceof MethodInvocation) {
        MethodInvocation mi = (MethodInvocation) parent;
        for (Object object : mi.arguments()) {
            if (node == object) {
                isField = true;
                break;
            }
        }
    } else if (parent instanceof ClassInstanceCreation) {
        ClassInstanceCreation cic = (ClassInstanceCreation) parent;
        for (Object object : cic.arguments()) {
            if (node == object) {
                isField = true;
                break;
            }
        }
    } else if (parent instanceof VariableDeclarationFragment) {
        isField = true;
    }

    return isField;
}

From source file:ca.mcgill.cs.swevo.ppa.tests.AnonymousVisitor.java

License:Open Source License

private MethodDeclaration getKey(AnonymousClassDeclaration node) {
    ASTNode parent = node.getParent();
    while (parent != null && !(parent instanceof MethodDeclaration)) {
        parent = parent.getParent();
    }/*  w w  w .  j  av a2s.  com*/

    MethodDeclaration mDeclaration = null;
    if (parent != null) {
        mDeclaration = (MethodDeclaration) parent;
    }

    return mDeclaration;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.NameMapVisitor.java

License:Open Source License

private ASTNode getDeclarationParent(ASTNode node) {
    if (node == null) {
        return null;
    } else if (node instanceof BodyDeclaration) {
        return node;
    } else if (node instanceof VariableDeclaration) {
        return node;
    } else {//from ww w  . j  ava2 s  .c om
        return node.getParent();
    }
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaMessageGrouper.java

License:Open Source License

public IMessageGrouping[] calculateGroups2(UMLSequenceViewer viewer, Object activationElement,
        Object[] children) {//  ww  w  .ja v a2s .co m
    HashMap<ASTNode, MappedMessageGrouping> groups = new HashMap<ASTNode, MappedMessageGrouping>();
    if (!(activationElement instanceof IAdaptable)) {
        return new IMessageGrouping[0];
    }
    ASTNode activationNode = (ASTNode) ((IAdaptable) activationElement).getAdapter(ASTNode.class);
    if (!(activationNode instanceof MethodDeclaration)) {
        return new IMessageGrouping[0];
    }
    for (int i = 0; i < children.length; i++) {
        if (children[i] instanceof IAdaptable) {
            ASTNode messageNode = (ASTNode) ((IAdaptable) children[i]).getAdapter(ASTNode.class);
            if (messageNode != null) {
                ASTNode currentParent = messageNode.getParent();
                while (currentParent != null && currentParent != activationNode) {
                    ASTNode block = null;
                    String text = null;
                    Color c = null;
                    Color bc = null;
                    String expressionString = "";
                    switch (currentParent.getNodeType()) {
                    case ASTNode.IF_STATEMENT:
                        block = checkIfSide((IfStatement) currentParent, messageNode);
                        if (block != null && block == ((IfStatement) currentParent).getElseStatement()) {
                            text = "else";
                        } else if (block == ((IfStatement) currentParent).getThenStatement()) {
                            text = "if (" + ((IfStatement) currentParent).getExpression().toString() + ")";
                        }
                        c = ISketchColorConstants.CONDITION_FG;
                        bc = ISketchColorConstants.CONDITION_FG;
                        break;
                    case ASTNode.WHILE_STATEMENT:
                        if (((WhileStatement) currentParent).getExpression() != null) {
                            expressionString = ((WhileStatement) currentParent).getExpression().toString();
                        }
                        text = "while (" + expressionString + ")";
                        block = currentParent;
                        c = ISketchColorConstants.LOOP_FG;
                        bc = ISketchColorConstants.LOOP_BG;
                        break;
                    case ASTNode.FOR_STATEMENT:
                        if (((ForStatement) currentParent).getExpression() != null) {
                            expressionString = ((ForStatement) currentParent).getExpression().toString();
                        } else {
                            expressionString = ";;";
                        }
                        text = "for (" + expressionString + ")";
                        block = currentParent;
                        c = ISketchColorConstants.LOOP_FG;
                        bc = ISketchColorConstants.LOOP_BG;
                        break;
                    case ASTNode.TRY_STATEMENT:
                        text = "try";
                        block = currentParent;
                        c = ISketchColorConstants.ERROR_FG;
                        bc = ISketchColorConstants.ERROR_BG;
                        break;
                    case ASTNode.CATCH_CLAUSE:
                        text = "catch (" + ((CatchClause) currentParent).getException().toString() + ")";
                        block = currentParent;
                        c = ISketchColorConstants.ERROR_FG;
                        bc = ISketchColorConstants.ERROR_BG;
                        break;
                    case ASTNode.DO_STATEMENT:
                        text = "do while (" + ((DoStatement) currentParent).getExpression().toString() + ")";
                        block = currentParent;
                        c = ISketchColorConstants.LOOP_FG;
                        bc = ISketchColorConstants.LOOP_BG;
                        break;
                    }
                    if (text != null) {
                        MappedMessageGrouping grouping = groups.get(block);
                        if (grouping == null) {
                            grouping = new MappedMessageGrouping(activationElement, i, 1, text, block);
                            grouping.setBackground(bc);
                            grouping.setForeground(c);
                            groups.put(block, grouping);
                        } else {
                            int length = (i - grouping.getOffset()) + 1;
                            grouping.setLength(length);
                        }
                    }
                    currentParent = currentParent.getParent();
                }
            }
        }
    }
    ArrayList<MappedMessageGrouping> groupList = new ArrayList<MappedMessageGrouping>(groups.values());
    Collections.sort(groupList, new Comparator<MappedMessageGrouping>() {
        public int compare(MappedMessageGrouping o1, MappedMessageGrouping o2) {
            ASTNode n1 = (ASTNode) o1.getKey();
            ASTNode n2 = (ASTNode) o2.getKey();
            int diff = n1.getStartPosition() - n2.getStartPosition();
            if (diff == 0) {
                diff = (n1.getStartPosition() + n1.getLength()) - (n2.getStartPosition() + n2.getLength());
            }
            return diff;
        }
    });
    return groupList.toArray(new IMessageGrouping[groupList.size()]);
}