Search in sources :

Example 16 with WildcardType

use of javax.lang.model.type.WildcardType in project tiger by google.

the class Utils method isPublicRecurively.

/**
 * Returns true is the type includes only public types.
 */
public boolean isPublicRecurively(TypeMirror type) {
    switch(type.getKind()) {
        case TYPEVAR:
            throw new RuntimeException("non-specilized type found: " + type);
        case DECLARED:
            if (!isSelfAndEnclosingPublic((TypeElement) ((DeclaredType) type).asElement())) {
                return false;
            }
            for (TypeMirror i : ((DeclaredType) type).getTypeArguments()) {
                if (!isPublicRecurively(i)) {
                    return false;
                }
            }
            break;
        case WILDCARD:
            WildcardType wildcardType = (WildcardType) type;
            TypeMirror extendsBound = wildcardType.getExtendsBound();
            if (extendsBound != null && !isPublicRecurively(extendsBound)) {
                return false;
            }
            TypeMirror superBound = wildcardType.getSuperBound();
            if (superBound != null && !isPublicRecurively(superBound)) {
                return false;
            }
            break;
        default:
            break;
    }
    return true;
}
Also used : WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 17 with WildcardType

use of javax.lang.model.type.WildcardType in project checker-framework by typetools.

the class CFAbstractValue method getEffectTypeVar.

/**
 * Returns the AnnotatedTypeVariable associated with the given TypeMirror or null.
 *
 * <p>If TypeMirror is a type variable, then the AnnotatedTypeVariable return is the declaration
 * of that TypeMirror. If the TypeMirror is a wildcard that extends a type variable, the
 * AnnotatedTypeVariable return is the declaration of that type variable. Otherwise, null is
 * returned.
 */
private AnnotatedTypeVariable getEffectTypeVar(TypeMirror typeMirror) {
    if (typeMirror == null) {
        return null;
    } else if (typeMirror.getKind() == TypeKind.WILDCARD) {
        return getEffectTypeVar(((WildcardType) typeMirror).getExtendsBound());
    } else if (typeMirror.getKind() == TypeKind.TYPEVAR) {
        TypeVariable typevar = ((TypeVariable) typeMirror);
        AnnotatedTypeMirror atm = analysis.getTypeFactory().getAnnotatedType(typevar.asElement());
        return (AnnotatedTypeVariable) atm;
    } else {
        return null;
    }
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(javax.lang.model.type.WildcardType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 18 with WildcardType

use of javax.lang.model.type.WildcardType in project checker-framework by typetools.

the class CFTreeBuilder method createAnnotatedType.

private Tree createAnnotatedType(AnnotatedTypeMirror annotatedType) {
    // Implementation based on com.sun.tools.javac.tree.TreeMaker.Type
    // Convert the annotations from a set of AnnotationMirrors
    // to a list of AnnotationTrees.
    Set<AnnotationMirror> annotations = annotatedType.getAnnotations();
    List<JCTree.JCAnnotation> annotationTrees = List.nil();
    for (AnnotationMirror am : annotations) {
        // TODO: what TypeAnnotationPosition should be used?
        Attribute.TypeCompound typeCompound = TypeAnnotationUtils.createTypeCompoundFromAnnotationMirror(am, TypeAnnotationUtils.unknownTAPosition(), env);
        JCTree.JCAnnotation annotationTree = maker.Annotation(typeCompound);
        JCTree.JCAnnotation typeAnnotationTree = maker.TypeAnnotation(annotationTree.getAnnotationType(), annotationTree.getArguments());
        typeAnnotationTree.attribute = typeCompound;
        annotationTrees = annotationTrees.append(typeAnnotationTree);
    }
    // Convert the underlying type from a TypeMirror to an
    // ExpressionTree and combine with the AnnotationTrees
    // to form a ClassTree of kind ANNOTATION_TYPE.
    Tree underlyingTypeTree;
    switch(annotatedType.getKind()) {
        case BYTE:
            underlyingTypeTree = maker.TypeIdent(TypeTag.BYTE);
            break;
        case CHAR:
            underlyingTypeTree = maker.TypeIdent(TypeTag.BYTE);
            break;
        case SHORT:
            underlyingTypeTree = maker.TypeIdent(TypeTag.SHORT);
            break;
        case INT:
            underlyingTypeTree = maker.TypeIdent(TypeTag.INT);
            break;
        case LONG:
            underlyingTypeTree = maker.TypeIdent(TypeTag.LONG);
            break;
        case FLOAT:
            underlyingTypeTree = maker.TypeIdent(TypeTag.FLOAT);
            break;
        case DOUBLE:
            underlyingTypeTree = maker.TypeIdent(TypeTag.DOUBLE);
            break;
        case BOOLEAN:
            underlyingTypeTree = maker.TypeIdent(TypeTag.BOOLEAN);
            break;
        case VOID:
            underlyingTypeTree = maker.TypeIdent(TypeTag.VOID);
            break;
        case TYPEVAR:
            {
                // No recursive annotations.
                AnnotatedTypeMirror.AnnotatedTypeVariable variable = (AnnotatedTypeMirror.AnnotatedTypeVariable) annotatedType;
                TypeVariable underlyingTypeVar = variable.getUnderlyingType();
                underlyingTypeTree = maker.Ident((Symbol.TypeSymbol) (underlyingTypeVar).asElement());
                break;
            }
        case WILDCARD:
            {
                AnnotatedTypeMirror.AnnotatedWildcardType wildcard = (AnnotatedTypeMirror.AnnotatedWildcardType) annotatedType;
                WildcardType wildcardType = wildcard.getUnderlyingType();
                if (wildcardType.getExtendsBound() != null) {
                    Tree annotatedExtendsBound = createAnnotatedType(wildcard.getExtendsBound());
                    underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), (JCTree) annotatedExtendsBound);
                } else if (wildcardType.getSuperBound() != null) {
                    Tree annotatedSuperBound = createAnnotatedType(wildcard.getSuperBound());
                    underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), (JCTree) annotatedSuperBound);
                } else {
                    underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
                }
                break;
            }
        case DECLARED:
            {
                underlyingTypeTree = maker.Type((Type) annotatedType.getUnderlyingType());
                if (underlyingTypeTree instanceof JCTree.JCTypeApply) {
                    // Replace the type parameters with annotated versions.
                    AnnotatedTypeMirror.AnnotatedDeclaredType annotatedDeclaredType = (AnnotatedTypeMirror.AnnotatedDeclaredType) annotatedType;
                    List<JCTree.JCExpression> typeArgTrees = List.nil();
                    for (AnnotatedTypeMirror arg : annotatedDeclaredType.getTypeArguments()) {
                        typeArgTrees = typeArgTrees.append((JCTree.JCExpression) createAnnotatedType(arg));
                    }
                    JCTree.JCExpression clazz = (JCTree.JCExpression) ((JCTree.JCTypeApply) underlyingTypeTree).getType();
                    underlyingTypeTree = maker.TypeApply(clazz, typeArgTrees);
                }
                break;
            }
        case ARRAY:
            {
                AnnotatedTypeMirror.AnnotatedArrayType annotatedArrayType = (AnnotatedTypeMirror.AnnotatedArrayType) annotatedType;
                Tree annotatedComponentTree = createAnnotatedType(annotatedArrayType.getComponentType());
                underlyingTypeTree = maker.TypeArray((JCTree.JCExpression) annotatedComponentTree);
                break;
            }
        case ERROR:
            underlyingTypeTree = maker.TypeIdent(TypeTag.ERROR);
            break;
        default:
            assert false : "unexpected type: " + annotatedType;
            underlyingTypeTree = null;
            break;
    }
    ((JCTree) underlyingTypeTree).setType((Type) annotatedType.getUnderlyingType());
    if (annotationTrees.isEmpty()) {
        return underlyingTypeTree;
    }
    JCTree.JCAnnotatedType annotatedTypeTree = maker.AnnotatedType(annotationTrees, (JCTree.JCExpression) underlyingTypeTree);
    annotatedTypeTree.setType((Type) annotatedType.getUnderlyingType());
    return annotatedTypeTree;
}
Also used : Attribute(com.sun.tools.javac.code.Attribute) Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror) WildcardType(javax.lang.model.type.WildcardType) TypeVariable(javax.lang.model.type.TypeVariable) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.sun.source.tree.Tree) List(com.sun.tools.javac.util.List)

Example 19 with WildcardType

use of javax.lang.model.type.WildcardType in project checker-framework by typetools.

the class AnnotatedTypeFactory method getUninferredWildcardType.

/**
 * Returns a wildcard type to be used as a type argument when the correct type could not be
 * inferred. The wildcard will be marked as an uninferred wildcard so that {@link
 * AnnotatedWildcardType#isUninferredTypeArgument()} returns true.
 *
 * <p>This method should only be used by type argument inference.
 * org.checkerframework.framework.util.AnnotatedTypes.inferTypeArguments(ProcessingEnvironment,
 * AnnotatedTypeFactory, ExpressionTree, ExecutableElement)
 *
 * @param typeVar TypeVariable which could not be inferred
 * @return a wildcard that is marked as an uninferred type argument
 */
public AnnotatedWildcardType getUninferredWildcardType(AnnotatedTypeVariable typeVar) {
    final boolean intersectionType;
    final TypeMirror boundType;
    if (typeVar.getUpperBound().getKind() == TypeKind.INTERSECTION) {
        boundType = typeVar.getUpperBound().directSuperTypes().get(0).getUnderlyingType();
        intersectionType = true;
    } else {
        boundType = typeVar.getUnderlyingType().getUpperBound();
        intersectionType = false;
    }
    WildcardType wc = types.getWildcardType(boundType, null);
    AnnotatedWildcardType wctype = (AnnotatedWildcardType) AnnotatedTypeMirror.createType(wc, this, false);
    wctype.setTypeVariable(typeVar.getUnderlyingType());
    if (!intersectionType) {
        wctype.setExtendsBound(typeVar.getUpperBound().deepCopy());
    } else {
        wctype.getExtendsBound().addAnnotations(typeVar.getUpperBound().getAnnotations());
    }
    wctype.setSuperBound(typeVar.getLowerBound().deepCopy());
    wctype.addAnnotations(typeVar.getAnnotations());
    addDefaultAnnotations(wctype);
    wctype.setUninferredTypeArgument();
    return wctype;
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)

Example 20 with WildcardType

use of javax.lang.model.type.WildcardType in project mapstruct by mapstruct.

the class Type method isWildCardSuperBound.

public boolean isWildCardSuperBound() {
    boolean result = false;
    if (typeMirror.getKind() == TypeKind.WILDCARD) {
        WildcardType wildcardType = (WildcardType) typeMirror;
        result = wildcardType.getSuperBound() != null;
    }
    return result;
}
Also used : WildcardType(javax.lang.model.type.WildcardType)

Aggregations

WildcardType (javax.lang.model.type.WildcardType)28 TypeMirror (javax.lang.model.type.TypeMirror)12 Test (org.junit.Test)9 DeclaredType (javax.lang.model.type.DeclaredType)8 Elements (javax.lang.model.util.Elements)7 TypeVariable (javax.lang.model.type.TypeVariable)6 ArrayType (javax.lang.model.type.ArrayType)5 Types (javax.lang.model.util.Types)5 Type (com.sun.tools.javac.code.Type)3 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)3 TypeElement (javax.lang.model.element.TypeElement)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Types (com.sun.tools.javac.code.Types)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 PrimitiveType (javax.lang.model.type.PrimitiveType)2 TypeKind (javax.lang.model.type.TypeKind)2 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)2 AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)2 MoreElements (com.google.auto.common.MoreElements)1