Search in sources :

Example 16 with Types

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

the class FunctionalInterfaceClash method functionalInterfaceSignature.

private static String functionalInterfaceSignature(VisitorState state, Type type) {
    Types types = state.getTypes();
    if (!maybeFunctionalInterface(type, types, state)) {
        return Signatures.descriptor(type, types);
    }
    Type descriptorType = types.findDescriptorType(type);
    List<Type> fiparams = descriptorType.getParameterTypes();
    // Implicitly typed block-statement-bodied lambdas are potentially compatible with
    // void-returning and value-returning functional interface types, so we don't consider return
    // types in general. The except is nullary functional interfaces, since the lambda parameters
    // will never be implicitly typed.
    String result = fiparams.isEmpty() ? Signatures.descriptor(descriptorType.getReturnType(), types) : "_";
    return String.format("(%s)->%s", fiparams.stream().map(t -> Signatures.descriptor(t, types)).collect(joining(",")), result);
}
Also used : Types(com.sun.tools.javac.code.Types) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Type(com.sun.tools.javac.code.Type)

Example 17 with Types

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

Example 18 with Types

use of com.sun.tools.javac.code.Types in project j2objc by google.

the class TreeConverter method convertFunctionalExpression.

private TreeNode convertFunctionalExpression(JCFunctionalExpression node, TreePath parent, FunctionalExpression newNode) {
    List<? extends TypeMirror> targets = getTargets(node, parent);
    for (TypeMirror type : targets) {
        newNode.addTargetType(type);
    }
    Types types = Types.instance(((com.sun.tools.javac.api.BasicJavacTask) env.task()).getContext());
    return newNode.setTypeMirror(targets.iterator().next()).setDescriptor(new ExecutablePair((ExecutableElement) types.findDescriptorSymbol(((com.sun.tools.javac.code.Type) targets.get(0)).tsym), (ExecutableType) node.getDescriptorType(types)));
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) Types(com.sun.tools.javac.code.Types) ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 19 with Types

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

the class InternalUtils method greatestLowerBound.

/**
     * Returns the greatest lower bound of two {@link TypeMirror}s.
     *
     * @param processingEnv The {@link ProcessingEnvironment} to use.
     * @param tm1 A {@link TypeMirror}.
     * @param tm2 A {@link 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) JCAnnotatedType(com.sun.tools.javac.tree.JCTree.JCAnnotatedType) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 20 with Types

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

the class BundleDeserializationCast method matchTypeCast.

@Override
public Description matchTypeCast(TypeCastTree tree, VisitorState state) {
    if (!BUNDLE_DESERIALIZATION_CAST_EXPRESSION.matches(tree, state)) {
        return NO_MATCH;
    }
    Tree targetType = tree.getType();
    // Casting to primitive types shouldn't cause issues since they extend no type and are final.
    if (isPrimitiveType().matches(targetType, state)) {
        return NO_MATCH;
    }
    /*
     * Ordering of these checks determines precedence of types (which type *should* be cast to).
     * Deduced by inspecting serialization code, see
     * https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/Parcel.java#1377
     * Simply allow specially handled final types, and check that other specially handled types
     * are cast to their base types.
     *
     * There is no documentation of what types are safe to cast to, so this code is paralleling the
     * code cited above to emulate the same logic in order to produce the correct behavior.
     */
    if (isSameType("java.lang.String").matches(targetType, state)) {
        return NO_MATCH;
    }
    if (isSubtypeOf("java.util.Map").matches(targetType, state)) {
        // Make an exception for HashMap.
        return anyOf(isSameType("java.util.Map"), isSameType("java.util.HashMap")).matches(targetType, state) ? NO_MATCH : getDescriptionForType(tree, "Map");
    }
    // All Parcelables handled after this point have their types preserved.
    if (isSubtypeOf("android.os.Parcelable").matches(targetType, state)) {
        return NO_MATCH;
    }
    if (isSubtypeOf("java.lang.CharSequence").matches(targetType, state)) {
        return isSameType("java.lang.CharSequence").matches(targetType, state) ? NO_MATCH : getDescriptionForType(tree, "CharSequence");
    }
    if (isSubtypeOf("java.util.List").matches(targetType, state)) {
        // Make an exception for ArrayList.
        return anyOf(isSameType("java.util.List"), isSameType("java.util.ArrayList")).matches(targetType, state) ? NO_MATCH : getDescriptionForType(tree, "List");
    }
    if (isSubtypeOf("android.util.SparseArray").matches(targetType, state)) {
        return isSameType("android.util.SparseArray").matches(targetType, state) ? NO_MATCH : getDescriptionForType(tree, "SparseArray");
    }
    // Check component types of arrays. The only type that may cause problems is CharSequence[].
    if (isArrayType().matches(targetType, state)) {
        Type componentType = ((ArrayType) getType(targetType)).getComponentType();
        Types types = state.getTypes();
        Type charSequenceType = typeFromString("java.lang.CharSequence").get(state);
        Type stringType = typeFromString("java.lang.String").get(state);
        // in the serialization code.
        if (types.isSubtype(componentType, charSequenceType) && !types.isSameType(componentType, charSequenceType) && !types.isSameType(componentType, stringType)) {
            return getDescriptionForType(tree, "CharSequence[]");
        }
    }
    return NO_MATCH;
}
Also used : ArrayType(com.sun.tools.javac.code.Type.ArrayType) Matchers.isArrayType(com.google.errorprone.matchers.Matchers.isArrayType) Types(com.sun.tools.javac.code.Types) Matchers.isPrimitiveType(com.google.errorprone.matchers.Matchers.isPrimitiveType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Matchers.isSameType(com.google.errorprone.matchers.Matchers.isSameType) Type(com.sun.tools.javac.code.Type) Matchers.isArrayType(com.google.errorprone.matchers.Matchers.isArrayType) TypeCastTree(com.sun.source.tree.TypeCastTree) Tree(com.sun.source.tree.Tree)

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