Example usage for javax.lang.model.type DeclaredType asElement

List of usage examples for javax.lang.model.type DeclaredType asElement

Introduction

In this page you can find the example usage for javax.lang.model.type DeclaredType asElement.

Prototype

Element asElement();

Source Link

Document

Returns the element corresponding to this type.

Usage

From source file:com.googlecode.androidannotations.helper.ValidatorHelper.java

public void canBeSavedAsInstanceState(Element element, IsValid isValid) {
    String typeString = element.asType().toString();

    if (!isKnowInstanceStateType(typeString)) {

        if (element.asType() instanceof DeclaredType) {

            DeclaredType declaredType = (DeclaredType) element.asType();
            typeString = declaredType.asElement().toString();

        } else if (element.asType() instanceof ArrayType) {
            ArrayType arrayType = (ArrayType) element.asType();
            TypeMirror componentType = arrayType.getComponentType();

            if (componentType instanceof DeclaredType) {

                DeclaredType declaredType = (DeclaredType) componentType;
                typeString = declaredType.asElement().toString();

            } else {
                typeString = componentType.toString();
            }/*w ww  .j a  va2 s .  com*/

        } else {
            typeString = element.asType().toString();
        }

        TypeElement elementType = annotationHelper.typeElementFromQualifiedName(typeString);

        if (elementType == null) {
            elementType = getArrayEnclosingType(typeString);

            if (elementType == null) {
                annotationHelper.printAnnotationError(element,
                        "Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable");
                isValid.invalidate();
            }
        }

        if (elementType != null) {
            TypeElement parcelableType = annotationHelper.typeElementFromQualifiedName("android.os.Parcelable");
            TypeElement serializableType = annotationHelper
                    .typeElementFromQualifiedName("java.io.Serializable");
            if (!annotationHelper.isSubtype(elementType, parcelableType)
                    && !annotationHelper.isSubtype(elementType, serializableType)) {
                annotationHelper.printAnnotationError(element,
                        "Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable");
                isValid.invalidate();
            }
        }
    }
}

From source file:io.github.jeddict.jpa.spec.extend.MultiRelationAttribute.java

@Override
@Deprecated/*from   ww  w .ja  va  2  s .c  om*/
public void loadAttribute(EntityMappings entityMappings, Element element, VariableElement variableElement,
        ExecutableElement getterElement, AnnotationMirror relationAnnotationMirror) {
    super.loadAttribute(entityMappings, element, variableElement, getterElement, relationAnnotationMirror);

    this.mappedBy = (String) JavaSourceParserUtil.findAnnotationValue(relationAnnotationMirror, "mappedBy");
    this.orderBy = OrderBy.load(element, variableElement);
    this.orderColumn = OrderColumn.load(element, variableElement);
    this.collectionType = ((DeclaredType) variableElement.asType()).asElement().toString();
    Class collectionTypeClass = null;
    try {
        collectionTypeClass = Class.forName(this.collectionType);
    } catch (ClassNotFoundException ex) {
    }
    boolean mapKeyExist = collectionTypeClass != null && Map.class.isAssignableFrom(collectionTypeClass);

    DeclaredType declaredType = (DeclaredType) JavaSourceParserUtil
            .findAnnotationValue(relationAnnotationMirror, "targetEntity");
    if (declaredType == null) {
        if (variableElement.asType() instanceof ErrorType) { //variable => "<any>"
            throw new TypeNotPresentException(this.name + " type not found", null);
        }
        declaredType = (DeclaredType) ((DeclaredType) variableElement.asType()).getTypeArguments()
                .get(mapKeyExist ? 1 : 0);
    }
    String fqn = declaredType.asElement().asType().toString();
    this.targetEntityPackage = getPackageName(fqn);
    this.targetEntity = getSimpleClassName(fqn);

    if (mapKeyExist) {
        this.mapKeyConvert = Convert.load(element, mapKeyExist, true);
        this.mapKey = new MapKey().load(element, null);
        this.mapKeyType = this.mapKey != null ? MapKeyType.EXT : MapKeyType.NEW;

        DeclaredType keyDeclaredType = MapKeyClass.getDeclaredType(element);
        if (keyDeclaredType == null) {
            keyDeclaredType = (DeclaredType) ((DeclaredType) variableElement.asType()).getTypeArguments()
                    .get(0);
        }
        if (isEmbeddable(keyDeclaredType.asElement())) {
            loadEmbeddableClass(entityMappings, element, variableElement, keyDeclaredType);
            this.mapKeyAttributeType = getSimpleClassName(keyDeclaredType.toString());
        } else if (isEntity(keyDeclaredType.asElement())) {
            loadEntity(entityMappings, element, variableElement, keyDeclaredType);
            this.mapKeyAttributeType = getSimpleClassName(keyDeclaredType.toString());
        } else {
            this.mapKeyAttributeType = keyDeclaredType.toString();
        }

        this.mapKeyColumn = new Column().load(element,
                JavaSourceParserUtil.findAnnotation(element, MAP_KEY_COLUMN_FQN));
        this.mapKeyTemporal = TemporalType.load(element,
                JavaSourceParserUtil.findAnnotation(element, MAP_KEY_TEMPORAL_FQN));
        this.mapKeyEnumerated = EnumType.load(element,
                JavaSourceParserUtil.findAnnotation(element, MAP_KEY_ENUMERATED_FQN));

        AnnotationMirror joinColumnsAnnotationMirror = JavaSourceParserUtil.findAnnotation(element,
                MAP_KEY_JOIN_COLUMNS_FQN);
        if (joinColumnsAnnotationMirror != null) {
            List joinColumnsAnnot = (List) JavaSourceParserUtil.findAnnotationValue(joinColumnsAnnotationMirror,
                    "value");
            if (joinColumnsAnnot != null) {
                for (Object joinColumnObj : joinColumnsAnnot) {
                    this.getMapKeyJoinColumn()
                            .add(new JoinColumn().load(element, (AnnotationMirror) joinColumnObj));
                }
            }
        } else {
            AnnotationMirror joinColumnAnnotationMirror = JavaSourceParserUtil.findAnnotation(element,
                    MAP_KEY_JOIN_COLUMN_FQN);
            if (joinColumnAnnotationMirror != null) {
                this.getMapKeyJoinColumn().add(new JoinColumn().load(element, joinColumnAnnotationMirror));
            }
        }

        this.mapKeyForeignKey = ForeignKey.load(element, null);
        this.getMapKeyAttributeOverride().addAll(AttributeOverride.load(element));

    }
}

From source file:com.googlecode.androidannotations.helper.ValidatorHelper.java

public void validateConverters(Element element, IsValid valid) {
    TypeMirror httpMessageConverterType = annotationHelper.typeElementFromQualifiedName(HTTP_MESSAGE_CONVERTER)
            .asType();//from  w ww  . j  a  va2  s .c o m
    TypeMirror httpMessageConverterTypeErased = annotationHelper.getTypeUtils()
            .erasure(httpMessageConverterType);
    List<DeclaredType> converters = annotationHelper.extractAnnotationClassArrayParameter(element,
            annotationHelper.getTarget(), "converters");
    for (DeclaredType converterType : converters) {
        TypeMirror erasedConverterType = annotationHelper.getTypeUtils().erasure(converterType);
        if (annotationHelper.isSubtype(erasedConverterType, httpMessageConverterTypeErased)) {
            Element converterElement = converterType.asElement();
            if (converterElement.getKind().isClass()) {
                if (!annotationHelper.isAbstract(converterElement)) {
                    List<ExecutableElement> constructors = ElementFilter
                            .constructorsIn(converterElement.getEnclosedElements());
                    for (ExecutableElement constructor : constructors) {
                        if (annotationHelper.isPublic(constructor) && constructor.getParameters().isEmpty()) {
                            return;
                        }
                    }
                    valid.invalidate();
                    annotationHelper.printAnnotationError(element,
                            "The converter class must have a public no argument constructor");
                } else {
                    valid.invalidate();
                    annotationHelper.printAnnotationError(element, "The converter class must not be abstract");
                }
            } else {
                valid.invalidate();
                annotationHelper.printAnnotationError(element, "The converter class must be a class");
            }
        } else {
            valid.invalidate();
            annotationHelper.printAnnotationError(element,
                    "The converter class must be a subtype of " + HTTP_MESSAGE_CONVERTER);
        }
    }

}

From source file:org.androidannotations.helper.ValidatorHelper.java

public void canBePutInABundle(Element element, IsValid isValid) {
    String typeString = element.asType().toString();

    if (!isKnownBundleCompatibleType(typeString)) {

        if (element.asType() instanceof DeclaredType) {

            DeclaredType declaredType = (DeclaredType) element.asType();
            typeString = declaredType.asElement().toString();

        } else if (element.asType() instanceof ArrayType) {
            ArrayType arrayType = (ArrayType) element.asType();
            TypeMirror componentType = arrayType.getComponentType();

            if (componentType instanceof DeclaredType) {

                DeclaredType declaredType = (DeclaredType) componentType;
                typeString = declaredType.asElement().toString();

            } else {
                typeString = componentType.toString();
            }//from  w  w w .  ja va2  s . c om

        } else {
            typeString = element.asType().toString();
        }

        TypeElement elementType = annotationHelper.typeElementFromQualifiedName(typeString);

        if (elementType == null) {
            elementType = getArrayEnclosingType(typeString);

            if (elementType == null) {
                annotationHelper.printAnnotationError(element,
                        "Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable");
                isValid.invalidate();
            }
        }

        if (elementType != null) {
            TypeElement parcelableType = annotationHelper
                    .typeElementFromQualifiedName(CanonicalNameConstants.PARCELABLE);
            TypeElement serializableType = annotationHelper
                    .typeElementFromQualifiedName("java.io.Serializable");
            if (!annotationHelper.isSubtype(elementType, parcelableType)
                    && !annotationHelper.isSubtype(elementType, serializableType)) {
                annotationHelper.printAnnotationError(element,
                        "Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable");
                isValid.invalidate();
            }
        }
    }
}

From source file:org.androidannotations.helper.ValidatorHelper.java

public void validateConverters(Element element, IsValid valid) {
    TypeMirror httpMessageConverterType = annotationHelper.typeElementFromQualifiedName(HTTP_MESSAGE_CONVERTER)
            .asType();//from w w w  .ja va 2  s  .c  om
    TypeMirror httpMessageConverterTypeErased = annotationHelper.getTypeUtils()
            .erasure(httpMessageConverterType);
    List<DeclaredType> converters = annotationHelper.extractAnnotationClassArrayParameter(element,
            annotationHelper.getTarget(), "converters");

    if (converters == null || converters.isEmpty()) {
        valid.invalidate();
        annotationHelper.printAnnotationError(element, "At least one converter is required");
        return;
    }

    for (DeclaredType converterType : converters) {
        TypeMirror erasedConverterType = annotationHelper.getTypeUtils().erasure(converterType);
        if (annotationHelper.isSubtype(erasedConverterType, httpMessageConverterTypeErased)) {
            Element converterElement = converterType.asElement();
            if (converterElement.getKind().isClass()) {
                if (!annotationHelper.isAbstract(converterElement)) {
                    if (converterElement.getAnnotation(EBean.class) == null) {
                        List<ExecutableElement> constructors = ElementFilter
                                .constructorsIn(converterElement.getEnclosedElements());
                        boolean hasPublicWithNoArgumentConstructor = false;
                        for (ExecutableElement constructor : constructors) {
                            if (annotationHelper.isPublic(constructor)
                                    && constructor.getParameters().isEmpty()) {
                                hasPublicWithNoArgumentConstructor = true;
                            }
                        }
                        if (!hasPublicWithNoArgumentConstructor) {
                            valid.invalidate();
                            annotationHelper.printAnnotationError(element,
                                    "The converter class must have a public no argument constructor");
                        }
                    }
                } else {
                    valid.invalidate();
                    annotationHelper.printAnnotationError(element, "The converter class must not be abstract");
                }
            } else {
                valid.invalidate();
                annotationHelper.printAnnotationError(element, "The converter class must be a class");
            }
        } else {
            valid.invalidate();
            annotationHelper.printAnnotationError(element,
                    "The converter class must be a subtype of " + HTTP_MESSAGE_CONVERTER);
        }
    }
}

From source file:org.androidannotations.helper.ValidatorHelper.java

public void validateInterceptors(Element element, IsValid valid) {
    TypeMirror clientHttpRequestInterceptorType = annotationHelper
            .typeElementFromQualifiedName(CLIENT_HTTP_REQUEST_INTERCEPTOR).asType();
    TypeMirror clientHttpRequestInterceptorTypeErased = annotationHelper.getTypeUtils()
            .erasure(clientHttpRequestInterceptorType);
    List<DeclaredType> interceptors = annotationHelper.extractAnnotationClassArrayParameter(element,
            annotationHelper.getTarget(), "interceptors");
    if (interceptors == null) {
        return;/*from  w  w w.  jav a2s. c  o m*/
    }
    for (DeclaredType interceptorType : interceptors) {
        TypeMirror erasedInterceptorType = annotationHelper.getTypeUtils().erasure(interceptorType);
        if (annotationHelper.isSubtype(erasedInterceptorType, clientHttpRequestInterceptorTypeErased)) {
            Element interceptorElement = interceptorType.asElement();
            if (interceptorElement.getKind().isClass()) {
                if (!annotationHelper.isAbstract(interceptorElement)) {
                    if (interceptorElement.getAnnotation(EBean.class) == null) {
                        List<ExecutableElement> constructors = ElementFilter
                                .constructorsIn(interceptorElement.getEnclosedElements());
                        boolean hasPublicWithNoArgumentConstructor = false;
                        for (ExecutableElement constructor : constructors) {
                            if (annotationHelper.isPublic(constructor)
                                    && constructor.getParameters().isEmpty()) {
                                hasPublicWithNoArgumentConstructor = true;
                            }
                        }
                        if (!hasPublicWithNoArgumentConstructor) {
                            valid.invalidate();
                            annotationHelper.printAnnotationError(element,
                                    "The interceptor class must have a public no argument constructor or be annotated with @EBean");
                        }
                    }
                } else {
                    valid.invalidate();
                    annotationHelper.printAnnotationError(element,
                            "The interceptor class must not be abstract");
                }
            } else {
                valid.invalidate();
                annotationHelper.printAnnotationError(element, "The interceptor class must be a class");
            }
        } else {
            valid.invalidate();
            annotationHelper.printAnnotationError(element,
                    "The interceptor class must be a subtype of " + CLIENT_HTTP_REQUEST_INTERCEPTOR);
        }
    }
}

From source file:org.androidannotations.helper.ValidatorHelper.java

public void validateRequestFactory(Element element, IsValid valid) {
    TypeMirror clientHttpRequestFactoryType = annotationHelper
            .typeElementFromQualifiedName(CLIENT_HTTP_REQUEST_FACTORY).asType();
    DeclaredType requestFactory = annotationHelper.extractAnnotationClassParameter(element,
            annotationHelper.getTarget(), "requestFactory");
    if (requestFactory != null) {
        if (annotationHelper.isSubtype(requestFactory, clientHttpRequestFactoryType)) {
            Element requestFactoryElement = requestFactory.asElement();
            if (requestFactoryElement.getKind().isClass()) {
                if (!annotationHelper.isAbstract(requestFactoryElement)) {
                    if (requestFactoryElement.getAnnotation(EBean.class) != null) {
                        return;
                    }//from w w  w  .ja  va 2  s .  c  o  m
                    List<ExecutableElement> constructors = ElementFilter
                            .constructorsIn(requestFactoryElement.getEnclosedElements());
                    for (ExecutableElement constructor : constructors) {
                        if (annotationHelper.isPublic(constructor) && constructor.getParameters().isEmpty()) {
                            return;
                        }
                    }
                    valid.invalidate();
                    annotationHelper.printAnnotationError(element,
                            "The requestFactory class must have a public no argument constructor or must be annotated with @EBean");
                } else {
                    valid.invalidate();
                    annotationHelper.printAnnotationError(element,
                            "The requestFactory class must not be abstract");
                }
            } else {
                valid.invalidate();
                annotationHelper.printAnnotationError(element, "The requestFactory class must be a class");
            }
        } else {
            valid.invalidate();
            annotationHelper.printAnnotationError(element,
                    "The requestFactory class must be a subtype of " + CLIENT_HTTP_REQUEST_FACTORY);
        }
    }
}