Search in sources :

Example 11 with WildcardType

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

the class Type method isWildCardExtendsBound.

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

Example 12 with WildcardType

use of javax.lang.model.type.WildcardType in project javapoet by square.

the class AbstractTypesTest method wildcardMirrorSuperType.

@Test
public void wildcardMirrorSuperType() throws Exception {
    Types types = getTypes();
    Elements elements = getElements();
    TypeMirror string = elements.getTypeElement(String.class.getName()).asType();
    WildcardType wildcard = types.getWildcardType(null, string);
    TypeName type = TypeName.get(wildcard);
    assertThat(type.toString()).isEqualTo("? super java.lang.String");
}
Also used : Types(javax.lang.model.util.Types) WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) Elements(javax.lang.model.util.Elements) Test(org.junit.Test)

Example 13 with WildcardType

use of javax.lang.model.type.WildcardType in project javapoet by square.

the class AbstractTypesTest method wildcardMirrorExtendsType.

@Test
public void wildcardMirrorExtendsType() throws Exception {
    Types types = getTypes();
    Elements elements = getElements();
    TypeMirror charSequence = elements.getTypeElement(CharSequence.class.getName()).asType();
    WildcardType wildcard = types.getWildcardType(charSequence, null);
    TypeName type = TypeName.get(wildcard);
    assertThat(type.toString()).isEqualTo("? extends java.lang.CharSequence");
}
Also used : Types(javax.lang.model.util.Types) WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) Elements(javax.lang.model.util.Elements) Test(org.junit.Test)

Example 14 with WildcardType

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

the class NameUtil method buildTypeSignature.

private void buildTypeSignature(TypeMirror type, StringBuilder sb) {
    switch(type.getKind()) {
        case BOOLEAN:
        case BYTE:
        case CHAR:
        case DOUBLE:
        case FLOAT:
        case INT:
        case LONG:
        case SHORT:
        case VOID:
            sb.append(TypeUtil.getBinaryName(type));
            break;
        case ARRAY:
            sb.append('[');
            buildTypeSignature(((ArrayType) type).getComponentType(), sb);
            break;
        case DECLARED:
            buildDeclaredType((DeclaredType) type, sb);
            sb.append(';');
            break;
        case TYPEVAR:
            buildTypeVariable((TypeVariable) type, sb);
            break;
        case INTERSECTION:
            sb.append("&{");
            for (TypeMirror bound : ((IntersectionType) type).getBounds()) {
                buildTypeSignature(bound, sb);
            }
            sb.append('}');
            break;
        case WILDCARD:
            TypeMirror upperBound = ((WildcardType) type).getExtendsBound();
            TypeMirror lowerBound = ((WildcardType) type).getSuperBound();
            if (upperBound != null) {
                sb.append('+');
                buildTypeSignature(upperBound, sb);
            } else if (lowerBound != null) {
                sb.append('-');
                buildTypeSignature(lowerBound, sb);
            } else {
                sb.append('*');
            }
            break;
        default:
            throw new AssertionError("Unexpected type kind: " + type.getKind());
    }
}
Also used : WildcardType(javax.lang.model.type.WildcardType) IntersectionType(javax.lang.model.type.IntersectionType) TypeMirror(javax.lang.model.type.TypeMirror)

Example 15 with WildcardType

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

the class SignatureGenerator method genTypeSignature.

// TODO(kstanger): Figure out if this can replace TypeUtil.getSignatureName().
private void genTypeSignature(TypeMirror type, StringBuilder sb) {
    switch(type.getKind()) {
        case BOOLEAN:
        case BYTE:
        case CHAR:
        case DOUBLE:
        case FLOAT:
        case INT:
        case LONG:
        case SHORT:
        case VOID:
            sb.append(TypeUtil.getBinaryName(type));
            break;
        case ARRAY:
            // ArrayTypeSignature ::= "[" TypSignature.
            sb.append('[');
            genTypeSignature(((ArrayType) type).getComponentType(), sb);
            break;
        case DECLARED:
            String typeName = elementUtil.getBinaryName(TypeUtil.asTypeElement(type));
            if (!TypeUtil.isStubType(typeName)) {
                // ClassTypeSignature ::= "L" {Ident "/"} Ident
                // OptTypeArguments {"." Ident OptTypeArguments} ";".
                sb.append('L');
                sb.append(typeName.replace('.', '/'));
                genOptTypeArguments(((DeclaredType) type).getTypeArguments(), sb);
                sb.append(';');
            }
            break;
        case TYPEVAR:
            // TypeVariableSignature ::= "T" Ident ";".
            sb.append('T');
            sb.append(ElementUtil.getName(((TypeVariable) type).asElement()));
            sb.append(';');
            break;
        case WILDCARD:
            // TypeArgument ::= (["+" | "-"] FieldTypeSignature) | "*".
            TypeMirror upperBound = ((WildcardType) type).getExtendsBound();
            TypeMirror lowerBound = ((WildcardType) type).getSuperBound();
            if (upperBound != null) {
                sb.append('+');
                genTypeSignature(upperBound, sb);
            } else if (lowerBound != null) {
                sb.append('-');
                genTypeSignature(lowerBound, sb);
            } else {
                sb.append('*');
            }
            break;
        default:
            throw new AssertionError("Unexpected type kind: " + type.getKind());
    }
}
Also used : WildcardType(javax.lang.model.type.WildcardType) TypeVariable(javax.lang.model.type.TypeVariable) TypeMirror(javax.lang.model.type.TypeMirror)

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