Search in sources :

Example 1 with BeanProperty

use of io.micronaut.core.beans.BeanProperty in project micronaut-core by micronaut-projects.

the class RequestBeanAnnotationBinder method bind.

@Override
public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<?> source) {
    Argument<T> argument = context.getArgument();
    AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata();
    boolean hasAnnotation = annotationMetadata.hasAnnotation(RequestBean.class);
    if (hasAnnotation) {
        BeanIntrospection<T> introspection = BeanIntrospection.getIntrospection(context.getArgument().getType());
        Map<String, BeanProperty<T, Object>> beanProperties = introspection.getBeanProperties().stream().collect(Collectors.toMap(Named::getName, p -> p));
        if (introspection.getConstructorArguments().length > 0) {
            // Handle injection with Constructor or @Creator
            Argument<?>[] constructorArguments = introspection.getConstructorArguments();
            Object[] argumentValues = new Object[constructorArguments.length];
            for (int i = 0; i < constructorArguments.length; i++) {
                @SuppressWarnings("unchecked") Argument<Object> constructorArgument = (Argument<Object>) constructorArguments[i];
                BeanProperty<T, Object> bp = beanProperties.get(constructorArgument.getName());
                Argument<Object> argumentToBind;
                if (bp != null) {
                    argumentToBind = bp.asArgument();
                } else {
                    argumentToBind = constructorArgument;
                }
                Optional<Object> bindableResult = getBindableResult(source, argumentToBind);
                argumentValues[i] = constructorArgument.isOptional() ? bindableResult : bindableResult.orElse(null);
            }
            return () -> Optional.of(introspection.instantiate(false, argumentValues));
        } else {
            // Handle injection with setters, we checked that all values are writable at compile time
            T bean = introspection.instantiate();
            for (BeanProperty<T, Object> property : beanProperties.values()) {
                Argument<Object> propertyArgument = property.asArgument();
                Optional<Object> bindableResult = getBindableResult(source, propertyArgument);
                property.set(bean, propertyArgument.isOptional() ? bindableResult : bindableResult.orElse(null));
            }
            return () -> Optional.of(bean);
        }
    } else {
        // noinspection unchecked
        return BindingResult.EMPTY;
    }
}
Also used : java.util(java.util) BeanProperty(io.micronaut.core.beans.BeanProperty) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException) Collectors(java.util.stream.Collectors) ArgumentBinder(io.micronaut.core.bind.ArgumentBinder) UnsatisfiedArgumentException(io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException) AbstractAnnotatedArgumentBinder(io.micronaut.core.bind.annotation.AbstractAnnotatedArgumentBinder) ConversionContext(io.micronaut.core.convert.ConversionContext) ConversionError(io.micronaut.core.convert.ConversionError) Named(io.micronaut.core.naming.Named) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) RequestBean(io.micronaut.http.annotation.RequestBean) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Argument(io.micronaut.core.type.Argument) HttpRequest(io.micronaut.http.HttpRequest) ConversionService(io.micronaut.core.convert.ConversionService) RequestBinderRegistry(io.micronaut.http.bind.RequestBinderRegistry) Argument(io.micronaut.core.type.Argument) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) BeanProperty(io.micronaut.core.beans.BeanProperty)

Example 2 with BeanProperty

use of io.micronaut.core.beans.BeanProperty in project micronaut-core by micronaut-projects.

the class DefaultValidator method cascadeToOne.

private <T> void cascadeToOne(@NonNull Class<T> rootClass, @Nullable T rootBean, Object object, DefaultConstraintValidatorContext context, Set overallViolations, AnnotatedElement cascadeProperty, Class propertyType, Object propertyValue, @Nullable DefaultPropertyNode container) {
    Class<?> beanType = Object.class;
    if (propertyValue != null) {
        beanType = propertyValue.getClass();
    } else if (cascadeProperty instanceof BeanProperty) {
        Argument argument = ((BeanProperty) cascadeProperty).asArgument();
        if (Map.class.isAssignableFrom(argument.getType())) {
            Argument[] typeParameters = argument.getTypeParameters();
            if (typeParameters.length == 2) {
                beanType = typeParameters[1].getType();
            }
        } else {
            beanType = argument.getFirstTypeVariable().map(Argument::getType).orElse(null);
        }
    }
    final BeanIntrospection<Object> beanIntrospection = getBeanIntrospection(beanType);
    AnnotationMetadata annotationMetadata = cascadeProperty.getAnnotationMetadata();
    if (beanIntrospection == null && !annotationMetadata.hasStereotype(Constraint.class)) {
        // error: only has @Valid but the propertyValue class is not @Introspected
        overallViolations.add(createIntrospectionConstraintViolation(rootClass, rootBean, context, beanType, propertyValue));
        return;
    }
    if (beanIntrospection != null) {
        if (container != null) {
            context.addPropertyNode(container.getName(), container);
        }
        try {
            cascadeToOneIntrospection(context, rootBean, propertyValue, beanIntrospection, overallViolations);
        } finally {
            if (container != null) {
                context.removeLast();
            }
        }
    } else {
        // try apply cascade rules to actual property
        // noinspection unchecked
        validateConstrainedPropertyInternal(rootClass, rootBean, object, cascadeProperty, propertyType, propertyValue, context, overallViolations, container);
    }
}
Also used : Argument(io.micronaut.core.type.Argument) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) BeanProperty(io.micronaut.core.beans.BeanProperty)

Example 3 with BeanProperty

use of io.micronaut.core.beans.BeanProperty in project micronaut-sql by micronaut-projects.

the class IntrospectedHibernateBytecodeProvider method getReflectionOptimizer.

@Override
public ReflectionOptimizer getReflectionOptimizer(Class clazz, String[] getterNames, String[] setterNames, Class[] types) {
    Optional<BeanIntrospection<?>> optionalBeanIntrospection = BeanIntrospector.SHARED.findIntrospection(clazz);
    return optionalBeanIntrospection.map(beanIntrospection -> new ReflectionOptimizer() {

        @Override
        public InstantiationOptimizer getInstantiationOptimizer() {
            return beanIntrospection::instantiate;
        }

        @Override
        public AccessOptimizer getAccessOptimizer() {
            BeanProperty[] beanProperties = beanIntrospection.getBeanProperties().toArray(new BeanProperty[0]);
            return new AccessOptimizer() {

                private final String[] propertyNames = Arrays.stream(beanProperties).map(BeanProperty::getName).toArray(String[]::new);

                @Override
                public String[] getPropertyNames() {
                    return propertyNames;
                }

                @Override
                public Object[] getPropertyValues(Object object) {
                    Object[] values = new Object[beanProperties.length];
                    for (int i = 0; i < beanProperties.length; i++) {
                        BeanProperty beanProperty = beanProperties[i];
                        values[i] = beanProperty.get(i);
                    }
                    return values;
                }

                @Override
                public void setPropertyValues(Object object, Object[] values) {
                    for (int i = 0; i < beanProperties.length; i++) {
                        BeanProperty beanProperty = beanProperties[i];
                        beanProperty.set(object, values[i]);
                    }
                }
            };
        }
    }).orElse(null);
}
Also used : ProxyFactoryFactory(org.hibernate.bytecode.spi.ProxyFactoryFactory) EnhancementContext(org.hibernate.bytecode.enhance.spi.EnhancementContext) BytecodeProvider(org.hibernate.bytecode.spi.BytecodeProvider) Arrays(java.util.Arrays) Enhancer(org.hibernate.bytecode.enhance.spi.Enhancer) BeanProperty(io.micronaut.core.beans.BeanProperty) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) Singleton(jakarta.inject.Singleton) ReflectionOptimizer(org.hibernate.bytecode.spi.ReflectionOptimizer) Optional(java.util.Optional) Internal(io.micronaut.core.annotation.Internal) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) ReflectionOptimizer(org.hibernate.bytecode.spi.ReflectionOptimizer) BeanProperty(io.micronaut.core.beans.BeanProperty)

Aggregations

BeanProperty (io.micronaut.core.beans.BeanProperty)3 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)2 Argument (io.micronaut.core.type.Argument)2 Internal (io.micronaut.core.annotation.Internal)1 BeanIntrospector (io.micronaut.core.beans.BeanIntrospector)1 ArgumentBinder (io.micronaut.core.bind.ArgumentBinder)1 AbstractAnnotatedArgumentBinder (io.micronaut.core.bind.annotation.AbstractAnnotatedArgumentBinder)1 UnsatisfiedArgumentException (io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionContext (io.micronaut.core.convert.ConversionContext)1 ConversionError (io.micronaut.core.convert.ConversionError)1 ConversionService (io.micronaut.core.convert.ConversionService)1 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)1 Named (io.micronaut.core.naming.Named)1 HttpRequest (io.micronaut.http.HttpRequest)1 RequestBean (io.micronaut.http.annotation.RequestBean)1 RequestBinderRegistry (io.micronaut.http.bind.RequestBinderRegistry)1 Singleton (jakarta.inject.Singleton)1 java.util (java.util)1