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;
}
}
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);
}
}
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);
}
Aggregations