Example usage for javax.lang.model.element TypeElement getAnnotation

List of usage examples for javax.lang.model.element TypeElement getAnnotation

Introduction

In this page you can find the example usage for javax.lang.model.element TypeElement getAnnotation.

Prototype

@Override
<A extends Annotation> A getAnnotation(Class<A> annotationType);

Source Link

Usage

From source file:org.leandreck.endpoints.processor.model.EndpointNodeFactory.java

private static String defineUrl(final TypeElement typeElement) {
    final RequestMapping requestMapping = typeElement.getAnnotation(RequestMapping.class);
    if (requestMapping != null) {
        final String[] mappings = requestMapping.value();
        if (mappings.length > 0) {
            return mappings[0];
        }//from w  ww.  jav  a 2s.c  om
    }
    return "";
}

From source file:net.minecrell.quartz.mappings.processor.MappingsGeneratorProcessor.java

private static ClassMapper createMapper(List<TypeElement> mappingClasses) {
    ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder();

    for (TypeElement element : mappingClasses) {
        String mapping = element.getAnnotation(Mapping.class).value();
        if (!mapping.isEmpty()) {
            classes.put(mapping, getInternalName(element));
        }/* w  w w.  j av  a  2 s.co m*/
    }

    return new ClassMapper(classes.build());
}

From source file:cop.raml.processor.rest.SpringRestImpl.java

private static String getClassRequestPath(TypeElement classElement) {
    if (classElement == null || classElement.getKind() != ElementKind.CLASS)
        return null;

    RestController annotation = classElement.getAnnotation(RestController.class);
    return annotation != null && StringUtils.isNoneBlank(annotation.value()) ? annotation.value() : null;
}

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   w  w  w.j a v 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:cop.raml.utils.example.JsonExample.java

/**
 * Returns all ignored fields for given {@code typeElement}. This time support only {@link JsonIgnoreProperties} and {@link JsonIgnore}
 * annotations. But in the following article <a href="http://www.baeldung.com/jackson-ignore-properties-on-serialization">Jackson Ignore
 * Properties on Marshalling</a>, more ways to ignore properties can be found, but there're not supported this time.
 *
 * @param typeElement type element//from  ww  w .  j a va 2 s. co m
 * @return not {@code null} list of ignored fields
 */
@NotNull
private static Set<String> getIgnoredFields(TypeElement typeElement) {
    if (typeElement == null)
        return Collections.emptySet();

    Set<String> res = new HashSet<>();
    JsonIgnoreProperties annotation = typeElement.getAnnotation(JsonIgnoreProperties.class);

    if (annotation != null && ArrayUtils.isNotEmpty(annotation.value()))
        Collections.addAll(res, annotation.value());

    JsonIgnore ann;

    for (Element element : typeElement.getEnclosedElements())
        if ((ann = element.getAnnotation(JsonIgnore.class)) != null && ann.value())
            res.add(element.toString());

    return res;
}

From source file:co.touchlab.squeaky.processor.AnnotationProcessor.java

private static String extractTableName(TypeElement element) {
    DatabaseTable databaseTable = element.getAnnotation(DatabaseTable.class);
    DatabaseView databaseView = element.getAnnotation(DatabaseView.class);
    if (databaseTable != null && StringUtils.isNotEmpty(databaseTable.tableName())) {
        return databaseTable.tableName();
    } else if (databaseView != null && StringUtils.isNotEmpty(databaseView.viewName())) {
        return databaseView.viewName();
    } else {//from w ww.  j av a  2 s  . c  om
        // if the name isn't specified, it is the class name lowercased
        return element.getSimpleName().toString().toLowerCase();
    }
}

From source file:org.leandreck.endpoints.processor.model.EndpointNodeFactory.java

public EndpointNode createEndpointNode(final TypeElement typeElement) {

    final TypeScriptEndpoint annotation = typeElement.getAnnotation(TypeScriptEndpoint.class);

    final String name = defineName(typeElement, annotation);
    final String url = defineUrl(typeElement);
    final String template = defineTemplate(annotation);
    final List<MethodNode> methods = defineMethods(typeElement, (DeclaredType) typeElement.asType());

    return new EndpointNode(name, url, template, methods, configuration.getGlobalPrintConfiguration());
}

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

/**
 * @throws ProcessingException if id() from annotation is null
 *///www. j av a2 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:com.googlecode.androidannotations.processing.rest.RestProcessor.java

@Override
public void process(Element element, JCodeModel codeModel, EBeansHolder activitiesHolder) throws Exception {

    RestImplementationHolder holder = restImplementationHolder.create(element);

    TypeElement typeElement = (TypeElement) element;

    holder.urlPrefix = typeElement.getAnnotation(Rest.class).value();

    String interfaceName = typeElement.getQualifiedName().toString();

    String implementationName = interfaceName + ModelConstants.GENERATION_SUFFIX;

    // holder.restImplementationClass = codeModel._class(JMod.PUBLIC |
    // JMod.ABSTRACT, implementationName, ClassType.CLASS);
    holder.restImplementationClass = codeModel._class(JMod.PUBLIC, implementationName, ClassType.CLASS);
    JClass interfaceClass = holder.refClass(interfaceName);
    holder.restImplementationClass._implements(interfaceClass);

    // RestTemplate field
    JClass restTemplateClass = holder.refClass(SPRING_REST_TEMPLATE_QUALIFIED_NAME);
    holder.restTemplateField = holder.restImplementationClass.field(JMod.PRIVATE, restTemplateClass,
            "restTemplate");

    // Default constructor
    JMethod defaultConstructor = holder.restImplementationClass.constructor(JMod.PUBLIC);
    defaultConstructor.body().assign(holder.restTemplateField, JExpr._new(restTemplateClass));

    // RestTemplate constructor
    JMethod restTemplateConstructor = holder.restImplementationClass.constructor(JMod.PUBLIC);
    JVar restTemplateParam = restTemplateConstructor.param(restTemplateClass, "restTemplate");
    restTemplateConstructor.body().assign(JExpr._this().ref(holder.restTemplateField), restTemplateParam);

    // RequestFactory constructor
    JMethod requestFactoryConstructor = holder.restImplementationClass.constructor(JMod.PUBLIC);
    JClass requestFactoryClass = holder.refClass("org.springframework.http.client.ClientHttpRequestFactory");
    JVar requestFactoryParam = requestFactoryConstructor.param(requestFactoryClass, "requestFactory");
    requestFactoryConstructor.body().assign(holder.restTemplateField,
            JExpr._new(restTemplateClass).arg(requestFactoryParam));

    // Implement getRestTemplate method
    List<? extends Element> enclosedElements = typeElement.getEnclosedElements();
    List<ExecutableElement> methods = ElementFilter.methodsIn(enclosedElements);
    for (ExecutableElement method : methods) {
        if (method.getParameters().size() == 0
                && method.getReturnType().toString().equals(SPRING_REST_TEMPLATE_QUALIFIED_NAME)) {
            String methodName = method.getSimpleName().toString();
            JMethod getRestTemplateMethod = holder.restImplementationClass.method(JMod.PUBLIC,
                    restTemplateClass, methodName);
            getRestTemplateMethod.annotate(Override.class);
            getRestTemplateMethod.body()._return(holder.restTemplateField);
            break; // Only one implementation
        }//from   w  ww. j  a  va2  s  . c  o m
    }

    for (ExecutableElement method : methods) {
        List<? extends VariableElement> parameters = method.getParameters();
        if (parameters.size() == 1 && method.getReturnType().getKind() == TypeKind.VOID) {
            VariableElement firstParameter = parameters.get(0);
            if (firstParameter.asType().toString().equals(SPRING_REST_TEMPLATE_QUALIFIED_NAME)) {
                String methodName = method.getSimpleName().toString();
                JMethod setRestTemplateMethod = holder.restImplementationClass.method(JMod.PUBLIC,
                        codeModel.VOID, methodName);
                setRestTemplateMethod.annotate(Override.class);

                JVar restTemplateSetterParam = setRestTemplateMethod.param(restTemplateClass,
                        firstParameter.getSimpleName().toString());

                setRestTemplateMethod.body().assign(_this().ref(holder.restTemplateField),
                        restTemplateSetterParam);
                break; // Only one implementation
            }
        }
    }

}

From source file:com.github.hexocraftapi.checkargs.processor.AnnotatedClass.java

/**
 * @param classElement TypeElement/*from   ww  w  . ja v  a  2s . c o  m*/
 * @param elementUtils TypeElement
 * @throws ClassNotFoundException ClassNotFoundException
 */
public AnnotatedClass(TypeElement classElement, Elements elementUtils) throws ClassNotFoundException {
    this.annotatedClassElement = classElement;

    CheckArgs annotation = classElement.getAnnotation(CheckArgs.class);
    List<? extends AnnotationMirror> annotationMirrors = elementUtils.getAllAnnotationMirrors(classElement);

    for (AnnotationMirror annotationMirror : annotationMirrors) {
        Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror
                .getElementValues();

        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues
                .entrySet()) {
            String key = entry.getKey().getSimpleName().toString();
            Object value = entry.getValue().getValue();

            switch (key) {
            case "count":
                this.count = (int) value;
                break;
            case "types":
                List<? extends AnnotationValue> typeMirrors = (List<? extends AnnotationValue>) value;
                for (AnnotationValue annotationValue : typeMirrors) {
                    String clazz = annotationValue.getValue().toString();
                    this.qualifiedClassName.add(clazz);
                    this.simpleClassName.add(clazz.substring(clazz.lastIndexOf(".") + 1));
                }
                break;
            }
        }
    }

    if (this.qualifiedClassName.size() > 0 && this.count != this.qualifiedClassName.size())
        this.count = this.qualifiedClassName.size();
}