Search in sources :

Example 51 with VariableTree

use of com.sun.source.tree.VariableTree 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)

Example 52 with VariableTree

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

the class TreeBuilder method buildVariableDecl.

/**
     * Builds an AST Tree to declare and initialize a variable, with no modifiers.
     *
     * @param type  the type of the variable
     * @param name  the name of the variable
     * @param owner  the element containing the new symbol
     * @param initializer  the initializer expression
     * @return  a VariableDeclTree declaring the new variable
     */
public VariableTree buildVariableDecl(TypeMirror type, String name, Element owner, ExpressionTree initializer) {
    DetachedVarSymbol sym = new DetachedVarSymbol(0, names.fromString(name), (Type) type, (Symbol) owner);
    VariableTree tree = maker.VarDef(sym, (JCTree.JCExpression) initializer);
    sym.setDeclaration(tree);
    return tree;
}
Also used : VariableTree(com.sun.source.tree.VariableTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 53 with VariableTree

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

the class WrongParameterPackage method describe.

public Description describe(MethodTree tree, VisitorState state) {
    SuggestedFix.Builder builder = null;
    MethodSymbol method = ASTHelpers.getSymbol(tree);
    if (supermethod == null) {
        throw new IllegalStateException("Matching supermethod was not found");
    }
    for (int x = 0; x < method.params().size(); x++) {
        Type methodParamType = method.params().get(x).type;
        Type supermethodParamType = supermethod.params().get(x).type;
        if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) && !state.getTypes().isSameType(methodParamType, supermethodParamType)) {
            VariableTree param = tree.getParameters().get(x);
            // TODO(user): Name is most likely more qualified than necessary.
            Name replacement = supermethodParamType.tsym.getQualifiedName();
            if (builder == null) {
                builder = SuggestedFix.builder();
            }
            builder.replace(param, replacement + " " + param.getName());
        }
    }
    return (builder != null) ? describeMatch(tree, builder.build()) : describeMatch(tree);
}
Also used : Type(com.sun.tools.javac.code.Type) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VariableTree(com.sun.source.tree.VariableTree) Name(com.sun.tools.javac.util.Name)

Example 54 with VariableTree

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

the class IsLoggableTagLength method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (!IS_LOGGABLE_CALL.matches(tree, state)) {
        return NO_MATCH;
    }
    ExpressionTree tagArg = tree.getArguments().get(0);
    // Check for constant value.
    String tagConstantValue = ASTHelpers.constValue(tagArg, String.class);
    if (tagConstantValue != null) {
        return isValidTag(tagConstantValue) ? NO_MATCH : describeMatch(tagArg);
    }
    // Check for class literal simple name (e.g. MyClass.class.getSimpleName().
    ExpressionTree tagExpr = tagArg;
    // If the tag argument is a final field, retrieve the initializer.
    if (kindIs(IDENTIFIER).matches(tagArg, state)) {
        VariableTree declaredField = findEnclosingIdentifier((IdentifierTree) tagArg, state);
        if (declaredField == null || !hasModifier(FINAL).matches(declaredField, state)) {
            return NO_MATCH;
        }
        tagExpr = declaredField.getInitializer();
    }
    if (GET_SIMPLE_NAME_CALL.matches(tagExpr, state) && RECEIVER_IS_CLASS_LITERAL.matches((MethodInvocationTree) tagExpr, state)) {
        String tagName = getSymbol(getReceiver(getReceiver(tagExpr))).getSimpleName().toString();
        return isValidTag(tagName) ? NO_MATCH : describeMatch(tagArg);
    }
    return NO_MATCH;
}
Also used : MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 55 with VariableTree

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

the class FormatStringAnnotationChecker method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    Type stringType = state.getSymtab().stringType;
    boolean isFormatMethod = ASTHelpers.hasAnnotation(ASTHelpers.getSymbol(tree), FormatMethod.class, state);
    boolean foundFormatString = false;
    boolean foundString = false;
    for (VariableTree param : tree.getParameters()) {
        VarSymbol paramSymbol = ASTHelpers.getSymbol(param);
        boolean isStringParam = ASTHelpers.isSameType(paramSymbol.type, stringType, state);
        if (isStringParam) {
            foundString = true;
        }
        if (ASTHelpers.hasAnnotation(paramSymbol, FormatString.class, state)) {
            if (!isFormatMethod) {
                return buildDescription(tree).setMessage("A parameter can only be annotated @FormatString in a method annotated " + "@FormatMethod: " + param).build();
            }
            if (!isStringParam) {
                return buildDescription(param).setMessage("Only strings can be annotated @FormatString.").build();
            }
            if (foundFormatString) {
                return buildDescription(tree).setMessage("A method cannot have more than one @FormatString parameter.").build();
            }
            foundFormatString = true;
        }
    }
    if (isFormatMethod && !foundString) {
        return buildDescription(tree).setMessage("An @FormatMethod must contain at least one String parameter.").build();
    }
    return Description.NO_MATCH;
}
Also used : Type(com.sun.tools.javac.code.Type) VariableTree(com.sun.source.tree.VariableTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Aggregations

VariableTree (com.sun.source.tree.VariableTree)86 Tree (com.sun.source.tree.Tree)43 ClassTree (com.sun.source.tree.ClassTree)37 MethodTree (com.sun.source.tree.MethodTree)37 ExpressionTree (com.sun.source.tree.ExpressionTree)34 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)25 NewClassTree (com.sun.source.tree.NewClassTree)23 TreePath (com.sun.source.util.TreePath)23 IdentifierTree (com.sun.source.tree.IdentifierTree)22 JCTree (com.sun.tools.javac.tree.JCTree)21 MemberSelectTree (com.sun.source.tree.MemberSelectTree)19 AssignmentTree (com.sun.source.tree.AssignmentTree)17 ArrayList (java.util.ArrayList)17 BlockTree (com.sun.source.tree.BlockTree)16 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)13 Type (com.sun.tools.javac.code.Type)13 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)12 ReturnTree (com.sun.source.tree.ReturnTree)12 StatementTree (com.sun.source.tree.StatementTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)12