Search in sources :

Example 6 with Types

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

the class BadAnnotationImplementation method matchClass.

@Override
public Description matchClass(ClassTree classTree, final VisitorState state) {
    if (!CLASS_TREE_MATCHER.matches(classTree, state)) {
        return Description.NO_MATCH;
    }
    // If this is an enum that is trying to implement Annotation, give a special error message.
    if (classTree.getKind() == Kind.ENUM) {
        return buildDescription(classTree).setMessage("Enums cannot correctly implement Annotation because their equals and hashCode " + "methods are final. Consider using AutoAnnotation instead of implementing " + "Annotation by hand.").build();
    }
    // Otherwise walk up type hierarchy looking for equals and hashcode methods
    MethodSymbol equals = null;
    MethodSymbol hashCode = null;
    final Types types = state.getTypes();
    Name equalsName = state.getName("equals");
    Predicate<MethodSymbol> equalsPredicate = new Predicate<MethodSymbol>() {

        @Override
        public boolean apply(MethodSymbol methodSymbol) {
            return !methodSymbol.isStatic() && ((methodSymbol.flags() & Flags.SYNTHETIC) == 0) && ((methodSymbol.flags() & Flags.ABSTRACT) == 0) && methodSymbol.getParameters().size() == 1 && types.isSameType(methodSymbol.getParameters().get(0).type, state.getSymtab().objectType);
        }
    };
    Name hashCodeName = state.getName("hashCode");
    Predicate<MethodSymbol> hashCodePredicate = new Predicate<MethodSymbol>() {

        @Override
        public boolean apply(MethodSymbol methodSymbol) {
            return !methodSymbol.isStatic() && ((methodSymbol.flags() & Flags.SYNTHETIC) == 0) && ((methodSymbol.flags() & Flags.ABSTRACT) == 0) && methodSymbol.getParameters().isEmpty();
        }
    };
    for (Type sup : types.closure(ASTHelpers.getSymbol(classTree).type)) {
        if (equals == null) {
            equals = getMatchingMethod(sup, equalsName, equalsPredicate);
        }
        if (hashCode == null) {
            hashCode = getMatchingMethod(sup, hashCodeName, hashCodePredicate);
        }
    }
    Verify.verifyNotNull(equals);
    Verify.verifyNotNull(hashCode);
    Symbol objectSymbol = state.getSymtab().objectType.tsym;
    if (equals.owner.equals(objectSymbol) || hashCode.owner.equals(objectSymbol)) {
        return describeMatch(classTree);
    }
    return Description.NO_MATCH;
}
Also used : Types(com.sun.tools.javac.code.Types) 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) Name(com.sun.tools.javac.util.Name) Predicate(com.google.common.base.Predicate)

Example 7 with Types

use of com.sun.tools.javac.code.Types 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 8 with Types

use of com.sun.tools.javac.code.Types in project lombok by rzwitserloot.

the class JavacResolution method ifTypeIsIterableToComponent.

public static Type ifTypeIsIterableToComponent(Type type, JavacAST ast) {
    if (type == null)
        return null;
    Types types = Types.instance(ast.getContext());
    Symtab syms = Symtab.instance(ast.getContext());
    Type boundType = ReflectiveAccess.Types_upperBound(types, type);
    // Type boundType = types.upperBound(type);
    Type elemTypeIfArray = types.elemtype(boundType);
    if (elemTypeIfArray != null)
        return elemTypeIfArray;
    Type base = types.asSuper(boundType, syms.iterableType.tsym);
    if (base == null)
        return syms.objectType;
    List<Type> iterableParams = base.allparams();
    return iterableParams.isEmpty() ? syms.objectType : ReflectiveAccess.Types_upperBound(types, iterableParams.head);
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) Types(com.sun.tools.javac.code.Types) ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type)

Example 9 with Types

use of com.sun.tools.javac.code.Types in project st-js by st-js.

the class InternalUtils method leastUpperBound.

/**
 * Returns the least upper bound of two {@link javax.lang.model.type.TypeMirror}s.
 *
 * @param processingEnv
 *            The {@link javax.annotation.processing.ProcessingEnvironment} to use.
 * @param tm1
 *            A {@link javax.lang.model.type.TypeMirror}.
 * @param tm2
 *            A {@link javax.lang.model.type.TypeMirror}.
 * @return The least upper bound of {@code tm1} and {@code tm2}.
 */
public static TypeMirror leastUpperBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
    Type t1 = (Type) tm1;
    Type t2 = (Type) tm2;
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
    Types types = Types.instance(javacEnv.getContext());
    if (types.isSameType(t1, t2)) {
        // Special case if the two types are equal.
        return t1;
    }
    // Handle the 'null' type manually (not done by types.lub).
    if (t1.getKind() == TypeKind.NULL) {
        return t2;
    }
    if (t2.getKind() == TypeKind.NULL) {
        return t1;
    }
    // Special case for primitives.
    if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
        if (types.isAssignable(t1, t2)) {
            return t2;
        } else if (types.isAssignable(t2, t1)) {
            return t1;
        } else {
            return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
        }
    }
    if (t1.getKind() == TypeKind.WILDCARD) {
        WildcardType wc1 = (WildcardType) t1;
        Type bound = (Type) wc1.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t1 = bound;
    }
    if (t2.getKind() == TypeKind.WILDCARD) {
        WildcardType wc2 = (WildcardType) t2;
        Type bound = (Type) wc2.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t2 = bound;
    }
    return types.lub(t1, t2);
}
Also used : Types(com.sun.tools.javac.code.Types) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(javax.lang.model.type.WildcardType) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) Elements(javax.lang.model.util.Elements)

Example 10 with Types

use of com.sun.tools.javac.code.Types in project st-js by st-js.

the class InternalUtils method greatestLowerBound.

/**
 * Returns the greatest lower bound of two {@link javax.lang.model.type.TypeMirror}s.
 *
 * @param processingEnv
 *            The {@link javax.annotation.processing.ProcessingEnvironment} to use.
 * @param tm1
 *            A {@link javax.lang.model.type.TypeMirror}.
 * @param tm2
 *            A {@link javax.lang.model.type.TypeMirror}.
 * @return The greatest lower bound of {@code tm1} and {@code tm2}.
 */
public static TypeMirror greatestLowerBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
    Type t1 = (Type) tm1;
    Type t2 = (Type) tm2;
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
    Types types = Types.instance(javacEnv.getContext());
    if (types.isSameType(t1, t2)) {
        // Special case if the two types are equal.
        return t1;
    }
    // Handle the 'null' type manually.
    if (t1.getKind() == TypeKind.NULL) {
        return t1;
    }
    if (t2.getKind() == TypeKind.NULL) {
        return t2;
    }
    // Special case for primitives.
    if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
        if (types.isAssignable(t1, t2)) {
            return t1;
        } else if (types.isAssignable(t2, t1)) {
            return t2;
        } else {
            // instead.
            return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
        }
    }
    if (t1.getKind() == TypeKind.WILDCARD) {
        return t2;
    }
    if (t2.getKind() == TypeKind.WILDCARD) {
        return t1;
    }
    return types.glb(t1, t2);
}
Also used : Types(com.sun.tools.javac.code.Types) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Aggregations

Types (com.sun.tools.javac.code.Types)39 Type (com.sun.tools.javac.code.Type)29 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)10 Tree (com.sun.source.tree.Tree)9 Symbol (com.sun.tools.javac.code.Symbol)9 VisitorState (com.google.errorprone.VisitorState)8 ASTHelpers.getType (com.google.errorprone.util.ASTHelpers.getType)7 ExpressionTree (com.sun.source.tree.ExpressionTree)6 Symtab (com.sun.tools.javac.code.Symtab)6 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)5 Description (com.google.errorprone.matchers.Description)5 ClassTree (com.sun.source.tree.ClassTree)5 MethodTree (com.sun.source.tree.MethodTree)5 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)5 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)5 ArrayList (java.util.ArrayList)5 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)4 List (java.util.List)4 BugPattern (com.google.errorprone.BugPattern)3