Search in sources :

Example 46 with Type

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

the class DescendantOf method matches.

@Override
public boolean matches(ExpressionTree expressionTree, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(expressionTree);
    if (sym == null) {
        return false;
    }
    if (!(sym instanceof MethodSymbol)) {
        throw new IllegalArgumentException("DescendantOf matcher expects a method call but found " + sym.getClass() + ". Expression: " + expressionTree);
    }
    if (sym.isStatic()) {
        return false;
    }
    if (methodName.equals(sym.toString())) {
        Type accessedReferenceType = sym.owner.type;
        Type collectionType = state.getTypeFromString(fullClassName);
        if (collectionType != null) {
            return state.getTypes().isSubtype(accessedReferenceType, state.getTypes().erasure(collectionType));
        }
    }
    return false;
}
Also used : Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Example 47 with Type

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

the class UTypeVarIdent method defaultAction.

@Override
protected Choice<Unifier> defaultAction(Tree target, Unifier unifier) {
    JCExpression expr = (JCExpression) target;
    Type targetType = expr.type;
    if (targetType == null) {
        return Choice.none();
    }
    @Nullable TypeWithExpression boundType = unifier.getBinding(key());
    if (boundType == null) {
        unifier.putBinding(key(), expr.accept(QUALIFIED_FROM_PACKAGE, null) ? TypeWithExpression.create(targetType) : /* use the ImportPolicy to refer to this type */
        TypeWithExpression.create(targetType, expr));
        return Choice.of(unifier);
    } else if (unifier.types().isSameType(targetType, boundType.type())) {
        return Choice.of(unifier);
    }
    return Choice.none();
}
Also used : Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TypeWithExpression(com.google.errorprone.refaster.UTypeVar.TypeWithExpression) Nullable(javax.annotation.Nullable)

Example 48 with Type

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

the class Template method expectedTypes.

/**
   * Returns a list of the expected types to be matched.  This consists of the argument types from
   * the @BeforeTemplate method, concatenated with the return types of expression placeholders, 
   * sorted by the name of the placeholder method.
   *
   * @throws CouldNotResolveImportException if a referenced type could not be resolved
   */
protected List<Type> expectedTypes(Inliner inliner) throws CouldNotResolveImportException {
    ArrayList<Type> result = new ArrayList<>();
    ImmutableList<UType> types = expressionArgumentTypes().values().asList();
    ImmutableList<String> argNames = expressionArgumentTypes().keySet().asList();
    for (int i = 0; i < argNames.size(); i++) {
        String argName = argNames.get(i);
        Optional<JCExpression> singleBinding = inliner.getOptionalBinding(new UFreeIdent.Key(argName));
        if (!singleBinding.isPresent()) {
            Optional<java.util.List<JCExpression>> exprs = inliner.getOptionalBinding(new URepeated.Key(argName));
            if (!exprs.isPresent() || exprs.get().isEmpty()) {
                // It is a repeated template variable and matches no expressions.
                continue;
            }
        }
        result.add(types.get(i).inline(inliner));
    }
    for (PlaceholderExpressionKey key : Ordering.natural().immutableSortedCopy(Iterables.filter(inliner.bindings.keySet(), PlaceholderExpressionKey.class))) {
        result.add(key.method.returnType().inline(inliner));
    }
    return List.from(result);
}
Also used : ArrayList(java.util.ArrayList) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) List(com.sun.tools.javac.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) PlaceholderExpressionKey(com.google.errorprone.refaster.PlaceholderMethod.PlaceholderExpressionKey)

Example 49 with Type

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

the class Template method checkBounds.

private boolean checkBounds(Unifier unifier, Inliner inliner, Warner warner) throws CouldNotResolveImportException {
    Types types = unifier.types();
    ListBuffer<Type> varsBuffer = new ListBuffer<>();
    ListBuffer<Type> bindingsBuffer = new ListBuffer<>();
    for (UTypeVar typeVar : typeVariables(unifier.getContext())) {
        varsBuffer.add(inliner.inlineAsVar(typeVar));
        bindingsBuffer.add(unifier.getBinding(typeVar.key()).type());
    }
    List<Type> vars = varsBuffer.toList();
    List<Type> bindings = bindingsBuffer.toList();
    for (UTypeVar typeVar : typeVariables(unifier.getContext())) {
        List<Type> bounds = types.getBounds(inliner.inlineAsVar(typeVar));
        bounds = types.subst(bounds, vars, bindings);
        if (!types.isSubtypeUnchecked(unifier.getBinding(typeVar.key()).type(), bounds, warner)) {
            logger.log(FINE, String.format("%s is not a subtype of %s", inliner.getBinding(typeVar.key()), bounds));
            return false;
        }
    }
    return true;
}
Also used : Types(com.sun.tools.javac.code.Types) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 50 with Type

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

the class Template method actualTypes.

/**
   * Returns a list of the actual types to be matched.  This consists of the types of the 
   * expressions bound to the @BeforeTemplate method parameters, concatenated with the types
   * of the expressions bound to expression placeholders, sorted by the name of the placeholder 
   * method.
   */
protected List<Type> actualTypes(Inliner inliner) {
    ArrayList<Type> result = new ArrayList<>();
    ImmutableList<String> argNames = expressionArgumentTypes().keySet().asList();
    for (int i = 0; i < expressionArgumentTypes().size(); i++) {
        String argName = argNames.get(i);
        Optional<JCExpression> singleBinding = inliner.getOptionalBinding(new UFreeIdent.Key(argName));
        if (singleBinding.isPresent()) {
            result.add(singleBinding.get().type);
        } else {
            Optional<java.util.List<JCExpression>> exprs = inliner.getOptionalBinding(new URepeated.Key(argName));
            if (exprs.isPresent() && !exprs.get().isEmpty()) {
                Type[] exprTys = new Type[exprs.get().size()];
                for (int j = 0; j < exprs.get().size(); j++) {
                    exprTys[j] = exprs.get().get(j).type;
                }
                // Get the least upper bound of the types of all expressions that the argument matches.
                // In the special case where exprs is empty, returns the "bottom" type, which is a
                // subtype of everything.
                result.add(inliner.types().lub(List.from(exprTys)));
            }
        }
    }
    for (PlaceholderExpressionKey key : Ordering.natural().immutableSortedCopy(Iterables.filter(inliner.bindings.keySet(), PlaceholderExpressionKey.class))) {
        result.add(inliner.getBinding(key).type);
    }
    return List.from(result);
}
Also used : ArrayList(java.util.ArrayList) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) List(com.sun.tools.javac.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) PlaceholderExpressionKey(com.google.errorprone.refaster.PlaceholderMethod.PlaceholderExpressionKey)

Aggregations

Type (com.sun.tools.javac.code.Type)254 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