Search in sources :

Example 6 with VariableTree

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

the class Overrides method matchMethod.

@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
    MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodTree);
    boolean isVarargs = (methodSymbol.flags() & Flags.VARARGS) != 0;
    Set<MethodSymbol> superMethods = ASTHelpers.findSuperMethods(methodSymbol, state.getTypes());
    // If there are no super methods, we're fine:
    if (superMethods.isEmpty()) {
        return Description.NO_MATCH;
    }
    Iterator<MethodSymbol> superMethodsIterator = superMethods.iterator();
    boolean areSupersVarargs = superMethodsIterator.next().isVarArgs();
    while (superMethodsIterator.hasNext()) {
        if (areSupersVarargs != superMethodsIterator.next().isVarArgs()) {
            // current method is inconsistent with some of its supermethods, so report a match.
            return describeMatch(methodTree);
        }
    }
    // The current method is consistent with all of its supermethods:
    if (isVarargs == areSupersVarargs) {
        return Description.NO_MATCH;
    }
    // The current method is inconsistent with all of its supermethods, so flip the varargs-ness
    // of the current method.
    List<? extends VariableTree> parameterTree = methodTree.getParameters();
    Tree paramType = parameterTree.get(parameterTree.size() - 1).getType();
    CharSequence paramTypeSource = state.getSourceForNode(paramType);
    if (paramTypeSource == null) {
        // No fix if we don't have tree end positions.
        return describeMatch(methodTree);
    }
    Description.Builder descriptionBuilder = buildDescription(methodTree);
    if (isVarargs) {
        descriptionBuilder.addFix(SuggestedFix.replace(paramType, "[]", paramTypeSource.length() - 3, 0));
    } else {
        // There may be a comment that includes a '[' character between the open and closed
        // brackets of the array type.  If so, we don't return a fix.
        int arrayOpenIndex = paramTypeSource.length() - 2;
        while (paramTypeSource.charAt(arrayOpenIndex) == ' ') {
            arrayOpenIndex--;
        }
        if (paramTypeSource.charAt(arrayOpenIndex) == '[') {
            descriptionBuilder.addFix(SuggestedFix.replace(paramType, "...", arrayOpenIndex, 0));
        }
    }
    return descriptionBuilder.build();
}
Also used : Description(com.google.errorprone.matchers.Description) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree)

Example 7 with VariableTree

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

the class BoxedPrimitiveConstructor method maybeCast.

private String maybeCast(VisitorState state, Type type, Type argType) {
    if (doubleAndFloatStatus(state, type, argType) == DoubleAndFloatStatus.PRIMITIVE_DOUBLE_INTO_FLOAT) {
        // e.g.: new Float(3.0d) => (float) 3.0d
        return "(float) ";
    }
    // primitive widening conversions can't be combined with autoboxing, so add a
    // explicit widening cast unless we're sure the expression doesn't get autoboxed
    Tree parent = state.getPath().getParentPath().getLeaf();
    // TODO(cushon): de-dupe with UnnecessaryCast
    Type targetType = parent.accept(new TreeScanner<Type, Void>() {

        @Override
        public Type visitAssignment(AssignmentTree node, Void unused) {
            return getType(node.getVariable());
        }

        @Override
        public Type visitCompoundAssignment(CompoundAssignmentTree node, Void unused) {
            return getType(node.getVariable());
        }

        @Override
        public Type visitReturn(ReturnTree node, Void unused) {
            return getType(state.findEnclosing(MethodTree.class).getReturnType());
        }

        @Override
        public Type visitVariable(VariableTree node, Void unused) {
            return getType(node.getType());
        }
    }, null);
    if (!isSameType(type, argType, state) && !isSameType(targetType, type, state)) {
        return String.format("(%s) ", type);
    }
    return "";
}
Also used : Matchers.toType(com.google.errorprone.matchers.Matchers.toType) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) ASTHelpers.isSameType(com.google.errorprone.util.ASTHelpers.isSameType) Type(com.sun.tools.javac.code.Type) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Example 8 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class TreeUtils method isTreeInStaticScope.

/**
 * Returns whether or not the leaf of the tree path is in a static scope.
 *
 * @param path TreePath whose leaf may or may not be in static scope
 * @return returns whether or not the leaf of the tree path is in a static scope
 */
public static boolean isTreeInStaticScope(TreePath path) {
    MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
    if (enclosingMethod != null) {
        return enclosingMethod.getModifiers().getFlags().contains(Modifier.STATIC);
    }
    // no enclosing method, check for static or initializer block
    BlockTree block = enclosingTopLevelBlock(path);
    if (block != null) {
        return block.isStatic();
    }
    // check if its in a variable initializer
    Tree t = enclosingVariable(path);
    if (t != null) {
        return ((VariableTree) t).getModifiers().getFlags().contains((Modifier.STATIC));
    }
    ClassTree classTree = enclosingClass(path);
    if (classTree != null) {
        return classTree.getModifiers().getFlags().contains((Modifier.STATIC));
    }
    return false;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) BlockTree(com.sun.source.tree.BlockTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 9 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

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 10 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class WholeProgramInferenceScenes method updateInferredParameterType.

/**
 * Updates the parameter type represented by lhs of the method methodTree in the Scene of the
 * receiverTree's enclosing class based on assignments to the parameter inside the method body.
 *
 * <ul>
 *   <li>If the Scene does not contain an annotated type for that parameter, then the type of
 *       the respective value passed as argument in the method call methodInvNode will be added
 *       to the parameter in the Scene.
 *   <li>If the Scene previously contained an annotated type for that parameter, then its new
 *       type will be the LUB between the previous type and the type of the respective value
 *       passed as argument in the method call.
 * </ul>
 *
 * <p>
 *
 * @param lhs the node representing the parameter
 * @param rhs the node being assigned to the parameter
 * @param classTree the tree of the class that contains the parameter
 * @param methodTree the tree of the method that contains the parameter
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used to update the parameter type
 */
@Override
public void updateInferredParameterType(LocalVariableNode lhs, Node rhs, ClassTree classTree, MethodTree methodTree, AnnotatedTypeFactory atf) {
    ClassSymbol classSymbol = getEnclosingClassSymbol(classTree, lhs);
    // https://github.com/typetools/checker-framework/issues/682
    if (classSymbol == null)
        return;
    String className = classSymbol.flatname.toString();
    String jaifPath = helper.getJaifPath(className);
    AClass clazz = helper.getAClass(className, jaifPath);
    String methodName = JVMNames.getJVMMethodName(methodTree);
    AMethod method = clazz.methods.vivify(methodName);
    List<? extends VariableTree> params = methodTree.getParameters();
    // Look-up parameter by name:
    for (int i = 0; i < params.size(); i++) {
        VariableTree vt = params.get(i);
        if (vt.getName().contentEquals(lhs.getName())) {
            Tree treeNode = rhs.getTree();
            if (treeNode == null) {
                // https://github.com/typetools/checker-framework/issues/682
                continue;
            }
            AnnotatedTypeMirror paramATM = atf.getAnnotatedType(vt);
            AnnotatedTypeMirror argATM = atf.getAnnotatedType(treeNode);
            AField param = method.parameters.vivify(i);
            helper.updateAnnotationSetInScene(param.type, atf, jaifPath, argATM, paramATM, TypeUseLocation.PARAMETER);
            break;
        }
    }
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VariableTree(com.sun.source.tree.VariableTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AField(scenelib.annotations.el.AField)

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