Example usage for org.eclipse.jdt.core.dom MethodDeclaration accept

List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration accept

Introduction

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

Prototype

public final void accept(ASTVisitor visitor) 

Source Link

Document

Accepts the given visitor on a visit of the current node.

Usage

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

License:Open Source License

public boolean visit(MethodDeclaration methodDeclaration) {
    methodDeclaration.accept(new MethodBodyColorMapper(compilationUnit, file, extracter, lineToColors));
    return false;
}

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

License:Open Source License

public static ASTMessageGroupingTree calculateGroups(IActivation activationElement) {
    //find the AST and define the groups
    IActivation activation = (IActivation) activationElement;
    IJavaElement element;/*from   ww  w  .  ja v a 2s  .c  o  m*/
    try {
        HashMap<IOriginMessage, ASTNode> mappings = new HashMap<IOriginMessage, ASTNode>();
        element = JavaSearchUtils.findElement(activation, new NullProgressMonitor());
        if (element instanceof IMethod) {
            IMethod method = (IMethod) element;
            IDocument document = getDocumentFor(method);
            ASTNode root = ASTUTils.getASTFor(method.getDeclaringType());
            if (root != null) {
                MethodDeclaration methodDeclaration = ASTUTils.findMethodDeclaration(root, method);
                if (methodDeclaration != null) {
                    try {
                        List<IOriginMessage> children = activation.getOriginMessages();
                        //a list of messages that aren't associated with an AST node:
                        //they are pre-cursors to something else that must occur due to
                        //reflection.
                        List<IOriginMessage> unassociated = new LinkedList<IOriginMessage>();
                        for (IOriginMessage child : children) {
                            ASTMessageFinder finder;
                            if (child instanceof ICall) {
                                finder = new ASTMessageFinder((ICall) child, document);
                            } else if (child instanceof IReply) {
                                finder = new ASTMessageFinder((IReply) child, document);
                            } else {
                                return null;
                            }
                            methodDeclaration.accept(finder);
                            ASTNode messageNode = finder.getNode();
                            if (messageNode == null) {
                                if (child instanceof IReply) {
                                    ASTMessageGroupingTree methodGroup = getGrouping(methodDeclaration);
                                    if (methodGroup != null) {
                                        methodGroup.addMessage(child, unassociated);
                                        unassociated.clear();
                                    }
                                    //sometimes returns don't have line numbers
                                    continue;
                                }
                                unassociated.add((IOriginMessage) child);
                                continue;
                            }
                            mappings.put((IOriginMessage) child, messageNode);
                            ASTNode blockNode = findBlockParent(messageNode);
                            if (blockNode == null)
                                return null;

                            ASTMessageGroupingTree grouping = getGrouping(blockNode);
                            if (grouping == null)
                                return null;
                            IOriginMessage lastMessage = grouping.getLastMessage();
                            ASTNode lastNode = mappings.get(lastMessage);
                            ASTNode thisNode = mappings.get(child);
                            if (grouping.getLastCodeLine() <= child.codeLine()) {
                                //if the called methods are different, then
                                //assume that they are different methods on the
                                //same line of code. Otherwise, it is a loop
                                if (lastNode != null && thisNode != null) {
                                    //first, check to see if the last node is a child of this node
                                    //because if it is, than the evaluation will be opposite
                                    if (isChild(lastNode, thisNode)) {
                                        //                              if (lastNode.getStartPosition() < thisNode.getStartPosition()) {
                                        //                                 grouping = resetGrouping(blockNode);
                                        //                              }
                                    } else if (isChild(thisNode, lastNode)) {
                                        if (lastNode.getStartPosition() <= thisNode.getStartPosition()) {
                                            grouping = resetGrouping(blockNode);
                                        }
                                    } else if (lastNode.getStartPosition() >= thisNode.getStartPosition()) {
                                        grouping = resetGrouping(blockNode);
                                    }
                                } else if (similarMessages((IOriginMessage) grouping.getLastMessage(),
                                        (IOriginMessage) child)) {
                                    grouping = resetGrouping(blockNode);
                                }
                            } else if (grouping.getLastCodeLine() > child.codeLine()) {
                                if (grouping.getNode() != methodDeclaration) {
                                    if (lastNode != null && thisNode != null) {
                                        if (!isChild(lastNode, thisNode)) {
                                            grouping = resetGrouping(blockNode);
                                        }
                                    } else {
                                        grouping = resetGrouping(blockNode);
                                    }
                                }
                            }
                            grouping.addMessage(child, unassociated);
                            unassociated.clear();
                        }
                        //return the node left on the method declaration
                        ASTMessageGroupingTree tree = (ASTMessageGroupingTree) methodDeclaration
                                .getProperty(CURRENT_GROUPING);
                        return tree;
                    } finally {
                        //make sure to clean up
                        if (methodDeclaration != null) {
                            methodDeclaration.accept(new GenericVisitor() {
                                /* (non-Javadoc)
                                 * @see ca.uvic.chisel.javasketch.internal.ast.GenericVisitor#visitNode(org.eclipse.jdt.core.dom.ASTNode)
                                 */
                                @Override
                                protected boolean visitNode(ASTNode node) {
                                    node.setProperty(CURRENT_GROUPING, null);
                                    return true;
                                }
                            });
                        }
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        //ignore and continue
    } catch (CoreException e) {
        SketchPlugin.getDefault().log(e);

    }
    return null;
}

From source file:cc.kave.eclipse.commons.analysis.transformer.DeclarationVisitor.java

License:Apache License

private void methodDeclHelper(MethodDeclaration decl) {
    if (decl != null) {
        MethodName methodName = (MethodName) NodeFactory.createNodeName(decl);

        // if (!isNestedDeclaration(methodName, context)) {
        cc.kave.commons.model.ssts.impl.declarations.MethodDeclaration sstDecl = new cc.kave.commons.model.ssts.impl.declarations.MethodDeclaration();
        sstDecl.setName(methodName);/* ww  w.  jav a 2  s. co m*/
        sstDecl.setEntryPoint(entryPoints.contains(methodName));

        context.getMethods().add(sstDecl);

        if (decl == marker.getAffectedNode()) {
            ExpressionStatement expStatement = new ExpressionStatement();
            expStatement.setExpression(new CompletionExpression());
            sstDecl.getBody().add(expStatement);
        }

        if (!Modifier.isAbstract(decl.getModifiers())) {
            BodyVisitor bodyVisitor = new BodyVisitor(new UniqueVariableNameGenerator(), marker,
                    new ArrayList<IStatement>());
            decl.accept(bodyVisitor);
        }
    }
    // }
}

From source file:chibi.gumtreediff.gen.jdt.cd.CdJdtVisitor.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/* w  w  w .j  a  va2  s.c o m*/
public boolean visit(TypeDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }
    // @Inria
    pushNode(node, node.getName().toString());
    visitListAsNode(EntityType.MODIFIERS, node.modifiers());
    visitListAsNode(EntityType.TYPE_ARGUMENTS, node.typeParameters());
    if (node.getSuperclassType() != null) {
        node.getSuperclassType().accept(this);
    }

    visitListAsNode(EntityType.SUPER_INTERFACE_TYPES, node.superInterfaceTypes());

    // @Inria
    // Change Distiller does not check the changes at Class Field declaration
    for (FieldDeclaration fd : node.getFields()) {
        fd.accept(this);
    }
    // @Inria
    // Visit Declaration and Body (inside MD visiting)
    for (MethodDeclaration md : node.getMethods()) {
        md.accept(this);
    }
    return false;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private void addReleaseStatements(MethodDeclaration method, List<IVariableBinding> fields) {
    // Find existing super.finalize(), if any.
    final boolean[] hasSuperFinalize = new boolean[1];
    method.accept(new ASTVisitor() {
        @Override//from w w  w.j  av a 2 s . c  o m
        public void endVisit(SuperMethodInvocation node) {
            if (FINALIZE_METHOD.equals(node.getName().getIdentifier())) {
                hasSuperFinalize[0] = true;
            }
        }
    });

    List<Statement> statements = method.getBody().statements(); // safe by definition
    if (!statements.isEmpty() && statements.get(0) instanceof TryStatement) {
        TryStatement tryStatement = ((TryStatement) statements.get(0));
        if (tryStatement.getBody() != null) {
            statements = tryStatement.getBody().statements(); // safe by definition
        }
    }
    AST ast = method.getAST();
    int index = statements.size();
    for (IVariableBinding field : fields) {
        if (!field.getType().isPrimitive() && !Types.isWeakReference(field)) {
            Assignment assign = ast.newAssignment();
            SimpleName receiver = ast.newSimpleName(field.getName());
            Types.addBinding(receiver, field);
            assign.setLeftHandSide(receiver);
            assign.setRightHandSide(Types.newNullLiteral());
            Types.addBinding(assign, field.getDeclaringClass());
            ExpressionStatement stmt = ast.newExpressionStatement(assign);
            statements.add(index, stmt);
        }
    }
    if (Options.useReferenceCounting() && !hasSuperFinalize[0]) {
        SuperMethodInvocation call = ast.newSuperMethodInvocation();
        IMethodBinding methodBinding = Types.getMethodBinding(method);
        GeneratedMethodBinding binding = new GeneratedMethodBinding(destructorName, Modifier.PUBLIC,
                Types.mapTypeName("void"), methodBinding.getDeclaringClass(), false, false, true);
        Types.addBinding(call, binding);
        call.setName(ast.newSimpleName(destructorName));
        Types.addBinding(call.getName(), binding);
        ExpressionStatement stmt = ast.newExpressionStatement(call);
        statements.add(stmt);
    }
}

From source file:com.google.gdt.eclipse.designer.gxt.databinding.model.DataBindingsRootInfo.java

License:Open Source License

private static void ensureInvokeInitDataBindings(AstEditor editor, MethodDeclaration lastInfoMethod)
        throws Exception {
    final boolean[] invoke = new boolean[1];
    lastInfoMethod.accept(new ASTVisitor() {
        @Override/*w w  w .j a v a 2 s  .  c  o  m*/
        public boolean visit(MethodInvocation node) {
            String methodName = node.getName().getIdentifier();
            if (node.arguments().isEmpty() && "initDataBindings".equals(methodName)) {
                Assert.isTrue(!invoke[0], "Double invoke initDataBindings()");
                invoke[0] = true;
            }
            return false;
        }
    });
    //
    if (invoke[0]) {
        return;
    }
    //
    List<Statement> statements = DomGenerics.statements(lastInfoMethod.getBody());
    StatementTarget methodTarget;
    if (statements.isEmpty()) {
        methodTarget = new StatementTarget(lastInfoMethod, true);
    } else {
        Statement lastStatement = statements.get(statements.size() - 1);
        methodTarget = new StatementTarget(lastStatement, lastStatement instanceof ReturnStatement);
    }
    //
    editor.addStatement("initDataBindings();", methodTarget);
}

From source file:com.liferay.blade.eclipse.provider.JavaFileJDT.java

License:Open Source License

@Override
public List<SearchResult> findMethodDeclaration(final String name, final String[] params,
        final String returnType) {

    final List<SearchResult> searchResults = new ArrayList<>();

    _ast.accept(new ASTVisitor() {

        @Override//from w w w. ja  va  2  s  .  c  o m
        public boolean visit(MethodDeclaration node) {
            boolean sameParmSize = true;
            boolean sameReturnType = true;

            if (returnType != null) {
                Type type = node.getReturnType2();

                if (type != null) {
                    String returnTypeName = type.resolveBinding().getName();
                    if (!returnTypeName.equals(returnType)) {
                        sameReturnType = false;
                    }
                } else {
                    sameReturnType = false;
                }
            }

            String methodName = node.getName().toString();
            List<?> parmsList = node.parameters();

            if (name.equals(methodName) && params.length == parmsList.size()) {

                for (int i = 0; i < params.length; i++) {
                    if (!(params[i].trim()
                            .equals(parmsList.get(i).toString().substring(0, params[i].trim().length())))) {
                        sameParmSize = false;
                        break;
                    }
                }
            } else {
                sameParmSize = false;
            }

            if (sameParmSize && sameReturnType) {
                final int startLine = _ast.getLineNumber(node.getName().getStartPosition());
                final int startOffset = node.getName().getStartPosition();
                node.accept(new ASTVisitor() {

                    @Override
                    public boolean visit(Block node) {

                        // SimpleName parent can not be MarkerAnnotation and
                        // SimpleType
                        // SingleVariableDeclaration node contains the
                        // parms's type

                        int endLine = _ast.getLineNumber(node.getStartPosition());
                        int endOffset = node.getStartPosition();
                        searchResults.add(
                                createSearchResult(null, startOffset, endOffset, startLine, endLine, true));

                        return false;
                    };
                });
            }

            return false;
        }
    });

    return searchResults;
}

From source file:com.motorola.studio.android.generateviewbylayout.GenerateCodeBasedOnLayoutVisitor.java

License:Apache License

private void findRestoredViews(MethodDeclaration node) {
    SaveStateVisitor visitor = new SaveStateVisitor();
    node.accept(visitor);
    restoredViewIds.addAll(visitor.getViewIds());
}

From source file:com.motorola.studio.android.generateviewbylayout.GenerateCodeBasedOnLayoutVisitor.java

License:Apache License

private void findSavedViews(MethodDeclaration node) {
    SaveStateVisitor visitor = new SaveStateVisitor();
    node.accept(visitor);
    savedViewIds.addAll(visitor.getViewIds());
}

From source file:com.tsc9526.monalisa.plugin.eclipse.generator.SelectGenerator.java

License:Open Source License

private void findSelectMethods() {
    Set<String> imps = new HashSet<String>();

    for (MethodDeclaration md : unit.getUnitType().getMethods()) {
        Type rt = md.getReturnType2();

        if (rt == null)
            continue;

        String returnClazz = rt.toString();
        Set<String> returnParameter = new HashSet<String>();

        if (rt.isParameterizedType()) {
            ParameterizedType ptype = (ParameterizedType) rt;
            returnClazz = ptype.getType().toString();
            for (Object arg : ptype.typeArguments()) {
                returnParameter.add(arg.toString());

                referTypes.add(arg.toString());
            }/*from   www  . jav  a 2s  .c om*/
        } else {
            referTypes.add(returnClazz);
        }

        Annotation a = unit.getAnnotation(md, Select.class);
        if (a != null) {
            SelectMethod sm = new SelectMethod(unit, md, a);
            String rcn = sm.getResultClassName();

            String newReturnType = null;
            if (isCollectionType(returnClazz)) {
                String ps = returnParameter.size() > 0 ? returnParameter.iterator().next() : "";
                if (ps.equals("") || isObjectOrDataMap(ps)) {
                    newReturnType = returnClazz + "<" + rcn + ">";
                    imps.add(sm.getResultClassPackage() + "." + rcn);
                } else {
                    if (rcn.equals(ps) == false && sm.isForceRenameResultClass()) {
                        newReturnType = returnClazz + "<" + rcn + ">";
                        imps.add(sm.getResultClassPackage() + "." + rcn);
                    }
                }
            } else if (isObjectOrDataMap(returnClazz)) {
                newReturnType = rcn;
                imps.add(sm.getResultClassPackage() + "." + rcn);
            } else {
                if (rcn.equals(returnClazz) == false && sm.isForceRenameResultClass()) {
                    newReturnType = rcn;
                    imps.add(sm.getResultClassPackage() + "." + rcn);
                }
            }

            if (newReturnType != null) {
                QueryRewriteVisitor rewrite = new QueryRewriteVisitor(rcn);
                md.accept(rewrite);

                List<ReplaceEdit> changes = rewrite.getChanges();
                changes.add(new ReplaceEdit(rt.getStartPosition(), rt.getLength(), newReturnType));

                for (ReplaceEdit re : changes) {
                    edit.addChild(re);
                }
                sm.calculateFingerprint(changes);

                addSelectMethod(sm);
            } else if (sm.isChanged()) {
                addSelectMethod(sm);
            }
        }
    }

    if (imps.size() > 0) {
        TextEdit importEdit = unit.createImports(imps);
        edit.addChild(importEdit);
    }
}