Java Code Examples for io.micronaut.core.annotation.AnnotationValue#builder()

The following examples show how to use io.micronaut.core.annotation.AnnotationValue#builder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScheduledAnnotationMapper.java    From micronaut-spring with Apache License 2.0 6 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final AnnotationValueBuilder<?> builder = AnnotationValue.builder(Scheduled.class);
    annotation.get("cron", String.class).ifPresent(s -> builder.member("cron", s));

    // members that take numbers needed "ms" appending
    annotation.get("fixedDelay", String.class).ifPresent(s -> builder.member("fixedDelay", s + "ms"));
    annotation.get("fixedRate", String.class).ifPresent(s -> builder.member("fixedRate", s + "ms"));
    annotation.get("initialDelay", String.class).ifPresent(s -> builder.member("initialDelay", s + "ms"));

    annotation.get("fixedDelayString", String.class).ifPresent(s -> builder.member("fixedDelay", s));
    annotation.get("fixedRateString", String.class).ifPresent(s -> builder.member("fixedRate", s));
    annotation.get("initialDelayString", String.class).ifPresent(s -> builder.member("initialDelay", s));

    final AnnotationValue<?> scheduledAnn = builder
            .build();
    return Collections.singletonList(scheduledAnn);
}
 
Example 2
Source File: BeanAnnotationMapper.java    From micronaut-spring with Apache License 2.0 6 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    List<AnnotationValue<?>> newAnnotations = new ArrayList<>(3);
    final AnnotationValueBuilder<Bean> beanAnn = AnnotationValue.builder(Bean.class);

    final Optional<String> destroyMethod = annotation.get("destroyMethod", String.class);
    destroyMethod.ifPresent(s -> beanAnn.member("preDestroy", s));
    newAnnotations.add(beanAnn.build());
    newAnnotations.add(AnnotationValue.builder(DefaultScope.class)
            .value(Singleton.class)
            .build());
    final String beanName = annotation.getValue(String.class).orElse(annotation.get("name", String.class).orElse(null));

    if (StringUtils.isNotEmpty(beanName)) {
        newAnnotations.add(AnnotationValue.builder(Named.class).value(beanName).build());
    }

    return newAnnotations;
}
 
Example 3
Source File: TransactionalAnnotationMapper.java    From micronaut-spring with Apache License 2.0 6 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final AnnotationValueBuilder<?> builder = AnnotationValue.builder("io.micronaut.spring.tx.annotation.Transactional");
    annotation.getValue(String.class).ifPresent(s -> {
        builder.value(s);
        builder.member("transactionManager", s);
    });

    Stream.of("propagation", "isolation", "transactionManager")
            .forEach(member -> annotation.get(member, String.class).ifPresent(s -> builder.member(member, s)));
    Stream.of("rollbackForClassName", "noRollbackForClassName")
            .forEach(member -> annotation.get(member, String[].class).ifPresent(s -> builder.member(member, s)));
    Stream.of("rollbackFor", "noRollbackFor")
            .forEach(member -> annotation.get(member, AnnotationClassValue[].class).ifPresent(classValues -> {
                String[] names = new String[classValues.length];
                for (int i = 0; i < classValues.length; i++) {
                    AnnotationClassValue classValue = classValues[i];
                    names[i] = classValue.getName();
                }
                builder.member(member, names);
            }));
    annotation.get("timeout", Integer.class).ifPresent(integer -> builder.member("timeout", integer));
    annotation.get("readOnly", Boolean.class).ifPresent(bool -> builder.member("readOnly", bool));

    return Collections.singletonList(builder.build());
}
 
Example 4
Source File: ConditionalOnPropertyAnnotationMapper.java    From micronaut-spring with Apache License 2.0 6 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final String[] propertyNames = annotation.getValue(String[].class).orElseGet(() -> annotation.get("name", String[].class).orElse(null));
    if (propertyNames != null) {
        final String prefix = annotation.get("prefix", String.class).orElse(null);
        final boolean matchIfMissing = annotation.get("matchIfMissing", boolean.class).orElse(false);
        final String havingValue = annotation.get("havingValue", String.class).orElse(null);
        List<AnnotationValue<?>> annotationValues = new ArrayList<>(propertyNames.length);
        for (String propertyName : propertyNames) {
            if (prefix != null) {
                propertyName = prefix + "." + propertyName;
            }

            final AnnotationValueBuilder<Requires> builder = AnnotationValue.builder(Requires.class);
            builder.member(matchIfMissing ? "missingProperty" : "property", propertyName);
            if (havingValue != null) {
                builder.value(havingValue);
            }
            annotationValues.add(builder.build());

        }

        return annotationValues;
    }
    return Collections.emptyList();
}
 
Example 5
Source File: SpringTransactionalEventListenerMapper.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
@Override
public List<AnnotationValue<?>> map(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final AnnotationValueBuilder<Annotation> ann =
            AnnotationValue.builder("io.micronaut.transaction.annotation.TransactionalEventListener");
    annotation.stringValue("phase").ifPresent(ann::value);
    return Collections.singletonList(
            ann.build()
    );
}
 
Example 6
Source File: EndpointAnnotationMapper.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final Optional<String> id = annotation.get("id", String.class);
    if (id.isPresent()) {
        final AnnotationValueBuilder<?> builder = AnnotationValue.builder("io.micronaut.management.endpoint.annotation.Endpoint");
        final Boolean enableByDefault = annotation.get("enableByDefault", boolean.class).orElse(true);
        builder.value(id.get());
        builder.member("id", id.get());
        builder.member("defaultEnabled", enableByDefault);
        return Collections.singletonList(builder.build());
    }

    return Collections.emptyList();
}
 
Example 7
Source File: WebBindAnnotationMapper.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    List<AnnotationValue<?>> mappedAnnotations = new ArrayList<>();

    final String name = annotation.getValue(String.class).orElseGet(() -> annotation.get("name", String.class).orElse(null));
    final String defaultValue = annotation.get("defaultValue", String.class).orElse(null);
    final boolean required = annotation.get("required", boolean.class).orElse(true);

    final AnnotationValueBuilder<?> builder = AnnotationValue.builder(annotationType());
    final AnnotationValueBuilder<Bindable> bindableBuilder = AnnotationValue.builder(Bindable.class);
    if (StringUtils.isNotEmpty(name)) {
        builder.value(name);
        bindableBuilder.value(name);
    }
    if (StringUtils.isNotEmpty(defaultValue)) {
        builder.member("defaultValue", name);
        bindableBuilder.member("defaultValue", defaultValue);
    }

    mappedAnnotations.add(builder.build());
    mappedAnnotations.add(bindableBuilder.build());
    if (!required) {
        mappedAnnotations.add(AnnotationValue.builder(Nullable.class).build());
    }

    return mappedAnnotations;
}
 
Example 8
Source File: RequestMappingAnnotationMapper.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new builder for the given http method.
 * @param httpMethod The method
 * @param annotation The annotation
 * @return The builder
 */
protected @Nonnull AnnotationValueBuilder<?> newBuilder(
        @Nullable HttpMethod httpMethod,
        AnnotationValue<Annotation> annotation) {

    if (httpMethod != null) {
        switch (httpMethod) {
            case TRACE:
                return AnnotationValue.builder(Trace.class);
            case DELETE:
                return AnnotationValue.builder(Delete.class);
            case GET:
                return AnnotationValue.builder(Get.class);
            case HEAD:
                return AnnotationValue.builder(Head.class);
            case POST:
                return AnnotationValue.builder(Post.class);
            case PUT:
                return AnnotationValue.builder(Put.class);
            case PATCH:
                return AnnotationValue.builder(Patch.class);
            case OPTIONS:
                return AnnotationValue.builder(Options.class);
            default:
                return AnnotationValue.builder("io.micronaut.http.annotation.UriMapping");
        }
    } else {
        return AnnotationValue.builder("io.micronaut.http.annotation.UriMapping");
    }
}
 
Example 9
Source File: ExceptionHandlerAnnotationMapper.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final AnnotationValueBuilder<Error> builder = AnnotationValue.builder(Error.class);
    annotation.getValue(AnnotationClassValue.class).ifPresent(annotationClassValue -> builder.member("value", annotationClassValue));
    return Collections.singletonList(
            builder
                .build()
    );
}
 
Example 10
Source File: RequestBodyAnnotationMapping.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    List<AnnotationValue<?>> mappedAnnotations = new ArrayList<>();
    final boolean required = annotation.get("required", boolean.class).orElse(true);
    final AnnotationValueBuilder<?> builder = AnnotationValue.builder(Body.class);
    final AnnotationValueBuilder<Bindable> bindableBuilder = AnnotationValue.builder(Bindable.class);
    mappedAnnotations.add(builder.build());
    mappedAnnotations.add(bindableBuilder.build());
    if (!required) {
        mappedAnnotations.add(AnnotationValue.builder(Nullable.class).build());
    }
    return mappedAnnotations;
}
 
Example 11
Source File: TableAnnotationMapper.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
@Override
public List<AnnotationValue<?>> map(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final AnnotationValueBuilder<MappedEntity> builder = AnnotationValue.builder(MappedEntity.class);
    annotation.stringValue("name").ifPresent(builder::value);
    annotation.stringValue(SqlMembers.CATALOG).ifPresent(catalog -> builder.member(SqlMembers.CATALOG, catalog));
    annotation.stringValue(SqlMembers.SCHEMA).ifPresent(catalog -> builder.member(SqlMembers.SCHEMA, catalog));
    return Collections.singletonList(builder.build());
}
 
Example 12
Source File: ManyToManyMapper.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
@Override
public List<AnnotationValue<?>> map(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final AnnotationValueBuilder<Relation> relationBuilder = AnnotationValue.builder(Relation.class);
    annotation.enumValue("cascade", Relation.Cascade.class).ifPresent(c ->
            relationBuilder.member("cascade", c)
    );
    AnnotationValue<Relation> ann = relationBuilder.value(Relation.Kind.MANY_TO_MANY).build();

    return Collections.singletonList(ann);
}
 
Example 13
Source File: SpringRepositoryMapper.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
@Override
public List<AnnotationValue<?>> map(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    final AnnotationValueBuilder<Repository> builder = AnnotationValue.builder(Repository.class);
    annotation.stringValue("org.springframework.stereotype.Repository").ifPresent(builder::value);
    return Collections.singletonList(
            builder
                .build()
    );
}
 
Example 14
Source File: DeleteMappingAnnotationMapper.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
protected AnnotationValueBuilder<?> newBuilder(HttpMethod httpMethod, AnnotationValue<Annotation> annotation) {
    return AnnotationValue.builder(Delete.class);
}
 
Example 15
Source File: GetMappingAnnotationMapper.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
protected AnnotationValueBuilder<?> newBuilder(HttpMethod httpMethod, AnnotationValue<Annotation> annotation) {
    return AnnotationValue.builder(Get.class);
}
 
Example 16
Source File: PatchMappingAnnotationMapper.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
protected AnnotationValueBuilder<?> newBuilder(HttpMethod httpMethod, AnnotationValue<Annotation> annotation) {
    return AnnotationValue.builder(Patch.class);
}
 
Example 17
Source File: PutMappingAnnotationMapper.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
protected AnnotationValueBuilder<?> newBuilder(HttpMethod httpMethod, AnnotationValue<Annotation> annotation) {
    return AnnotationValue.builder(Put.class);
}
 
Example 18
Source File: PostMappingAnnotationMapper.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
protected AnnotationValueBuilder<?> newBuilder(HttpMethod httpMethod, AnnotationValue<Annotation> annotation) {
    return AnnotationValue.builder(Post.class);
}
 
Example 19
Source File: CacheEvictAnnotationMapper.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
protected @Nonnull AnnotationValueBuilder<? extends Annotation> buildAnnotation() {
    return AnnotationValue.builder(CacheInvalidate.class);
}
 
Example 20
Source File: CacheableAnnotationMapper.java    From micronaut-spring with Apache License 2.0 2 votes vote down vote up
/**
 * Builds the target annotation.
 * @return The annotation builder
 */
protected @Nonnull AnnotationValueBuilder<? extends Annotation> buildAnnotation() {
    return AnnotationValue.builder(Cacheable.class);
}