Example usage for javax.annotation.processing ProcessingEnvironment getTypeUtils

List of usage examples for javax.annotation.processing ProcessingEnvironment getTypeUtils

Introduction

In this page you can find the example usage for javax.annotation.processing ProcessingEnvironment getTypeUtils.

Prototype

Types getTypeUtils();

Source Link

Document

Returns an implementation of some utility methods for operating on types.

Usage

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  www . j  av  a  2  s.c  o 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:co.touchlab.squeaky.processor.AnnotationProcessor.java

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    typeUtils = processingEnv.getTypeUtils();
    elementUtils = processingEnv.getElementUtils();
    filer = processingEnv.getFiler();// ww w. j a v a 2 s  .c  o  m
    messager = processingEnv.getMessager();
}