Search in sources :

Example 21 with WildcardType

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

the class TypeFactory method getTypeBound.

/**
 * Establishes the type bound:
 * <ol>
 * <li>{@code <? extends Number>}, returns Number</li>
 * <li>{@code <? super Number>}, returns Number</li>
 * <li>{@code <?>}, returns Object</li>
 * <li>{@code <T extends Number>, returns Number}</li>
 * </ol>
 *
 * @param typeMirror the type to return the bound for
 * @return the bound for this parameter
 */
public TypeMirror getTypeBound(TypeMirror typeMirror) {
    if (typeMirror.getKind() == TypeKind.WILDCARD) {
        WildcardType wildCardType = (WildcardType) typeMirror;
        if (wildCardType.getExtendsBound() != null) {
            return wildCardType.getExtendsBound();
        }
        if (wildCardType.getSuperBound() != null) {
            return wildCardType.getSuperBound();
        }
        String wildCardName = wildCardType.toString();
        if ("?".equals(wildCardName)) {
            return elementUtils.getTypeElement(Object.class.getCanonicalName()).asType();
        }
    } else if (typeMirror.getKind() == TypeKind.TYPEVAR) {
        TypeVariable typeVariableType = (TypeVariable) typeMirror;
        if (typeVariableType.getUpperBound() != null) {
            return typeVariableType.getUpperBound();
        }
    // Lowerbounds intentionally left out: Type variables otherwise have a lower bound of NullType.
    }
    return typeMirror;
}
Also used : WildcardType(javax.lang.model.type.WildcardType) TypeVariable(javax.lang.model.type.TypeVariable)

Example 22 with WildcardType

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

the class Utils method isGenericNotSpecialized.

/**
 * Returns true is the type is {@link java.lang.reflect.ParameterizedType} and has {@link
 * javax.lang.model.type.TypeVariable}.
 */
public boolean isGenericNotSpecialized(TypeMirror type) {
    // logger.n("type: %s", type);
    switch(type.getKind()) {
        case TYPEVAR:
            return true;
        case DECLARED:
            for (TypeMirror i : ((DeclaredType) type).getTypeArguments()) {
                if (isGenericNotSpecialized(i)) {
                    return true;
                }
            }
            break;
        case WILDCARD:
            WildcardType wildcardType = (WildcardType) type;
            TypeMirror extendsBound = wildcardType.getExtendsBound();
            if (extendsBound != null && isGenericNotSpecialized(extendsBound)) {
                return true;
            }
            TypeMirror superBound = wildcardType.getSuperBound();
            if (superBound != null && isGenericNotSpecialized(superBound)) {
                return true;
            }
            break;
        default:
            break;
    }
    return false;
}
Also used : WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 23 with WildcardType

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

the class RestAnnotationHelper method retrieveDecoratedResponseClass.

/**
 * Recursive method used to find if one of the grand-parent of the
 * <code>enclosingJClass</code> is {@link java.util.Map Map}, {@link Set} or
 * {@link java.util.Collection Collection}.
 */
private AbstractJClass retrieveDecoratedResponseClass(DeclaredType declaredType, TypeElement typeElement, RestHolder holder) {
    String classTypeBaseName = typeElement.toString();
    // Looking for basic java.util interfaces to set a default
    // implementation
    String decoratedClassName = null;
    if (typeElement.getKind() == ElementKind.INTERFACE) {
        if (classTypeBaseName.equals(CanonicalNameConstants.MAP)) {
            decoratedClassName = LinkedHashMap.class.getCanonicalName();
        } else if (classTypeBaseName.equals(CanonicalNameConstants.SET)) {
            decoratedClassName = TreeSet.class.getCanonicalName();
        } else if (classTypeBaseName.equals(CanonicalNameConstants.LIST)) {
            decoratedClassName = ArrayList.class.getCanonicalName();
        } else if (classTypeBaseName.equals(CanonicalNameConstants.COLLECTION)) {
            decoratedClassName = ArrayList.class.getCanonicalName();
        }
    } else {
        decoratedClassName = typeElement.getQualifiedName().toString();
    }
    if (decoratedClassName != null) {
        // Configure the super class of the final decorated class
        String decoratedClassNameSuffix = "";
        AbstractJClass decoratedSuperClass = getEnvironment().getJClass(decoratedClassName);
        for (TypeMirror typeArgument : declaredType.getTypeArguments()) {
            TypeMirror actualTypeArgument = typeArgument;
            if (typeArgument instanceof WildcardType) {
                WildcardType wildcardType = (WildcardType) typeArgument;
                if (wildcardType.getExtendsBound() != null) {
                    actualTypeArgument = wildcardType.getExtendsBound();
                } else if (wildcardType.getSuperBound() != null) {
                    actualTypeArgument = wildcardType.getSuperBound();
                }
            }
            AbstractJClass narrowJClass = codeModelHelper.typeMirrorToJClass(actualTypeArgument);
            decoratedSuperClass = decoratedSuperClass.narrow(narrowJClass);
            decoratedClassNameSuffix += plainName(narrowJClass);
        }
        String decoratedFinalClassName = classTypeBaseName + "_" + decoratedClassNameSuffix;
        decoratedFinalClassName = decoratedFinalClassName.replaceAll("\\[\\]", "s");
        String packageName = holder.getGeneratedClass()._package().name();
        decoratedFinalClassName = packageName + "." + decoratedFinalClassName;
        JDefinedClass decoratedJClass = getEnvironment().getDefinedClass(decoratedFinalClassName);
        decoratedJClass._extends(decoratedSuperClass);
        return decoratedJClass;
    }
    // Try to find the superclass and make a recursive call to the this
    // method
    TypeMirror enclosingSuperJClass = typeElement.getSuperclass();
    if (enclosingSuperJClass != null && enclosingSuperJClass.getKind() == TypeKind.DECLARED) {
        DeclaredType declaredEnclosingSuperJClass = (DeclaredType) enclosingSuperJClass;
        return retrieveDecoratedResponseClass(declaredType, (TypeElement) declaredEnclosingSuperJClass.asElement(), holder);
    }
    // Falling back to the current enclosingJClass if Class can't be found
    return null;
}
Also used : WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) JDefinedClass(com.helger.jcodemodel.JDefinedClass) ArrayList(java.util.ArrayList) AbstractJClass(com.helger.jcodemodel.AbstractJClass) LinkedHashMap(java.util.LinkedHashMap) DeclaredType(javax.lang.model.type.DeclaredType)

Example 24 with WildcardType

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

the class AbstractTypesTest method wildcardMirrorNoBounds.

@Test
public void wildcardMirrorNoBounds() throws Exception {
    WildcardType wildcard = getTypes().getWildcardType(null, null);
    TypeName type = TypeName.get(wildcard);
    assertThat(type.toString()).isEqualTo("?");
}
Also used : WildcardType(javax.lang.model.type.WildcardType) Test(org.junit.Test)

Example 25 with WildcardType

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

the class TypeStringProvider method caseType.

void caseType(TypeMirror type) {
    if (ended) {
        // to prevent additional recursive effects when using workaround
        return;
    }
    switch(type.getKind()) {
        case ERROR:
            unresolvedTypeHasOccured = true;
        // $FALL-THROUGH$
        case DECLARED:
            DeclaredType declaredType = (DeclaredType) type;
            appendResolved(declaredType);
            appendTypeArguments(type, declaredType);
            break;
        case ARRAY:
            TypeMirror componentType = ((ArrayType) type).getComponentType();
            int mark = buffer.length();
            caseType(componentType);
            cutTypeArgument(type, mark);
            buffer.append('[').append(']');
            break;
        case WILDCARD:
            WildcardType wildcard = (WildcardType) type;
            @Nullable TypeMirror extendsBound = wildcard.getExtendsBound();
            @Nullable TypeMirror superBound = wildcard.getSuperBound();
            if (extendsBound != null) {
                buffer.append("? extends ");
                caseType(extendsBound);
            } else if (superBound != null) {
                buffer.append("? super ");
                caseType(superBound);
            } else {
                buffer.append('?');
            }
            break;
        case TYPEVAR:
            if (allowedTypevars.length != 0) {
                TypeVariable typeVariable = (TypeVariable) type;
                String var = typeVariable.toString();
                int indexOfVar = Arrays.asList(allowedTypevars).indexOf(var);
                if (indexOfVar >= 0) {
                    if (typevarArguments != null) {
                        buffer.append(typevarArguments[indexOfVar]);
                    } else {
                        hasTypeVariables = true;
                        buffer.append(var);
                    }
                    break;
                }
            // If we don't have such parameter we consider this is the quirk
            // that was witnessed in Eclipse, we let the code below deal with it.
            }
            // ended flag
            if (tryToUseSourceAsAWorkaround()) {
                ended = true;
                break;
            }
            reporter.withElement(element).error("It is a compiler/annotation processing bug to receive type variable '%s' here." + " To avoid it — do not use not yet generated types in %s attribute", type, element.getSimpleName());
            // just append as toString whatever we have
            buffer.append(type);
            break;
        case BOOLEAN:
        case CHAR:
        case INT:
        case DOUBLE:
        case FLOAT:
        case SHORT:
        case LONG:
        case BYTE:
            String typeName = Ascii.toLowerCase(type.getKind().name());
            buffer.append(typeName);
            /* Just skip type annotations with primitives (for now?) too many problems/breakages
      List<? extends AnnotationMirror> annotations = null;
      if (processNestedTypeUseAnnotations
          && startType != type
          && !(annotations = AnnotationMirrors.from(type)).isEmpty()) {
        buffer.append(typeAnnotationsToBuffer(annotations, true)).append(typeName);
      } else {
        buffer.append(typeName);
      }*/
            break;
        default:
            buffer.append(type);
    }
    // workaround for Javac problem
    if (unresolvedTypeHasOccured && buffer.toString().contains("<any>")) {
        if (tryToUseSourceAsAWorkaround()) {
            ended = true;
        }
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) Nullable(javax.annotation.Nullable) DeclaredType(javax.lang.model.type.DeclaredType)

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