Search in sources :

Example 11 with Type

use of com.sun.tools.javac.code.Type in project bazel by bazelbuild.

the class TreeBuilder method buildNextMethodAccess.

/**
     * Builds an AST Tree to access the next() method of an iterator.
     *
     * @param iteratorExpr  an expression whose type is a subtype of Iterator
     * @return  a MemberSelectTree that accesses the next() method of
     *    the expression
     */
public MemberSelectTree buildNextMethodAccess(ExpressionTree iteratorExpr) {
    DeclaredType exprType = (DeclaredType) InternalUtils.typeOf(iteratorExpr);
    assert exprType != null : "expression must be of declared type Iterator<>";
    TypeElement exprElement = (TypeElement) exprType.asElement();
    // Find the next() method of the iterator type
    Symbol.MethodSymbol nextMethod = null;
    for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(exprElement))) {
        Name methodName = method.getSimpleName();
        if (method.getParameters().size() == 0) {
            if (methodName.contentEquals("next")) {
                nextMethod = (Symbol.MethodSymbol) method;
            }
        }
    }
    assert nextMethod != null : "no next method declared for expression type";
    Type.MethodType methodType = (Type.MethodType) nextMethod.asType();
    Symbol.TypeSymbol methodClass = methodType.asElement();
    Type elementType;
    if (exprType.getTypeArguments().size() > 0) {
        elementType = (Type) exprType.getTypeArguments().get(0);
    } else {
        elementType = symtab.objectType;
    }
    // Replace the next method's generic return type with
    // the actual element type of the expression.
    Type.MethodType updatedMethodType = new Type.MethodType(com.sun.tools.javac.util.List.<Type>nil(), elementType, com.sun.tools.javac.util.List.<Type>nil(), methodClass);
    JCTree.JCFieldAccess nextAccess = (JCTree.JCFieldAccess) maker.Select((JCTree.JCExpression) iteratorExpr, nextMethod);
    nextAccess.setType(updatedMethodType);
    return nextAccess;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Symbol(com.sun.tools.javac.code.Symbol) ExecutableElement(javax.lang.model.element.ExecutableElement) JCTree(com.sun.tools.javac.tree.JCTree) Name(javax.lang.model.element.Name) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(javax.lang.model.type.ArrayType) Type(com.sun.tools.javac.code.Type) DeclaredType(javax.lang.model.type.DeclaredType)

Example 12 with Type

use of com.sun.tools.javac.code.Type in project bazel by bazelbuild.

the class TreeBuilder method buildVariableDecl.

/**
     * Builds an AST Tree to declare and initialize a variable.  The
     * type of the variable is specified by a Tree.
     *
     * @param type  the type of the variable, as a Tree
     * @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(Tree type, String name, Element owner, ExpressionTree initializer) {
    Type typeMirror = (Type) InternalUtils.typeOf(type);
    DetachedVarSymbol sym = new DetachedVarSymbol(0, names.fromString(name), typeMirror, (Symbol) owner);
    JCTree.JCModifiers mods = maker.Modifiers(0);
    JCTree.JCVariableDecl decl = maker.VarDef(mods, sym.name, (JCTree.JCExpression) type, (JCTree.JCExpression) initializer);
    decl.setType(typeMirror);
    decl.sym = sym;
    sym.setDeclaration(decl);
    return decl;
}
Also used : DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(javax.lang.model.type.ArrayType) Type(com.sun.tools.javac.code.Type) JCTree(com.sun.tools.javac.tree.JCTree)

Example 13 with Type

use of com.sun.tools.javac.code.Type in project bazel by bazelbuild.

the class TreeBuilder method buildValueOfMethodAccess.

/**
     * Builds an AST Tree to access the valueOf() method of boxed type
     * such as Short or Float.
     *
     * @param expr  an expression whose type is a boxed type
     * @return  a MemberSelectTree that accesses the valueOf() method of
     *    the expression
     */
public MemberSelectTree buildValueOfMethodAccess(Tree expr) {
    TypeMirror boxedType = InternalUtils.typeOf(expr);
    assert TypesUtils.isBoxedPrimitive(boxedType);
    // Find the valueOf(unboxedType) method of the boxed type
    Symbol.MethodSymbol valueOfMethod = getValueOfMethod(env, boxedType);
    Type.MethodType methodType = (Type.MethodType) valueOfMethod.asType();
    JCTree.JCFieldAccess valueOfAccess = (JCTree.JCFieldAccess) maker.Select((JCTree.JCExpression) expr, valueOfMethod);
    valueOfAccess.setType(methodType);
    return valueOfAccess;
}
Also used : DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(javax.lang.model.type.ArrayType) Type(com.sun.tools.javac.code.Type) TypeMirror(javax.lang.model.type.TypeMirror) Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree)

Example 14 with Type

use of com.sun.tools.javac.code.Type in project error-prone by google.

the class EqualsIncompatibleType method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree invocationTree, final VisitorState state) {
    if (!STATIC_EQUALS_INVOCATION_MATCHER.matches(invocationTree, state) && !INSTANCE_EQUALS_INVOCATION_MATCHER.matches(invocationTree, state)) {
        return Description.NO_MATCH;
    }
    // This is the type of the object on which the java.lang.Object.equals() method
    // is called, either directly or indirectly via a static utility method. In the latter,
    // it is the type of the first argument to the static method.
    Type receiverType;
    // This is the type of the argument to the java.lang.Object.equals() method.
    // In case a static utility method is used, it is the type of the second argument
    // to this method.
    Type argumentType;
    if (STATIC_EQUALS_INVOCATION_MATCHER.matches(invocationTree, state)) {
        receiverType = ASTHelpers.getType(invocationTree.getArguments().get(0));
        argumentType = ASTHelpers.getType(invocationTree.getArguments().get(1));
    } else {
        receiverType = ASTHelpers.getReceiverType(invocationTree);
        argumentType = ASTHelpers.getType(invocationTree.getArguments().get(0));
    }
    if (receiverType == null || argumentType == null) {
        return Description.NO_MATCH;
    }
    // 1.7: java.lang.Object can be cast to primitives (implicitly through the boxed primitive type)
    if (ASTHelpers.isCastable(argumentType, receiverType, state)) {
        return Description.NO_MATCH;
    }
    // Otherwise, we explore the superclasses of the receiver type as well as the interfaces it
    // implements and we collect all overrides of java.lang.Object.equals(). If one of those
    // overrides is inherited by the argument, then we don't flag the equality test.
    final Types types = state.getTypes();
    Predicate<MethodSymbol> equalsPredicate = new Predicate<MethodSymbol>() {

        @Override
        public boolean apply(MethodSymbol methodSymbol) {
            return !methodSymbol.isStatic() && ((methodSymbol.flags() & Flags.SYNTHETIC) == 0) && types.isSameType(methodSymbol.getReturnType(), state.getSymtab().booleanType) && methodSymbol.getParameters().size() == 1 && types.isSameType(methodSymbol.getParameters().get(0).type, state.getSymtab().objectType);
        }
    };
    Name equalsName = state.getName("equals");
    Set<MethodSymbol> overridesOfEquals = ASTHelpers.findMatchingMethods(equalsName, equalsPredicate, receiverType, types);
    ClassSymbol argumentClass = (ClassSymbol) argumentType.tsym;
    for (MethodSymbol method : overridesOfEquals) {
        ClassSymbol methodClass = method.enclClass();
        if (argumentClass.isSubClass(methodClass, types) && !methodClass.equals(state.getSymtab().objectType.tsym) && !methodClass.equals(state.getSymtab().enumSym)) {
            // with the receiver that implements an override of java.lang.Object.equals().
            return Description.NO_MATCH;
        }
    }
    // assertFalse(objOfReceiverType.equals(objOfArgumentType))
    if (ASSERT_FALSE_MATCHER.matches(state.getPath().getParentPath().getLeaf(), state)) {
        return Description.NO_MATCH;
    }
    // When we reach this point, we know that the two following facts hold:
    // (1) The types of the receiver and the argument to the eventual invocation of
    //     java.lang.Object.equals() are incompatible.
    // (2) No common superclass (other than java.lang.Object) or interface of the receiver and the
    //     argument defines an override of java.lang.Object.equals().
    // This equality test almost certainly evaluates to false, which is very unlikely to be the
    // programmer's intent. Hence, this is reported as an error. There is no sensible fix to suggest
    // in this situation.
    Description.Builder description = buildDescription(invocationTree);
    description.setMessage("Calling " + ASTHelpers.getSymbol(invocationTree).getSimpleName() + " on incompatible types " + receiverType + " and " + argumentType);
    return description.build();
}
Also used : Types(com.sun.tools.javac.code.Types) Matchers.toType(com.google.errorprone.matchers.Matchers.toType) Matchers.isSameType(com.google.errorprone.matchers.Matchers.isSameType) Type(com.sun.tools.javac.code.Type) Description(com.google.errorprone.matchers.Description) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Predicate(com.google.common.base.Predicate) Name(com.sun.tools.javac.util.Name)

Example 15 with Type

use of com.sun.tools.javac.code.Type in project error-prone by google.

the class JUnit3FloatingPointComparisonWithoutDelta method removeMessageArgumentIfPresent.

/**
   * Removes the message argument if it is present.
   */
private void removeMessageArgumentIfPresent(VisitorState state, List<Type> argumentTypes) {
    if (argumentTypes.size() == 2) {
        return;
    }
    Types types = state.getTypes();
    Type firstType = argumentTypes.get(0);
    if (types.isSameType(firstType, state.getSymtab().stringType)) {
        argumentTypes.remove(0);
    }
}
Also used : Types(com.sun.tools.javac.code.Types) Type(com.sun.tools.javac.code.Type)

Aggregations

Type (com.sun.tools.javac.code.Type)253 Symbol (com.sun.tools.javac.code.Symbol)64 ClassType (com.sun.tools.javac.code.Type.ClassType)55 ArrayType (com.sun.tools.javac.code.Type.ArrayType)47 MethodType (com.sun.tools.javac.code.Type.MethodType)42 WildcardType (com.sun.tools.javac.code.Type.WildcardType)42 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)36 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)33 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)32 JCTree (com.sun.tools.javac.tree.JCTree)27 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)24 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)23 Types (com.sun.tools.javac.code.Types)22 ExpressionTree (com.sun.source.tree.ExpressionTree)21 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)17 ArrayList (java.util.ArrayList)17 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)16 Tree (com.sun.source.tree.Tree)14 Name (com.sun.tools.javac.util.Name)14 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)13