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

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

Introduction

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

Prototype

@Override
public final boolean equals(Object obj) 

Source Link

Document

The ASTNode implementation of this Object method uses object identity (==).

Usage

From source file:br.ufal.cideei.soot.instrument.CachedLineNumberMapper.java

License:Open Source License

public Map<Integer, Set<String>> makeAccept(CompilationUnit compilationUnit, IFile file,
        IFeatureExtracter extracter, ASTNode node) {
    // itialized and cached: HIT
    if (visitee != null && node.equals(visitee)) {
        return cachedResult;
    } else {//from  w  ww .j  a  v  a2 s  .c  o  m
        //MISS
        colorMapper = new LineNumberColorMapper(compilationUnit, file, extracter);
        visitee = node;
        visitee.accept(colorMapper);
        return this.cachedResult = colorMapper.getLineToColors();
    }
}

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

License:Open Source License

public IMessageGrouping[] calculateGroups(UMLSequenceViewer viewer, Object activationElement,
        Object[] children) {/*from   ww  w. jav  a 2s. com*/
    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 blockParent = findBlockParent(messageNode);

                List<MappedMessageGrouping> blocks = new LinkedList<MappedMessageGrouping>();
                while (blockParent != null && blockParent.getNodeType() != ASTNode.METHOD_DECLARATION) {

                    if (blockParent != null && blockParent.getNodeType() == ASTNode.IF_STATEMENT) {
                        IfStatement ifStatement = (IfStatement) blockParent;
                        ASTNode block = checkIfSide(ifStatement, messageNode);
                        if (block != null && block.equals(ifStatement.getElseStatement())) {
                            //add a block for the else statement as well
                            MappedMessageGrouping blockNode = groups.get(block);
                            if (blockNode == null) {
                                blockNode = new MappedMessageGrouping(activationElement, i, 0, "", block);
                                groups.put(block, blockNode);
                            }
                            blocks.add(blockNode);
                        }
                    }
                    MappedMessageGrouping blockNode = groups.get(blockParent);
                    if (blockNode == null) {
                        blockNode = new MappedMessageGrouping(activationElement, i, 0, "", blockParent);
                        groups.put(blockParent, blockNode);
                    }
                    blocks.add(blockNode);
                    blockParent = findBlockParent(blockParent);
                }
                for (MappedMessageGrouping blockNode : blocks) {
                    blockNode.setLength(blockNode.getLength() + 1);
                }
            }
        }
    }
    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());
            }
            if (diff == 0) {
                IfStatement ifStatement = null;
                //make sure that else statements are contained in if statements
                if (n1 instanceof IfStatement) {
                    ifStatement = (IfStatement) n1;
                } else if (n2 instanceof IfStatement) {
                    ifStatement = (IfStatement) n2;
                }
                if (ifStatement != null) {
                    if (n2.equals(ifStatement.getElseStatement())) {
                        return -1;
                    } else if (n1.equals(ifStatement.getElseStatement())) {
                        return 1;
                    }
                }
            }
            return diff;
        }
    });
    for (MappedMessageGrouping blockNode : groupList) {
        updateGrouping(blockNode);
    }
    return groupList.toArray(new IMessageGrouping[groupList.size()]);
}

From source file:ca.uvic.chisel.javasketch.internal.ast.groups.ASTLoopGroupCalculator.java

License:Open Source License

/**
 * Checks to see if node c is a child of p
 * @param c//ww  w.j  a v  a 2s.  c  o  m
 * @param p
 * @return
 */
private static boolean isChild(ASTNode c, ASTNode p) {
    ASTNode parent = c.getParent();
    while (parent != null) {
        if (parent.equals(p)) {
            return true;
        }
        parent = parent.getParent();
    }
    return false;
}

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 a va 2 s. c om*/
    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  w w.j  a  va2  s  . co  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.jdt.TreeConverter.java

License:Apache License

public static CompilationUnit convertCompilationUnit(TranslationEnvironment env,
        org.eclipse.jdt.core.dom.CompilationUnit jdtUnit, String sourceFilePath, String mainTypeName,
        String source) {/*from ww w. java  2  s . c  o  m*/
    CompilationUnit unit = new CompilationUnit(env, sourceFilePath, mainTypeName, source);
    if (jdtUnit.getPackage() == null) {
        unit.setPackage(new PackageDeclaration());
    } else {
        unit.setPackage((PackageDeclaration) TreeConverter.convert(jdtUnit.getPackage()));
    }
    for (Object comment : jdtUnit.getCommentList()) {
        // Comments are not normally parented in the JDT AST. Javadoc nodes are
        // normally parented by the BodyDeclaration they apply to, so here we only
        // keep the unparented comments to avoid duplicate comment nodes.
        ASTNode commentParent = ((ASTNode) comment).getParent();
        if (commentParent == null || commentParent.equals(jdtUnit)) {
            Comment newComment = (Comment) TreeConverter.convert(comment);
            // Since the comment is unparented, it's constructor is unable to get
            // the root CompilationUnit to determine the line number.
            newComment.setLineNumber(jdtUnit.getLineNumber(newComment.getStartPosition()));
            unit.addComment(newComment);
        }
    }
    for (Object type : jdtUnit.types()) {
        unit.addType((AbstractTypeDeclaration) TreeConverter.convert(type));
    }
    return unit;
}

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  av a  2 s  . com*/
    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:org.autorefactor.refactoring.Refactorings.java

License:Open Source License

/**
 * Returns whether the provided nodes are a valid existing range.
 *
 * @param nodes the node range to validate
 * @return true if the provided nodes are a valid existing range, false otherwise
 *//*w w w.j  a  va2 s . c  o m*/
public boolean isValidRange(List<? extends ASTNode> nodes) {
    if (nodes.isEmpty()) {
        return true;
    }
    @SuppressWarnings("unchecked")
    final List<ASTNode> originalList = getListRewrite(nodes.get(0)).getOriginalList();
    final Iterator<ASTNode> origIter = originalList.iterator();
    while (origIter.hasNext()) {
        ASTNode origNode = origIter.next();
        final Iterator<? extends ASTNode> currIter = nodes.iterator();
        while (currIter.hasNext()) {
            ASTNode currNode = currIter.next();
            if (origNode.equals(currNode)) {
                // all current nodes must be found in the original list now
                while (origIter.hasNext() && currIter.hasNext()) {
                    origNode = origIter.next();
                    currNode = currIter.next();
                    if (!origNode.equals(currNode)) {
                        return false;
                    }
                }
                return !currIter.hasNext();
            } // else iterate until finding the correct node
        }
    }
    return true;
}

From source file:org.autorefactor.refactoring.rules.AbstractClassSubstituteRefactoring.java

License:Open Source License

private boolean isObjectPassedInParameter(final ASTNode subNode, final MethodInvocation mi) {
    return !subNode.equals(mi.getExpression());
}

From source file:org.autorefactor.refactoring.rules.ReduceVariableScopeRefactoring.java

License:Open Source License

private boolean isReadDominatedByWriteInScopeMoreReducedThanVariableScope(ASTNode readScope, ASTNode writeScope,
        ASTNode varScope) {
    if (varScope.equals(readScope) || varScope.equals(writeScope)) {
        return false;
    }/*from ww  w . j a va  2s . c  om*/
    if (readScope.equals(writeScope)) {
        return true;
    }
    return isReadDominatedByWriteInScopeMoreReducedThanVariableScope(readScope.getParent(), writeScope,
            varScope);
}