Example usage for org.eclipse.jdt.internal.compiler.ast AbstractMethodDeclaration traverse

List of usage examples for org.eclipse.jdt.internal.compiler.ast AbstractMethodDeclaration traverse

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast AbstractMethodDeclaration traverse.

Prototype

public void traverse(ASTVisitor visitor, ClassScope classScope) 

Source Link

Usage

From source file:ch.uzh.ifi.seal.changedistiller.ast.java.WhenCommentsAreAssociatedToSourceCode.java

License:Apache License

@BeforeClass
public static void prepareCompilationUnit() throws Exception {
    sCompilation = CompilationUtils.compileFile("src_comments/ClassWithCommentsToAssociate.java");
    List<Comment> comments = CompilationUtils.extractComments(sCompilation);
    CommentCleaner visitor = new CommentCleaner(sCompilation.getSource());
    for (Comment comment : comments) {
        visitor.process(comment);//ww w. java  2s.  c o m
    }
    sComments = visitor.getComments();
    sRoot = new Node(JavaEntityType.METHOD, "foo");
    sRoot.setEntity(new SourceCodeEntity("foo", JavaEntityType.METHOD, new SourceRange()));
    AbstractMethodDeclaration method = CompilationUtils.findMethod(sCompilation.getCompilationUnit(), "foo");
    JavaMethodBodyConverter bodyT = sInjector.getInstance(JavaMethodBodyConverter.class);
    bodyT.initialize(sRoot, method, sComments, sCompilation.getScanner());
    method.traverse(bodyT, (ClassScope) null);
}

From source file:ch.uzh.ifi.seal.changedistiller.ast.java.WhenDeclarationsAreConverted.java

License:Apache License

private void convertMethod(String name) {
    createRootNode(JavaEntityType.METHOD_DECLARATION, name);
    AbstractMethodDeclaration method = CompilationUtils.findMethod(fCompilation.getCompilationUnit(), name);
    method.traverse(getDeclarationconverter(), (ClassScope) null);
}

From source file:ch.uzh.ifi.seal.changedistiller.ast.java.WhenMethodBodiesAreConverted.java

License:Apache License

private void convert() {
    createRootNode(JavaEntityType.METHOD, "method");
    AbstractMethodDeclaration method = CompilationUtils.findMethod(fCompilation.getCompilationUnit(), "method");
    sMethodBodyConverter.initialize(fRoot, method, null, fCompilation.getScanner());
    method.traverse(sMethodBodyConverter, (ClassScope) null);
}

From source file:ch.uzh.ifi.seal.changedistiller.distilling.WhenChangesAreExtracted.java

License:Apache License

public Node convertMethodBody(String methodName, JavaCompilation compilation) {
    AbstractMethodDeclaration method = CompilationUtils.findMethod(compilation.getCompilationUnit(),
            methodName);//from  w ww.j  a  v  a  2s  .c o  m
    Node root = new Node(JavaEntityType.METHOD, methodName);
    root.setEntity(new SourceCodeEntity(methodName, JavaEntityType.METHOD,
            new SourceRange(method.declarationSourceStart, method.declarationSourceEnd)));
    List<Comment> comments = CompilationUtils.extractComments(compilation);
    sMethodBodyConverter.initialize(root, method, comments, compilation.getScanner());
    method.traverse(sMethodBodyConverter, (ClassScope) null);
    return root;
}

From source file:ch.uzh.ifi.seal.changedistiller.distilling.WhenChangesAreExtracted.java

License:Apache License

public Node convertMethodDeclaration(String methodName, JavaCompilation compilation) {
    AbstractMethodDeclaration method = CompilationUtils.findMethod(compilation.getCompilationUnit(),
            methodName);//from  w  w  w . j  av a  2  s . c  o  m
    Node root = new Node(JavaEntityType.METHOD, methodName);
    root.setEntity(new SourceCodeEntity(methodName, JavaEntityType.METHOD,
            new SourceRange(method.declarationSourceStart, method.declarationSourceEnd)));
    sDeclarationConverter.initialize(root, compilation.getScanner());
    method.traverse(sDeclarationConverter, (ClassScope) null);
    return root;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Visit the given method declaration and report the nodes that match exactly the
 * search pattern (i.e. the ones in the matching nodes set)
 * Note that the method declaration has already been checked.
 *///from   w  ww .  j av  a2  s .c  om
protected void reportMatching(AbstractMethodDeclaration method, TypeDeclaration type, IJavaElement parent,
        int accuracy, boolean typeInHierarchy, MatchingNodeSet nodeSet) throws CoreException {
    IJavaElement enclosingElement = null;

    // report method declaration itself
    if (accuracy > -1) {
        enclosingElement = createHandle(method, parent);
        if (enclosingElement != null) { // skip if unable to find method
            // compute source positions of the selector
            Scanner scanner = this.parser.scanner;
            int nameSourceStart = method.sourceStart;
            scanner.setSource(this.currentPossibleMatch.getContents());
            scanner.resetTo(nameSourceStart, method.sourceEnd);
            try {
                scanner.getNextToken();
            } catch (InvalidInputException e) {
                // ignore
            }
            if (encloses(enclosingElement)) {
                SearchMatch match = null;
                if (method.isDefaultConstructor()) {
                    // Use type for match associated element as default constructor does not exist in source
                    int offset = type.sourceStart;
                    match = this.patternLocator.newDeclarationMatch(type, parent, type.binding, accuracy,
                            type.sourceEnd - offset + 1, this);
                } else {
                    int length = scanner.currentPosition - nameSourceStart;
                    match = this.patternLocator.newDeclarationMatch(method, enclosingElement, method.binding,
                            accuracy, length, this);
                }
                if (match != null) {
                    report(match);
                }
            }
        }
    }

    // handle nodes for the local type first
    if ((method.bits & ASTNode.HasLocalType) != 0) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        // Traverse method declaration to report matches both in local types declaration
        // and in local variables declaration
        ASTNode[] nodes = typeInHierarchy
                ? nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd)
                : null;
        boolean report = (this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0
                && encloses(enclosingElement);
        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(enclosingElement,
                report ? nodes : null, nodeSet, this);
        try {
            method.traverse(declarationVisitor, (ClassScope) null);
        } catch (WrappedCoreException e) {
            throw e.coreException;
        }
        // Report all nodes and remove them
        if (nodes != null) {
            int length = nodes.length;
            for (int i = 0; i < length; i++) {
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                if (report && level != null) {
                    this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                            declarationVisitor.getLocalElement(i), declarationVisitor.getOtherElements(i),
                            method.binding, level.intValue(), this);
                }
            }
        }
    }

    // report the type parameters
    TypeParameter[] typeParameters = method.typeParameters();
    if (typeParameters != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(typeParameters, enclosingElement, parent, method.binding, nodeSet);
        }
    }

    // report annotations
    if (method.annotations != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(method.annotations, enclosingElement, null, method.binding, nodeSet, true, true);
        }
    }

    // references in this method
    if (typeInHierarchy) {
        ASTNode[] nodes = nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd);
        if (nodes != null) {
            if ((this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0) {
                if (enclosingElement == null) {
                    enclosingElement = createHandle(method, parent);
                }
                if (encloses(enclosingElement)) {
                    if (this.pattern.mustResolve) {
                        // Visit only if the pattern must resolve
                        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(
                                enclosingElement, nodes, nodeSet, this);
                        method.traverse(declarationVisitor, (ClassScope) null);
                        int length = nodes.length;
                        for (int i = 0; i < length; i++) {
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                                        declarationVisitor.getLocalElement(i),
                                        declarationVisitor.getOtherElements(i), method.binding,
                                        level.intValue(), this);
                            }
                        }
                    } else {
                        for (int i = 0, l = nodes.length; i < l; i++) {
                            ASTNode node = nodes[i];
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                        method.binding, level.intValue(), this);
                            }
                        }
                    }
                    return;
                }
            }
            // Remove all remaining nodes
            for (int i = 0, l = nodes.length; i < l; i++) {
                nodeSet.matchingNodes.removeKey(nodes[i]);
            }
        }
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.util.ASTNodeFinder.java

License:Open Source License

public TypeDeclaration findType(IType typeHandle) {
    IJavaElement parent = typeHandle.getParent();
    final char[] typeName = typeHandle.getElementName().toCharArray();
    final int occurenceCount = ((SourceType) typeHandle).occurrenceCount;
    final boolean findAnonymous = typeName.length == 0;
    class Visitor extends ASTVisitor {
        TypeDeclaration result;/*from   ww w.ja  va2 s  .  c  o m*/
        int count = 0;

        public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
            if (this.result != null)
                return false;
            if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
                if (findAnonymous && ++this.count == occurenceCount) {
                    this.result = typeDeclaration;
                }
            } else {
                if (!findAnonymous && CharOperation.equals(typeName, typeDeclaration.name)) {
                    this.result = typeDeclaration;
                }
            }
            return false; // visit only one level
        }
    }
    switch (parent.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        TypeDeclaration[] types = this.unit.types;
        if (types != null) {
            for (int i = 0, length = types.length; i < length; i++) {
                TypeDeclaration type = types[i];
                if (CharOperation.equals(typeName, type.name)) {
                    return type;
                }
            }
        }
        break;
    case IJavaElement.TYPE:
        TypeDeclaration parentDecl = findType((IType) parent);
        if (parentDecl == null)
            return null;
        types = parentDecl.memberTypes;
        if (types != null) {
            for (int i = 0, length = types.length; i < length; i++) {
                TypeDeclaration type = types[i];
                if (CharOperation.equals(typeName, type.name)) {
                    return type;
                }
            }
        }
        break;
    case IJavaElement.FIELD:
        FieldDeclaration fieldDecl = findField((IField) parent);
        if (fieldDecl == null)
            return null;
        Visitor visitor = new Visitor();
        fieldDecl.traverse(visitor, null);
        return visitor.result;
    case IJavaElement.INITIALIZER:
        Initializer initializer = findInitializer((IInitializer) parent);
        if (initializer == null)
            return null;
        visitor = new Visitor();
        initializer.traverse(visitor, null);
        return visitor.result;
    case IJavaElement.METHOD:
        AbstractMethodDeclaration methodDecl = findMethod((IMethod) parent);
        if (methodDecl == null)
            return null;
        visitor = new Visitor();
        methodDecl.traverse(visitor, (ClassScope) null);
        return visitor.result;
    }
    return null;
}

From source file:org.eclipse.che.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
* Visit the given method declaration and report the nodes that match exactly the
* search pattern (i.e. the ones in the matching nodes set)
* Note that the method declaration has already been checked.
*/// w ww .  j  a  v a2  s. c  o m
protected void reportMatching(AbstractMethodDeclaration method, TypeDeclaration type, IJavaElement parent,
        int accuracy, boolean typeInHierarchy, MatchingNodeSet nodeSet) throws CoreException {
    IJavaElement enclosingElement = null;

    // report method declaration itself
    if (accuracy > -1) {
        enclosingElement = createHandle(method, parent);
        if (enclosingElement != null) { // skip if unable to find method
            // compute source positions of the selector
            Scanner scanner = this.parser.scanner;
            int nameSourceStart = method.sourceStart;
            scanner.setSource(this.currentPossibleMatch.getContents());
            scanner.resetTo(nameSourceStart, method.sourceEnd);
            try {
                scanner.getNextToken();
            } catch (InvalidInputException e) {
                // ignore
            }
            if (encloses(enclosingElement)) {
                SearchMatch match = null;
                if (method.isDefaultConstructor()) {
                    // Use type for match associated element as default constructor does not exist in source
                    int offset = type.sourceStart;
                    match = this.patternLocator.newDeclarationMatch(type, parent, type.binding, accuracy,
                            type.sourceEnd - offset + 1, this);
                } else {
                    int length = scanner.currentPosition - nameSourceStart;
                    match = this.patternLocator.newDeclarationMatch(method, enclosingElement, method.binding,
                            accuracy, length, this);
                }
                if (match != null) {
                    report(match);
                }
            }
        }
    }

    // handle nodes for the local type first
    if ((method.bits & ASTNode.HasLocalType) != 0) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        // Traverse method declaration to report matches both in local types declaration
        // and in local variables declaration
        ASTNode[] nodes = typeInHierarchy
                ? nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd)
                : null;
        boolean report = (this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0
                && encloses(enclosingElement);
        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(enclosingElement,
                report ? nodes : null, nodeSet, this, typeInHierarchy);
        try {
            method.traverse(declarationVisitor, (ClassScope) null);
        } catch (WrappedCoreException e) {
            throw e.coreException;
        }
        // Report all nodes and remove them
        if (nodes != null) {
            int length = nodes.length;
            for (int i = 0; i < length; i++) {
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                if (report && level != null) {
                    this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                            declarationVisitor.getLocalElement(i), declarationVisitor.getOtherElements(i),
                            method.binding, level.intValue(), this);
                }
            }
        }
    }

    // report the type parameters
    TypeParameter[] typeParameters = method.typeParameters();
    if (typeParameters != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(typeParameters, enclosingElement, parent, method.binding, nodeSet);
        }
    }

    // report annotations
    if (method.annotations != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(method.annotations, enclosingElement, null, method.binding, nodeSet, true, true);
        }
    }

    // references in this method
    if (typeInHierarchy) {
        ASTNode[] nodes = nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd);
        if (nodes != null) {
            if ((this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0) {
                if (enclosingElement == null) {
                    enclosingElement = createHandle(method, parent);
                }
                if (encloses(enclosingElement)) {
                    if (this.pattern.mustResolve) {
                        // Visit only if the pattern must resolve
                        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(
                                enclosingElement, nodes, nodeSet, this, typeInHierarchy);
                        method.traverse(declarationVisitor, (ClassScope) null);
                        int length = nodes.length;
                        for (int i = 0; i < length; i++) {
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                                        declarationVisitor.getLocalElement(i),
                                        declarationVisitor.getOtherElements(i), method.binding,
                                        level.intValue(), this);
                            }
                        }
                    } else {
                        for (int i = 0, l = nodes.length; i < l; i++) {
                            ASTNode node = nodes[i];
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                        method.binding, level.intValue(), this);
                            }
                        }
                    }
                    return;
                }
            }
            // Remove all remaining nodes
            for (int i = 0, l = nodes.length; i < l; i++) {
                nodeSet.matchingNodes.removeKey(nodes[i]);
            }
        }
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.TransformStatementsVisitor.java

License:Open Source License

public static void checkTransformStatements(AbstractMethodDeclaration method) {
    ClassScope scope = method.scope.classScope();
    if (scope.referenceContext.ignoreFurtherInvestigation && method instanceof MethodDeclaration
            && method.isCallin() && (method.bits & ASTNode.HasBeenTransformed) == 0) {
        // but if class has errors the visitor bailed out.
        // need to transform before changing the selector
        method.traverse(new TransformStatementsVisitor(scope.compilerOptions().weavingScheme), scope);
    }/*  ww  w . j ava  2  s  .  c o m*/
}

From source file:ut.seal.plugins.utils.ast.UTASTNodeConverter.java

License:Apache License

/**
 * Convert methods./*  w  ww .j  a va  2 s.c o m*/
 * 
 * @param icu the icu
 * @return the list
 */
public List<Node> convertMethods(ICompilationUnit icu) {
    List<Node> nodes = new ArrayList<Node>();
    List<AbstractMethodDeclaration> methods = new ArrayList<AbstractMethodDeclaration>();
    UTGeneralVisitor visitor = new UTGeneralVisitor();
    JavaCompilation compilation = UTCompilationUtils.compileSource(icu);
    CompilationUnitDeclaration comUnitDecl = compilation.getCompilationUnit();
    for (TypeDeclaration type : comUnitDecl.types) {
        type.traverse(visitor, (ClassScope) null);
        methods.addAll(visitor.getMethodDeclList());
    }
    for (int i = 0; i < methods.size(); i++) {
        AbstractMethodDeclaration method = methods.get(i);
        String methodName = String.valueOf(method.selector);
        Node root = new Node(JavaEntityType.METHOD, methodName);
        root.setEntity(new SourceCodeEntity(methodName, JavaEntityType.METHOD, //
                new SourceRange(method.declarationSourceStart, //
                        method.declarationSourceEnd)));
        List<Comment> comments = UTCompilationUtils.extractComments(compilation);
        sMethodBodyConverter.initialize(root, method, comments, compilation.getScanner());
        method.traverse(sMethodBodyConverter, (ClassScope) null);
        nodes.add(root);
    }
    return nodes;
}