Search in sources :

Example 41 with MethodTree

use of com.sun.source.tree.MethodTree in project error-prone by google.

the class PreconditionsCheckNotNullPrimitive method hasMethodParameter.

/**
 * Determines whether the expression contains a reference to one of the enclosing method's
 * parameters.
 *
 * <p>TODO(eaftan): Extract this to ASTHelpers.
 *
 * @param path the path to the current tree node
 * @param tree the node to compare against the parameters
 * @return whether the argument is a parameter to the enclosing method
 */
private static boolean hasMethodParameter(TreePath path, ExpressionTree tree) {
    Set<Symbol> symbols = new HashSet<>();
    for (IdentifierTree ident : getVariableUses(tree)) {
        Symbol sym = ASTHelpers.getSymbol(ident);
        if (sym.isLocal()) {
            symbols.add(sym);
        }
    }
    // Find enclosing method declaration.
    while (path != null && !(path.getLeaf() instanceof MethodTree)) {
        path = path.getParentPath();
    }
    if (path == null) {
        throw new IllegalStateException("Should have an enclosing method declaration");
    }
    MethodTree methodDecl = (MethodTree) path.getLeaf();
    for (VariableTree param : methodDecl.getParameters()) {
        if (symbols.contains(ASTHelpers.getSymbol(param))) {
            return true;
        }
    }
    return false;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) VariableTree(com.sun.source.tree.VariableTree) IdentifierTree(com.sun.source.tree.IdentifierTree) HashSet(java.util.HashSet)

Example 42 with MethodTree

use of com.sun.source.tree.MethodTree in project error-prone by google.

the class OverrideThrowableToString method matchClass.

@Override
public Description matchClass(ClassTree classTree, VisitorState visitorState) {
    Symbol throwableClass = visitorState.getSymbolFromString("java.lang.Throwable");
    if (Objects.equals(ASTHelpers.getSymbol(classTree.getExtendsClause()), throwableClass)) {
        Optional<? extends Tree> methodTree = classTree.getMembers().stream().filter(m -> m instanceof MethodTree && ((MethodTree) m).getName().contentEquals("toString")).findFirst();
        if (methodTree.isPresent()) {
            SuggestedFix.Builder builder = SuggestedFix.builder();
            MethodTree tree = (MethodTree) methodTree.get();
            if (!tree.getParameters().isEmpty()) {
                return Description.NO_MATCH;
            }
            String newTree = tree.getModifiers().toString().replaceAll("@Override[(][)]", "@Override") + "String getMessage()\n" + visitorState.getSourceForNode(tree.getBody());
            builder.replace(tree, newTree);
            return describeMatch(classTree, builder.build());
        }
    }
    return Description.NO_MATCH;
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) MethodTree(com.sun.source.tree.MethodTree) Symbol(com.sun.tools.javac.code.Symbol) Objects(java.util.Objects) VisitorState(com.google.errorprone.VisitorState) Description(com.google.errorprone.matchers.Description) BugPattern(com.google.errorprone.BugPattern) Optional(java.util.Optional) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JDK(com.google.errorprone.BugPattern.Category.JDK) Tree(com.sun.source.tree.Tree) ASTHelpers(com.google.errorprone.util.ASTHelpers) ClassTree(com.sun.source.tree.ClassTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodTree(com.sun.source.tree.MethodTree) Symbol(com.sun.tools.javac.code.Symbol)

Example 43 with MethodTree

use of com.sun.source.tree.MethodTree in project j2objc by google.

the class TreeConverter method convertAnnotationTypeDeclaration.

private TreeNode convertAnnotationTypeDeclaration(ClassTree node, TreePath parent) {
    AnnotationTypeDeclaration newNode = new AnnotationTypeDeclaration();
    TreePath path = getTreePath(parent, node);
    Element element = getElement(path);
    convertBodyDeclaration(node, path, node.getModifiers(), newNode);
    for (Tree bodyDecl : node.getMembers()) {
        if (bodyDecl.getKind() == Kind.METHOD) {
            MethodTree methodTree = (MethodTree) bodyDecl;
            TreePath methodPath = getTreePath(path, methodTree);
            ExecutableElement methodElement = (ExecutableElement) getElement(methodPath);
            Tree defaultValue = methodTree.getDefaultValue();
            ModifiersTree modifiers = methodTree.getModifiers();
            AnnotationTypeMemberDeclaration newMember = new AnnotationTypeMemberDeclaration().setDefault((Expression) convert(defaultValue, methodPath)).setExecutableElement(methodElement);
            newMember.setModifiers((int) ((JCModifiers) modifiers).flags).setAnnotations(convertAnnotations(modifiers, getTreePath(methodPath, modifiers))).setJavadoc((Javadoc) getAssociatedJavaDoc(methodTree, methodPath));
            newNode.addBodyDeclaration(newMember);
        } else {
            newNode.addBodyDeclaration((BodyDeclaration) convert(bodyDecl, path));
        }
    }
    return newNode.setName(convertSimpleName(element, getTypeMirror(path), getNamePosition(node))).setTypeElement((TypeElement) element);
}
Also used : TreePath(com.sun.source.util.TreePath) ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) MethodTree(com.sun.source.tree.MethodTree) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) JCFunctionalExpression(com.sun.tools.javac.tree.JCTree.JCFunctionalExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) GeneratedPackageElement(com.google.devtools.j2objc.types.GeneratedPackageElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationTypeDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeDeclaration) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ThrowTree(com.sun.source.tree.ThrowTree) BlockTree(com.sun.source.tree.BlockTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) BreakTree(com.sun.source.tree.BreakTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ContinueTree(com.sun.source.tree.ContinueTree) CaseTree(com.sun.source.tree.CaseTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) SwitchTree(com.sun.source.tree.SwitchTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) JCTree(com.sun.tools.javac.tree.JCTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) TryTree(com.sun.source.tree.TryTree)

Example 44 with MethodTree

use of com.sun.source.tree.MethodTree in project vertx-docgen by vert-x3.

the class JavaDocGenerator method renderSource.

/**
 * Render the source fragment for the Java language. Java being the pivot language, we consider this method as the
 * _default_ behavior. This method is final as it must not be overridden by any extension.
 *
 * @param elt    the element
 * @param source the source
 * @return the fragment
 */
@Override
public String renderSource(ExecutableElement elt, String source) {
    // Get block
    TreePath path = docTrees.getPath(elt);
    MethodTree methodTree = (MethodTree) path.getLeaf();
    BlockTree blockTree = methodTree.getBody();
    List<? extends StatementTree> statements = blockTree.getStatements();
    if (statements.size() > 0) {
        return renderSource(path, statements, source);
    } else {
        return null;
    }
}
Also used : TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) BlockTree(com.sun.source.tree.BlockTree)

Example 45 with MethodTree

use of com.sun.source.tree.MethodTree in project bazel by bazelbuild.

the class Analysis method init.

/** Initialize the analysis with a new control flow graph. */
protected void init(ControlFlowGraph cfg) {
    this.cfg = cfg;
    thenStores = new IdentityHashMap<>();
    elseStores = new IdentityHashMap<>();
    inputs = new IdentityHashMap<>();
    storesAtReturnStatements = new IdentityHashMap<>();
    worklist = new Worklist(cfg);
    nodeValues = new IdentityHashMap<>();
    finalLocalValues = new HashMap<>();
    worklist.add(cfg.getEntryBlock());
    List<LocalVariableNode> parameters = null;
    UnderlyingAST underlyingAST = cfg.getUnderlyingAST();
    if (underlyingAST.getKind() == Kind.METHOD) {
        MethodTree tree = ((CFGMethod) underlyingAST).getMethod();
        parameters = new ArrayList<>();
        for (VariableTree p : tree.getParameters()) {
            LocalVariableNode var = new LocalVariableNode(p);
            parameters.add(var);
        // TODO: document that LocalVariableNode has no block that it
        // belongs to
        }
    } else if (underlyingAST.getKind() == Kind.LAMBDA) {
        LambdaExpressionTree lambda = ((CFGLambda) underlyingAST).getLambdaTree();
        parameters = new ArrayList<>();
        for (VariableTree p : lambda.getParameters()) {
            LocalVariableNode var = new LocalVariableNode(p);
            parameters.add(var);
        // TODO: document that LocalVariableNode has no block that it
        // belongs to
        }
    } else {
    // nothing to do
    }
    S initialStore = transferFunction.initialStore(underlyingAST, parameters);
    Block entry = cfg.getEntryBlock();
    thenStores.put(entry, initialStore);
    elseStores.put(entry, initialStore);
    inputs.put(entry, new TransferInput<>(null, this, initialStore));
}
Also used : MethodTree(com.sun.source.tree.MethodTree) CFGMethod(org.checkerframework.dataflow.cfg.UnderlyingAST.CFGMethod) VariableTree(com.sun.source.tree.VariableTree) ArrayList(java.util.ArrayList) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ExceptionBlock(org.checkerframework.dataflow.cfg.block.ExceptionBlock) SpecialBlock(org.checkerframework.dataflow.cfg.block.SpecialBlock) Block(org.checkerframework.dataflow.cfg.block.Block) RegularBlock(org.checkerframework.dataflow.cfg.block.RegularBlock) ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) UnderlyingAST(org.checkerframework.dataflow.cfg.UnderlyingAST)

Aggregations

MethodTree (com.sun.source.tree.MethodTree)91 Tree (com.sun.source.tree.Tree)46 ClassTree (com.sun.source.tree.ClassTree)45 VariableTree (com.sun.source.tree.VariableTree)39 ExpressionTree (com.sun.source.tree.ExpressionTree)36 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)28 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)22 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)20 TreePath (com.sun.source.util.TreePath)20 IdentifierTree (com.sun.source.tree.IdentifierTree)19 NewClassTree (com.sun.source.tree.NewClassTree)19 ReturnTree (com.sun.source.tree.ReturnTree)18 ExecutableElement (javax.lang.model.element.ExecutableElement)17 Symbol (com.sun.tools.javac.code.Symbol)16 ArrayList (java.util.ArrayList)15 AssignmentTree (com.sun.source.tree.AssignmentTree)14 BlockTree (com.sun.source.tree.BlockTree)14 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)14 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)14 MemberSelectTree (com.sun.source.tree.MemberSelectTree)14