Search in sources :

Example 86 with TypeMirror

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

the class AttributeAnnotationValidator method checkMethodReturnTypeIsNumberOrDate.

private void checkMethodReturnTypeIsNumberOrDate(final TypeElement annotationElement, final ExecutableElement methodElement) {
    TypeMirror numberType = elementUtils.getTypeElement("java.lang.Number").asType();
    TypeMirror dateType = elementUtils.getTypeElement("java.util.Date").asType();
    if (!typeUtils.isAssignable(methodElement.getReturnType(), numberType) && !typeUtils.isSameType(methodElement.getReturnType(), dateType)) {
        messager.printMessage(Diagnostic.Kind.ERROR, "@" + annotationElement.getSimpleName() + " return type does not extend java.lang.Number" + " and is not java.util.Date : " + methodElement.getReturnType().toString(), methodElement);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror)

Example 87 with TypeMirror

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

the class OperationAnnotationValidator method checkInterfaceExtendsConfiguredObject.

public void checkInterfaceExtendsConfiguredObject(final TypeElement annotationElement, final Element e) {
    Types typeUtils = processingEnv.getTypeUtils();
    TypeMirror configuredObjectType = getErasure("org.apache.qpid.server.model.ConfiguredObject");
    TypeElement parent = (TypeElement) e.getEnclosingElement();
    if (!typeUtils.isAssignable(typeUtils.erasure(parent.asType()), configuredObjectType)) {
        processingEnv.getMessager().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 : Types(javax.lang.model.util.Types) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement)

Example 88 with TypeMirror

use of javax.lang.model.type.TypeMirror in project react4j by react4j.

the class ProcessorUtil method enumerateMethods.

private static void enumerateMethods(@Nonnull final TypeElement scope, @Nonnull final Types typeUtils, @Nonnull final TypeElement element, @Nonnull final Map<String, ExecutableElement> methods) {
    final TypeMirror superclass = element.getSuperclass();
    if (TypeKind.NONE != superclass.getKind()) {
        enumerateMethods(scope, typeUtils, (TypeElement) ((DeclaredType) superclass).asElement(), methods);
    }
    for (final TypeMirror interfaceType : element.getInterfaces()) {
        final TypeElement interfaceElement = (TypeElement) ((DeclaredType) interfaceType).asElement();
        enumerateMethods(scope, typeUtils, interfaceElement, methods);
    }
    for (final Element member : element.getEnclosedElements()) {
        if (member.getKind() == ElementKind.METHOD) {
            final ExecutableType methodType = (ExecutableType) typeUtils.asMemberOf((DeclaredType) scope.asType(), member);
            methods.put(member.getSimpleName() + methodType.toString(), (ExecutableElement) member);
        }
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 89 with TypeMirror

use of javax.lang.model.type.TypeMirror in project butterknife by JakeWharton.

the class ButterKnifeProcessor method isSubtypeOfType.

static boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
    if (isTypeEqual(typeMirror, otherType)) {
        return true;
    }
    if (typeMirror.getKind() != TypeKind.DECLARED) {
        return false;
    }
    DeclaredType declaredType = (DeclaredType) typeMirror;
    List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
    if (typeArguments.size() > 0) {
        StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
        typeString.append('<');
        for (int i = 0; i < typeArguments.size(); i++) {
            if (i > 0) {
                typeString.append(',');
            }
            typeString.append('?');
        }
        typeString.append('>');
        if (typeString.toString().equals(otherType)) {
            return true;
        }
    }
    Element element = declaredType.asElement();
    if (!(element instanceof TypeElement)) {
        return false;
    }
    TypeElement typeElement = (TypeElement) element;
    TypeMirror superType = typeElement.getSuperclass();
    if (isSubtypeOfType(superType, otherType)) {
        return true;
    }
    for (TypeMirror interfaceType : typeElement.getInterfaces()) {
        if (isSubtypeOfType(interfaceType, otherType)) {
            return true;
        }
    }
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 90 with TypeMirror

use of javax.lang.model.type.TypeMirror in project butterknife by JakeWharton.

the class ButterKnifeProcessor method parseResourceColor.

private void parseResourceColor(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
    boolean hasError = false;
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    // Verify that the target type is int or ColorStateList.
    boolean isColorStateList = false;
    TypeMirror elementType = element.asType();
    if (COLOR_STATE_LIST_TYPE.equals(elementType.toString())) {
        isColorStateList = true;
    } else if (elementType.getKind() != TypeKind.INT) {
        error(element, "@%s field type must be 'int' or 'ColorStateList'. (%s.%s)", BindColor.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    // Verify common generated code restrictions.
    hasError |= isInaccessibleViaGeneratedCode(BindColor.class, "fields", element);
    hasError |= isBindingInWrongPackage(BindColor.class, element);
    if (hasError) {
        return;
    }
    // Assemble information on the field.
    String name = element.getSimpleName().toString();
    int id = element.getAnnotation(BindColor.class).value();
    QualifiedId qualifiedId = elementToQualifiedId(element, id);
    BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
    builder.addResource(new FieldResourceBinding(getId(qualifiedId), name, isColorStateList ? FieldResourceBinding.Type.COLOR_STATE_LIST : FieldResourceBinding.Type.COLOR));
    erasedTargetNames.add(enclosingElement);
}
Also used : BindColor(butterknife.BindColor) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) BindString(butterknife.BindString)

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