Search in sources :

Example 96 with DeclaredType

use of javax.lang.model.type.DeclaredType 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;
        default:
            buffer.append(type);
    }
}
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)

Example 97 with DeclaredType

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

the class ValueAttribute method introspectType.

@Override
protected void introspectType() {
    TypeMirror typeMirror = returnType;
    // Special case for primitive Optional, may become a pattern for specialized types
    if (typeKind.isOptionalSpecializedJdk()) {
        typeParameters = ImmutableList.of(optionalSpecializedType());
        // no delegation to introspect further
        return;
    }
    if (isContainerType()) {
        if (typeMirror.getKind() == TypeKind.DECLARED || typeMirror.getKind() == TypeKind.ERROR) {
            DeclaredType declaredType = (DeclaredType) typeMirror;
            List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
            if (!typeArguments.isEmpty()) {
                final TypeMirror typeArgument = typeArguments.get(0);
                if (isSetType() && protoclass().environment().hasOrdinalModule()) {
                    this.generateOrdinalValueSet = new TypeIntrospectionBase() {

                        @Override
                        protected TypeMirror internalTypeMirror() {
                            return typeArgument;
                        }

                        @Override
                        protected TypeHierarchyCollector collectTypeHierarchy(TypeMirror typeMirror) {
                            TypeHierarchyCollector collector = containingType.createTypeHierarchyCollector(reporter, element);
                            collector.collectFrom(typeMirror);
                            return collector;
                        }
                    }.isOrdinalValue();
                }
                if (isMapType()) {
                    TypeMirror typeSecondArgument = typeArguments.get(1);
                    if (typeSecondArgument.getKind() == TypeKind.DECLARED) {
                        TypeElement typeElement = (TypeElement) ((DeclaredType) typeSecondArgument).asElement();
                        this.containedSecondaryTypeElement = typeElement;
                    }
                }
                typeMirror = typeArgument;
            }
        }
    } else if (isArrayType()) {
        arrayComponent = ((ArrayType) typeMirror).getComponentType();
        typeMirror = arrayComponent;
    }
    if (typeMirror.getKind() == TypeKind.DECLARED) {
        TypeElement typeElement = (TypeElement) ((DeclaredType) typeMirror).asElement();
        this.containedTypeElement = typeElement;
    }
    introspectTypeMirror(typeMirror);
    introspectSupertypes();
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeHierarchyCollector(org.immutables.generator.TypeHierarchyCollector) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 98 with DeclaredType

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

the class TypeHierarchyCollector method collectHierarchyMirrors.

private void collectHierarchyMirrors(TypeMirror typeMirror, String stringRepresentation) {
    if (typeMirror.getKind() != TypeKind.DECLARED || typeMirror.toString().equals(Object.class.getName())) {
        return;
    }
    DeclaredType declaredType = toDeclaredType(typeMirror);
    TypeElement e = toTypeElement(declaredType);
    TypevarContext context = new TypevarContext(e, stringRepresentation);
    collectInterfacesMirrors(declaredType, context);
    TypeMirror superclassMirror = e.getSuperclass();
    if (superclassMirror.getKind() != TypeKind.NONE) {
        DeclaredType superclass = toDeclaredType(superclassMirror);
        String stringified = stringify(superclass, context);
        if (!stringified.equals(Object.class.getName())) {
            extendedClasses.add(toTypeElement(superclass));
            extendedClassNames.add(stringified);
            collectHierarchyMirrors(superclass, stringified);
        }
    }
    for (TypeMirror m : e.getInterfaces()) {
        collectUnresolvedInterface(m, context);
        collectInterfacesMirrors(m, context);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 99 with DeclaredType

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

the class TypeHierarchyCollector method collectInterfacesMirrors.

private void collectInterfacesMirrors(TypeMirror typeMirror, TypevarContext context) {
    if (typeMirror.getKind() != TypeKind.DECLARED) {
        return;
    }
    DeclaredType declaredType = toDeclaredType(typeMirror);
    TypeElement e = toTypeElement(declaredType);
    if (e.getKind().isInterface()) {
        implementedInterfaces.add(e);
        String stringified = stringify(declaredType, context);
        TypevarContext nestedContext = new TypevarContext(e, stringified);
        implementedInterfaceNames.add(stringified);
        for (TypeMirror m : e.getInterfaces()) {
            collectInterfacesMirrors(m, nestedContext);
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 100 with DeclaredType

use of javax.lang.model.type.DeclaredType in project hibernate-orm by hibernate.

the class ClassWriter method findMappedSuperClass.

private static String findMappedSuperClass(MetaEntity entity, Context context) {
    TypeMirror superClass = entity.getTypeElement().getSuperclass();
    //superclass of Object is of NoType which returns some other kind
    while (superClass.getKind() == TypeKind.DECLARED) {
        //F..king Ch...t Have those people used their horrible APIs even once?
        final Element superClassElement = ((DeclaredType) superClass).asElement();
        String superClassName = ((TypeElement) superClassElement).getQualifiedName().toString();
        if (extendsSuperMetaModel(superClassElement, entity.isMetaComplete(), context)) {
            return superClassName;
        }
        superClass = ((TypeElement) superClassElement).getSuperclass();
    }
    return null;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

DeclaredType (javax.lang.model.type.DeclaredType)136 TypeElement (javax.lang.model.element.TypeElement)81 TypeMirror (javax.lang.model.type.TypeMirror)74 ExecutableElement (javax.lang.model.element.ExecutableElement)38 Element (javax.lang.model.element.Element)28 VariableElement (javax.lang.model.element.VariableElement)26 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)14 ArrayType (javax.lang.model.type.ArrayType)14 List (java.util.List)10 Map (java.util.Map)9 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 HashSet (java.util.HashSet)8 AnnotationValue (javax.lang.model.element.AnnotationValue)8 HashMap (java.util.HashMap)6 Types (javax.lang.model.util.Types)6 Name (javax.lang.model.element.Name)5 PackageElement (javax.lang.model.element.PackageElement)5 TypeParameterElement (javax.lang.model.element.TypeParameterElement)5