Search in sources :

Example 81 with TypeMirror

use of javax.lang.model.type.TypeMirror in project qpid-broker-j by apache.

the class ConfiguredObjectRegistrationGenerator method getCategory.

private String getCategory(final TypeElement e) {
    Elements elementUtils = processingEnv.getElementUtils();
    TypeElement annotationElement = elementUtils.getTypeElement(MANAGED_OBJECT_CANONICAL_NAME);
    String category = null;
    List<? extends AnnotationMirror> annotationMirrors = e.getAnnotationMirrors();
    if (annotationMirrors != null) {
        for (AnnotationMirror a : annotationMirrors) {
            if (a.getAnnotationType().asElement().equals(annotationElement)) {
                category = e.getSimpleName().toString().toLowerCase();
                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : a.getElementValues().entrySet()) {
                    if (entry.getKey().getSimpleName().toString().equals("category")) {
                        if (!Boolean.TRUE.equals(entry.getValue().getValue())) {
                            category = null;
                        }
                        break;
                    }
                }
                break;
            }
        }
    }
    if (category == null) {
        for (TypeMirror interfaceMirror : e.getInterfaces()) {
            category = getCategory((TypeElement) processingEnv.getTypeUtils().asElement(interfaceMirror));
            if (category != null) {
                break;
            }
        }
    }
    if (category == null && e.getSuperclass() != null) {
        TypeElement parent = (TypeElement) processingEnv.getTypeUtils().asElement(e.getSuperclass());
        if (parent != null) {
            category = getCategory(parent);
        }
    }
    return category;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) HashMap(java.util.HashMap) Map(java.util.Map)

Example 82 with TypeMirror

use of javax.lang.model.type.TypeMirror in project qpid-broker-j by apache.

the class AttributeAnnotationValidator method isValidType.

static boolean isValidType(ProcessingEnvironment processingEnv, final TypeMirror type, final boolean allowAbstractManagedTypes) {
    Types typeUtils = processingEnv.getTypeUtils();
    Elements elementUtils = processingEnv.getElementUtils();
    Element typeElement = typeUtils.asElement(type);
    if (VALID_PRIMITIVE_TYPES.contains(type.getKind())) {
        return true;
    }
    for (TypeKind primitive : VALID_PRIMITIVE_TYPES) {
        if (typeUtils.isSameType(type, typeUtils.boxedClass(typeUtils.getPrimitiveType(primitive)).asType())) {
            return true;
        }
    }
    if (typeElement != null && typeElement.getKind() == ElementKind.ENUM) {
        return true;
    }
    String className = "org.apache.qpid.server.model.ConfiguredObject";
    TypeMirror configuredObjectType = getErasure(processingEnv, className);
    if (typeUtils.isAssignable(typeUtils.erasure(type), configuredObjectType)) {
        return true;
    }
    final TypeElement managedAttributeTypeValueElement = elementUtils.getTypeElement(ManagedAttributeValueTypeValidator.MANAGED_ATTRIBUTE_VALUE_TYPE_CLASS_NAME);
    if (typeElement != null) {
        for (AnnotationMirror annotation : typeElement.getAnnotationMirrors()) {
            if (annotation.getAnnotationType().asElement().equals(managedAttributeTypeValueElement)) {
                if (allowAbstractManagedTypes) {
                    return true;
                } else {
                    final Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues = elementUtils.getElementValuesWithDefaults(annotation);
                    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> element : annotationValues.entrySet()) {
                        if ("isAbstract".contentEquals(element.getKey().getSimpleName())) {
                            return element.getValue().getValue().equals(Boolean.FALSE);
                        }
                    }
                    return false;
                }
            }
        }
    }
    if (typeUtils.isSameType(type, elementUtils.getTypeElement("java.lang.Object").asType())) {
        return true;
    }
    if (typeUtils.isSameType(type, elementUtils.getTypeElement("java.lang.String").asType())) {
        return true;
    }
    if (typeUtils.isSameType(type, elementUtils.getTypeElement("java.util.UUID").asType())) {
        return true;
    }
    if (typeUtils.isSameType(type, elementUtils.getTypeElement("java.util.Date").asType())) {
        return true;
    }
    if (typeUtils.isSameType(type, elementUtils.getTypeElement("java.net.URI").asType())) {
        return true;
    }
    if (typeUtils.isSameType(type, elementUtils.getTypeElement("java.security.cert.Certificate").asType())) {
        return true;
    }
    if (typeUtils.isSameType(type, elementUtils.getTypeElement("java.security.Principal").asType())) {
        return true;
    }
    TypeMirror erasedType = typeUtils.erasure(type);
    if (typeUtils.isSameType(erasedType, getErasure(processingEnv, "java.util.List")) || typeUtils.isSameType(erasedType, getErasure(processingEnv, "java.util.Set")) || typeUtils.isSameType(erasedType, getErasure(processingEnv, "java.util.Collection"))) {
        for (TypeMirror paramType : ((DeclaredType) type).getTypeArguments()) {
            if (!isValidType(processingEnv, paramType, allowAbstractManagedTypes)) {
                return false;
            }
        }
        return true;
    }
    if (typeUtils.isSameType(erasedType, getErasure(processingEnv, "java.util.Map"))) {
        List<? extends TypeMirror> args = ((DeclaredType) type).getTypeArguments();
        if (args.size() != 2) {
            throw new IllegalArgumentException("Map types " + type + " must have exactly two type arguments");
        }
        return isValidType(processingEnv, args.get(0), false) && (isValidType(processingEnv, args.get(1), false) || typeUtils.isSameType(args.get(1), getErasure(processingEnv, "java.lang.Object")));
    }
    return false;
}
Also used : Types(javax.lang.model.util.Types) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) TypeKind(javax.lang.model.type.TypeKind) Elements(javax.lang.model.util.Elements) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType)

Example 83 with TypeMirror

use of javax.lang.model.type.TypeMirror in project qpid-broker-j by apache.

the class AttributeAnnotationValidator method isNamed.

static boolean isNamed(ProcessingEnvironment processingEnv, final TypeMirror type) {
    Types typeUtils = processingEnv.getTypeUtils();
    String className = "org.apache.qpid.server.model.Named";
    TypeMirror namedType = getErasure(processingEnv, className);
    return typeUtils.isAssignable(typeUtils.erasure(type), namedType);
}
Also used : Types(javax.lang.model.util.Types) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) TypeMirror(javax.lang.model.type.TypeMirror)

Example 84 with TypeMirror

use of javax.lang.model.type.TypeMirror in project qpid-broker-j by apache.

the class AttributeAnnotationValidator method isStringOrCollectionOfStrings.

private boolean isStringOrCollectionOfStrings(TypeMirror type) {
    TypeMirror stringType = elementUtils.getTypeElement("java.lang.String").asType();
    TypeMirror collectionType = elementUtils.getTypeElement("java.util.Collection").asType();
    TypeElement typeAsElement = (TypeElement) typeUtils.asElement(type);
    return typeUtils.isAssignable(type, stringType) || (typeAsElement != null && typeAsElement.getTypeParameters().size() == 1 && "java.lang.String".equals(getFullyQualifiedName(getErasedParameterType((DeclaredType) type, 0))) && typeUtils.isAssignable(typeUtils.erasure(typeAsElement.asType()), collectionType));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement)

Example 85 with TypeMirror

use of javax.lang.model.type.TypeMirror in project qpid-broker-j by apache.

the class AttributeAnnotationValidator method checkInterfaceExtendsConfiguredObject.

public void checkInterfaceExtendsConfiguredObject(final TypeElement annotationElement, final Element e) {
    TypeMirror configuredObjectType = getErasure("org.apache.qpid.server.model.ConfiguredObject");
    TypeElement parent = (TypeElement) e.getEnclosingElement();
    if (!typeUtils.isAssignable(typeUtils.erasure(parent.asType()), configuredObjectType)) {
        messager.printMessage(Diagnostic.Kind.ERROR, "@" + annotationElement.getSimpleName() + " can only be applied to methods within an interface which extends " + configuredObjectType.toString() + " which does not apply to " + parent.asType().toString(), e);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)1086 TypeElement (javax.lang.model.element.TypeElement)419 ExecutableElement (javax.lang.model.element.ExecutableElement)259 VariableElement (javax.lang.model.element.VariableElement)237 Element (javax.lang.model.element.Element)196 DeclaredType (javax.lang.model.type.DeclaredType)177 ArrayList (java.util.ArrayList)154 Test (org.junit.Test)152 AnnotationMirror (javax.lang.model.element.AnnotationMirror)75 Types (javax.lang.model.util.Types)70 List (java.util.List)66 Elements (javax.lang.model.util.Elements)60 HashSet (java.util.HashSet)53 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)50 Map (java.util.Map)49 ExecutableType (javax.lang.model.type.ExecutableType)45 MethodSpec (com.squareup.javapoet.MethodSpec)44 PackageElement (javax.lang.model.element.PackageElement)41 ArrayType (javax.lang.model.type.ArrayType)41 Expression (com.google.devtools.j2objc.ast.Expression)39