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:org.lambdamatic.mongodb.apt.template.ElementUtils.java

/**
 * Checks if the given {@link Element} is, implements or extends the given target type.
 * //from   ww w  .j  av  a  2 s. co m
 * @param type the element to analyze
 * @param targetType the type to check
 * @return <code>true</code> if the given {@link Element} corresponds to a type that implements
 *         {@link List}, <code>false</code> otherwise.
 */
public static boolean isAssignable(final DeclaredType type, final Class<?> targetType) {
    if (type instanceof NoType) {
        return false;
    }
    if (type.asElement().toString().equals(targetType.getName())) {
        return true;
    }
    final TypeElement element = (TypeElement) type.asElement();
    final boolean implementation = element.getInterfaces().stream()
            .filter(interfaceMirror -> interfaceMirror.getKind() == TypeKind.DECLARED)
            .map(interfaceMirror -> (DeclaredType) interfaceMirror)
            .map(declaredInterface -> declaredInterface.asElement())
            .anyMatch(declaredElement -> declaredElement.toString().equals(targetType.getName()));
    if (implementation) {
        return true;
    }
    if (element.getSuperclass().getKind() == TypeKind.DECLARED) {
        return isAssignable((DeclaredType) (element.getSuperclass()), targetType);
    }
    return false;
}

From source file:org.lambdamatic.mongodb.apt.template.TemplateType.java

/**
 * Returns the {@link TemplateType} for the given {@link VariableType}, or throws a
 * {@link MetadataGenerationException} if it was not a known or supported type.
 * /*from ww w.  j a  v a  2s. co m*/
 * @param variableType the variable to analyze
 * @return the corresponding {@link TemplateType}
 * @throws MetadataGenerationException if the given variable type is not supported
 */
private static TemplateType getMetadataFieldType(final TypeMirror variableType,
        final BiFunction<PrimitiveType, ProcessingEnvironment, TemplateType> primitiveTypeToTemplateType,
        final Function<DeclaredType, TemplateType> embeddedDocumentToTemplateType,
        final BiFunction<TypeMirror, ProcessingEnvironment, TemplateType> collectionToTemplateType,
        final BiFunction<DeclaredType, ProcessingEnvironment, TemplateType> mapToTemplateType,
        final Function<DeclaredType, TemplateType> declaredTypeToTemplateType,
        final ProcessingEnvironment processingEnv) throws MetadataGenerationException {
    if (variableType instanceof PrimitiveType) {
        return primitiveTypeToTemplateType.apply((PrimitiveType) variableType, processingEnv);
    } else if (variableType instanceof DeclaredType) {
        final DeclaredType declaredType = (DeclaredType) variableType;
        final TypeElement declaredElement = (TypeElement) declaredType.asElement();
        if (declaredElement.getAnnotation(EmbeddedDocument.class) != null) {
            // embedded documents
            return embeddedDocumentToTemplateType.apply(declaredType);
        } else if (ElementUtils.isAssignable(declaredType, Collection.class)) {
            // collections (list/set)
            return collectionToTemplateType.apply(declaredType.getTypeArguments().get(0), processingEnv);
        } else if (ElementUtils.isAssignable(declaredType, Map.class)) {
            // map
            return mapToTemplateType.apply(declaredType, processingEnv);
        } else {
            return declaredTypeToTemplateType.apply(declaredType);
        }
    } else if (variableType.getKind() == TypeKind.ARRAY) {
        final TypeMirror componentType = ((ArrayType) variableType).getComponentType();
        if (componentType instanceof PrimitiveType) {
            final PrimitiveType primitiveType = (PrimitiveType) componentType;
            final TypeElement boxedClass = processingEnv.getTypeUtils().boxedClass(primitiveType);
            return collectionToTemplateType.apply(boxedClass.asType(), processingEnv);
        }
        return collectionToTemplateType.apply(componentType, processingEnv);
    }
    throw new MetadataGenerationException("Unexpected variable type: " + variableType);
}

From source file:com.github.hackersun.processor.factory.FactoryAnnotatedClass.java

/**
 * @throws ProcessingException if id() from annotation is null
 *//*from   ww w  . j a  v a  2  s.  c  o m*/
public FactoryAnnotatedClass(TypeElement classElement) throws ProcessingException {
    this.annotatedClassElement = classElement;
    Factory annotation = classElement.getAnnotation(Factory.class);
    id = annotation.id();

    if (StringUtils.isEmpty(id)) {
        throw new ProcessingException(classElement,
                "id() in @%s for class %s is null or empty! that's not allowed", Factory.class.getSimpleName(),
                classElement.getQualifiedName().toString());
    }

    // Get the full QualifiedTypeName
    try {
        Class<?> clazz = annotation.type();
        qualifiedGroupClassName = clazz.getCanonicalName();
        simpleFactoryGroupName = clazz.getSimpleName();
    } catch (MirroredTypeException mte) {
        DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
        TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement();
        qualifiedGroupClassName = classTypeElement.getQualifiedName().toString();
        simpleFactoryGroupName = classTypeElement.getSimpleName().toString();
    }
}

From source file:org.mule.devkit.generation.mule.transfomer.JaxbTransformerGenerator.java

private DefinedClass getJaxbTransformerClass(Method executableElement, Variable variable) {
    DeclaredType declaredType = (DeclaredType) variable.asType();
    XmlType xmlType = declaredType.asElement().getAnnotation(XmlType.class);
    Package pkg = ctx().getCodeModel()
            ._package(executableElement.parent().getPackageName() + NamingConstants.TRANSFORMERS_NAMESPACE);

    return pkg._class(StringUtils.capitalize(xmlType.name()) + "JaxbTransformer", AbstractTransformer.class,
            new Class<?>[] { DiscoverableTransformer.class });
}

From source file:org.mule.devkit.module.generation.JaxbTransformerGenerator.java

private DefinedClass getJaxbTransformerClass(ExecutableElement executableElement, VariableElement variable) {
    DeclaredType declaredType = (DeclaredType) variable.asType();
    XmlType xmlType = declaredType.asElement().getAnnotation(XmlType.class);
    TypeElement parentClass = ElementFilter.typesIn(Arrays.asList(executableElement.getEnclosingElement()))
            .get(0);/*from ww  w . j  a v  a  2  s .  com*/
    String packageName = context.getNameUtils()
            .getPackageName(context.getElementsUtils().getBinaryName(parentClass).toString()) + ".config";
    Package pkg = context.getCodeModel()._package(packageName);
    DefinedClass jaxbTransformer = pkg._class(StringUtils.capitalize(xmlType.name()) + "JaxbTransformer",
            AbstractTransformer.class, new Class<?>[] { DiscoverableTransformer.class });

    return jaxbTransformer;
}

From source file:org.mule.devkit.generation.mule.transfomer.JaxbTransformerGenerator.java

private void generateConstructor(DefinedClass jaxbTransformerClass, Variable variable) {
    // generate constructor
    org.mule.devkit.model.code.Method constructor = jaxbTransformerClass.constructor(Modifier.PUBLIC);

    // register source data type
    registerSourceType(constructor);/*  w w w. jav a2  s .co  m*/

    // register destination data type
    registerDestinationType(constructor, variable);

    DeclaredType declaredType = (DeclaredType) variable.asType();
    XmlType xmlType = declaredType.asElement().getAnnotation(XmlType.class);

    constructor.body().invoke("setName").arg(StringUtils.capitalize(xmlType.name()) + "JaxbTransformer");
}

From source file:org.mule.devkit.module.generation.JaxbTransformerGenerator.java

private void generateConstructor(DefinedClass jaxbTransformerClass, ExecutableElement executableElement,
        VariableElement variable) {
    // generate constructor
    Method constructor = jaxbTransformerClass.constructor(Modifier.PUBLIC);

    // register source data type
    registerSourceType(constructor);//from w  ww.  java2 s .  c  o m

    // register destination data type
    registerDestinationType(constructor, variable);

    DeclaredType declaredType = (DeclaredType) variable.asType();
    XmlType xmlType = declaredType.asElement().getAnnotation(XmlType.class);

    constructor.body().invoke("setName").arg(StringUtils.capitalize(xmlType.name()) + "JaxbTransformer");
}

From source file:com.github.pellaton.springconfigvalidation.SpringConfigurationValidationProcessor.java

private boolean isInConfigurationClass(ExecutableElement methodElement) {
    Element enclosingElement = methodElement.getEnclosingElement();

    List<? extends AnnotationMirror> annotationMirrors = enclosingElement.getAnnotationMirrors();
    for (AnnotationMirror annotationMirror : annotationMirrors) {
        DeclaredType annotationType = annotationMirror.getAnnotationType();

        if (this.configurationTypeElement.equals(annotationType.asElement())) {
            return true;
        }//ww w .  jav  a  2 s.com
    }

    return false;
}

From source file:org.androidtransfuse.adapter.element.ASTElementFactory.java

public ASTType buildASTElementType(DeclaredType declaredType) {

    ASTType astType = getType((TypeElement) declaredType.asElement());

    if (!declaredType.getTypeArguments().isEmpty()) {
        return astFactory.buildGenericTypeWrapper(astType, astFactory.buildParameterBuilder(declaredType));
    }/* www .j a  va  2  s  .co m*/
    return astType;
}

From source file:easymvp.compiler.EasyMVPProcessor.java

private TypeElement getTypeElement(MirroredTypeException mte) {
    DeclaredType declaredType = (DeclaredType) mte.getTypeMirror();
    return (TypeElement) declaredType.asElement();
}