io.micronaut.core.annotation.AnnotationMetadataProvider Java Examples

The following examples show how to use io.micronaut.core.annotation.AnnotationMetadataProvider. 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: AnnotatedRequestHandler.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
@Override
default boolean canHandle(HandlerInput handlerInput) {
    final Class<? extends AnnotatedRequestHandler> type = getClass();
    final String annotationMetadata = type.getPackage().getName() + ".$" + type.getSimpleName() + "DefinitionClass";
    final AnnotationMetadata metadata = ClassUtils.forName(annotationMetadata, type.getClassLoader()).flatMap(aClass -> {
        final Object o = InstantiationUtils.tryInstantiate(aClass).orElse(null);
        if (o instanceof AnnotationMetadataProvider) {
            return Optional.of(((AnnotationMetadataProvider) o).getAnnotationMetadata());
        }
        return Optional.empty();
    }).orElse(AnnotationMetadata.EMPTY_METADATA);
    final String[] names = metadata.getValue(IntentHandler.class, String[].class).orElse(StringUtils.EMPTY_STRING_ARRAY);
    return Arrays.stream(names).anyMatch(n -> handlerInput.matches(intentName(n)));
}
 
Example #2
Source File: EntitiesInPackageCondition.java    From micronaut-sql with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context) {
    final AnnotationMetadataProvider component = context.getComponent();
    if (component instanceof BeanDefinition) {
        BeanDefinition<?> definition = (BeanDefinition<?>) component;
        final BeanContext beanContext = context.getBeanContext();
        if (beanContext instanceof ApplicationContext) {

            final Optional<String> name = definition instanceof NameResolver ? ((NameResolver) definition).resolveName() : Optional.empty();
            final Qualifier<JpaConfiguration> q = Qualifiers.byName(name.orElse("default"));
            final Optional<JpaConfiguration> jpaConfiguration = beanContext.findBean(JpaConfiguration.class, q);
            JpaConfiguration.EntityScanConfiguration entityScanConfig = jpaConfiguration.map(JpaConfiguration::getEntityScanConfiguration).orElse(null);

            if (entityScanConfig == null) {
                return false;
            }

            boolean isClasspathScanEnabled = entityScanConfig.isClasspath();
            String[] packagesToScan = entityScanConfig.getPackages();
            boolean hasEntitiesOnClassPath = false;
            boolean hasIntrospections = false;
            if (isClasspathScanEnabled) {
                final Environment environment = ((ApplicationContext) beanContext).getEnvironment();
                if (ArrayUtils.isNotEmpty(packagesToScan)) {
                    hasEntitiesOnClassPath = environment.scan(Entity.class, packagesToScan).findAny().isPresent();
                } else {
                    hasEntitiesOnClassPath = environment.scan(Entity.class).findAny().isPresent();
                }
            } else {
                if (ArrayUtils.isNotEmpty(packagesToScan)) {
                    hasIntrospections = !BeanIntrospector.SHARED
                            .findIntrospections(Entity.class, packagesToScan).isEmpty();
                } else {
                    hasIntrospections = !BeanIntrospector.SHARED
                            .findIntrospections(Entity.class).isEmpty();
                }
            }

            return hasEntitiesOnClassPath || hasIntrospections;
        }
    }
    return true;
}
 
Example #3
Source File: AbstractPersistentEntity.java    From micronaut-data with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 * @param annotationMetadataProvider The annotation metadata provider.
 */
protected AbstractPersistentEntity(AnnotationMetadataProvider annotationMetadataProvider) {
    this.annotationMetadataProvider = annotationMetadataProvider;
    AnnotationMetadata annotationMetadata = annotationMetadataProvider.getAnnotationMetadata();
    this.namingStrategy = getNamingStrategy(annotationMetadata);
}