use of io.micronaut.core.beans.exceptions.IntrospectionException in project kestra by kestra-io.
the class KestraValidator method findIntrospection.
private <T> Optional<BeanIntrospection<T>> findIntrospection(@NonNull Class<T> beanType) {
ArgumentUtils.requireNonNull("beanType", beanType);
BeanIntrospectionReference reference = this.getIntrospections().get(beanType.getName());
try {
if (reference != null) {
return Optional.of(reference).map((ref) -> {
if (log.isDebugEnabled()) {
log.debug("Found BeanIntrospection for type: " + ref.getBeanType());
}
return ref.load();
});
} else {
if (log.isDebugEnabled()) {
log.debug("No BeanIntrospection found for bean type: " + beanType);
}
return Optional.empty();
}
} catch (Throwable e) {
throw new IntrospectionException("Error loading BeanIntrospection for type [" + beanType + "]: " + e.getMessage(), e);
}
}
use of io.micronaut.core.beans.exceptions.IntrospectionException in project micronaut-data by micronaut-projects.
the class BeanIntrospectionMapper method map.
@Override
@NonNull
default R map(@NonNull D object, @NonNull Class<R> type) throws InstantiationException {
ArgumentUtils.requireNonNull("resultSet", object);
ArgumentUtils.requireNonNull("type", type);
try {
BeanIntrospection<R> introspection = BeanIntrospection.getIntrospection(type);
Argument<?>[] arguments = introspection.getConstructorArguments();
R instance;
if (ArrayUtils.isEmpty(arguments)) {
instance = introspection.instantiate();
} else {
Object[] args = new Object[arguments.length];
for (int i = 0; i < arguments.length; i++) {
Argument<?> argument = arguments[i];
Object o = read(object, argument);
if (o == null) {
args[i] = o;
} else {
if (argument.getType().isInstance(o)) {
args[i] = o;
} else {
Object convertFrom;
if (Collection.class.isAssignableFrom(argument.getType()) && !(o instanceof Collection)) {
convertFrom = Collections.singleton(o);
} else {
convertFrom = o;
}
args[i] = convert(convertFrom, argument);
}
}
}
instance = introspection.instantiate(args);
}
Collection<BeanProperty<R, Object>> properties = introspection.getBeanProperties();
for (BeanProperty<R, Object> property : properties) {
if (property.isReadOnly()) {
continue;
}
Object v = read(object, property.getName());
if (v != null) {
if (property.getType().isInstance(v)) {
property.set(instance, v);
} else if (Iterable.class.isAssignableFrom(property.getType())) {
Object value = property.get(instance);
if (value instanceof Collection) {
((Collection) value).add(v);
} else if (value instanceof Iterable) {
List list = new ArrayList(CollectionUtils.iterableToList((Iterable) value));
list.add(v);
property.set(instance, convert(list, property.asArgument()));
} else {
property.set(instance, convert(Collections.singleton(v), property.asArgument()));
}
} else {
property.set(instance, convert(v, property.asArgument()));
}
}
}
return instance;
} catch (IntrospectionException | InstantiationException e) {
throw new DataAccessException("Error instantiating type [" + type.getName() + "] from introspection: " + e.getMessage(), e);
}
}
use of io.micronaut.core.beans.exceptions.IntrospectionException in project micronaut-data by micronaut-projects.
the class AbstractQueryInterceptor method storeInParameterValues.
private <RT> void storeInParameterValues(MethodInvocationContext<T, R> context, StoredQuery<?, RT> storedQuery, Map<String, Object> namedValues, Object index, String argument, Map parameterValues) {
if (namedValues.containsKey(argument)) {
parameterValues.put(index, namedValues.get(argument));
} else {
int i = argument.indexOf('.');
if (i > -1) {
String argumentName = argument.substring(0, i);
Object o = namedValues.get(argumentName);
if (o != null) {
try {
BeanWrapper<Object> wrapper = BeanWrapper.getWrapper(o);
String prop = argument.substring(i + 1);
Object val = wrapper.getRequiredProperty(prop, Object.class);
parameterValues.put(index, val);
} catch (IntrospectionException e) {
throw new DataAccessException("Embedded value [" + o + "] should be annotated with introspected");
}
}
} else {
for (Argument a : context.getArguments()) {
String n = a.getAnnotationMetadata().stringValue(Parameter.class).orElse(a.getName());
if (n.equals(argument)) {
parameterValues.put(index, namedValues.get(a.getName()));
return;
}
}
throw new IllegalArgumentException("Missing query arguments: " + argument);
}
}
}
use of io.micronaut.core.beans.exceptions.IntrospectionException in project micronaut-serialization by micronaut-projects.
the class ObjectSerializer method createSpecific.
@Override
public Serializer<Object> createSpecific(EncoderContext encoderContext, Argument<? extends Object> type) {
if (type.equalsType(Argument.OBJECT_ARGUMENT)) {
// dynamic type resolving
return new RuntimeTypeSerializer(encoderContext);
} else {
SerBean<Object> serBean;
try {
serBean = getSerBean(type, encoderContext).get();
} catch (IntrospectionException e) {
return createRuntimeSerializer(encoderContext, type, e);
}
final AnnotationMetadata annotationMetadata = type.getAnnotationMetadata();
if (serBean.hasJsonValue()) {
return new Serializer<Object>() {
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) throws IOException {
serBean.initialize(context);
SerBean.SerProperty<Object, Object> jsonValue = serBean.jsonValue;
final Object v = jsonValue.get(value);
serBean.jsonValue.serializer.serialize(encoder, context, jsonValue.argument, v);
}
@Override
public boolean isEmpty(EncoderContext context, Object value) {
try {
serBean.initialize(context);
} catch (SerdeException e) {
throw new RuntimeException(e);
}
return serBean.jsonValue.serializer.isEmpty(context, serBean.jsonValue.get(value));
}
@Override
public boolean isAbsent(EncoderContext context, Object value) {
try {
serBean.initialize(context);
} catch (SerdeException e) {
throw new RuntimeException(e);
}
return serBean.jsonValue.serializer.isAbsent(context, serBean.jsonValue.get(value));
}
};
} else if (annotationMetadata.isAnnotationPresent(SerdeConfig.SerIgnored.class) || annotationMetadata.isAnnotationPresent(SerdeConfig.META_ANNOTATION_PROPERTY_ORDER) || annotationMetadata.isAnnotationPresent(SerdeConfig.SerIncluded.class)) {
final String[] ignored = annotationMetadata.stringValues(SerdeConfig.SerIgnored.class);
final String[] included = annotationMetadata.stringValues(SerdeConfig.SerIncluded.class);
List<String> order = Arrays.asList(annotationMetadata.stringValues(SerdeConfig.META_ANNOTATION_PROPERTY_ORDER));
final boolean hasIgnored = ArrayUtils.isNotEmpty(ignored);
final boolean hasIncluded = ArrayUtils.isNotEmpty(included);
Set<String> ignoreSet = hasIgnored ? CollectionUtils.setOf(ignored) : null;
Set<String> includedSet = hasIncluded ? CollectionUtils.setOf(included) : null;
if (!order.isEmpty() || hasIgnored || hasIncluded) {
return new CustomizedObjectSerializer<Object>(serBean) {
@Override
protected List<SerBean.SerProperty<Object, Object>> getWriteProperties(SerBean<Object> serBean) {
final List<SerBean.SerProperty<Object, Object>> writeProperties = new ArrayList<>(super.getWriteProperties(serBean));
if (!order.isEmpty()) {
writeProperties.sort(Comparator.comparingInt(o -> order.indexOf(o.name)));
}
if (hasIgnored) {
writeProperties.removeIf(p -> ignoreSet.contains(p.name));
}
if (hasIncluded) {
writeProperties.removeIf(p -> !includedSet.contains(p.name));
}
return writeProperties;
}
};
}
}
Serializer<Object> outer;
if (serBean.simpleBean) {
outer = new SimpleObjectSerializer<>(serBean);
} else {
outer = new CustomizedObjectSerializer<>(serBean);
}
if (serBean.subtyped) {
return new RuntimeTypeSerializer(encoderContext) {
@Override
protected Serializer<Object> tryToFindSerializer(EncoderContext context, Object value) throws SerdeException {
if (value.getClass().equals(type.getType())) {
return outer;
} else {
return super.tryToFindSerializer(context, value);
}
}
};
} else {
return outer;
}
}
}
use of io.micronaut.core.beans.exceptions.IntrospectionException in project micronaut-serialization by micronaut-projects.
the class SimpleObjectSerializer method serialize.
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends T> type, T value) throws IOException {
try {
serBean.initialize(context);
Encoder childEncoder = encoder.encodeObject(type);
for (SerBean.SerProperty<Object, Object> property : serBean.writeProperties) {
childEncoder.encodeKey(property.name);
Object v = property.get(value);
if (v == null) {
childEncoder.encodeNull();
} else {
property.serializer.serialize(childEncoder, context, property.argument, v);
}
}
childEncoder.finishStructure();
} catch (StackOverflowError e) {
throw new SerdeException("Infinite recursion serializing type: " + type.getType().getSimpleName() + " at path " + encoder.currentPath(), e);
} catch (IntrospectionException e) {
throw new SerdeException("Error serializing value at path: " + encoder.currentPath() + ". No serializer found for " + "type: " + type, e);
}
}
Aggregations