Search in sources :

Example 1 with IntrospectionException

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);
    }
}
Also used : BeanIntrospectionReference(io.micronaut.core.beans.BeanIntrospectionReference) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException)

Example 2 with IntrospectionException

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);
    }
}
Also used : Argument(io.micronaut.core.type.Argument) ArrayList(java.util.ArrayList) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException) BeanProperty(io.micronaut.core.beans.BeanProperty) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) DataAccessException(io.micronaut.data.exceptions.DataAccessException) InstantiationException(io.micronaut.core.reflect.exception.InstantiationException) NonNull(io.micronaut.core.annotation.NonNull)

Example 3 with IntrospectionException

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);
        }
    }
}
Also used : Argument(io.micronaut.core.type.Argument) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException) Parameter(io.micronaut.context.annotation.Parameter) DataAccessException(io.micronaut.data.exceptions.DataAccessException)

Example 4 with IntrospectionException

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;
        }
    }
}
Also used : CustomizableSerializer(io.micronaut.serde.util.CustomizableSerializer) Arrays(java.util.Arrays) ArrayUtils(io.micronaut.core.util.ArrayUtils) TypeKey(io.micronaut.serde.support.util.TypeKey) Internal(io.micronaut.core.annotation.Internal) Encoder(io.micronaut.serde.Encoder) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Primary(io.micronaut.context.annotation.Primary) Serializer(io.micronaut.serde.Serializer) SerializationConfiguration(io.micronaut.serde.config.SerializationConfiguration) SerdeConfig(io.micronaut.serde.config.annotation.SerdeConfig) Map(java.util.Map) SerdeIntrospections(io.micronaut.serde.SerdeIntrospections) Argument(io.micronaut.core.type.Argument) SupplierUtil(io.micronaut.core.util.SupplierUtil) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Singleton(jakarta.inject.Singleton) Set(java.util.Set) IOException(java.io.IOException) List(java.util.List) CollectionUtils(io.micronaut.core.util.CollectionUtils) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Comparator(java.util.Comparator) SerdeException(io.micronaut.serde.exceptions.SerdeException) Set(java.util.Set) Argument(io.micronaut.core.type.Argument) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException) SerdeException(io.micronaut.serde.exceptions.SerdeException) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Encoder(io.micronaut.serde.Encoder) ArrayList(java.util.ArrayList) List(java.util.List) CustomizableSerializer(io.micronaut.serde.util.CustomizableSerializer) Serializer(io.micronaut.serde.Serializer) SerdeConfig(io.micronaut.serde.config.annotation.SerdeConfig)

Example 5 with IntrospectionException

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);
    }
}
Also used : Encoder(io.micronaut.serde.Encoder) SerdeException(io.micronaut.serde.exceptions.SerdeException) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException)

Aggregations

IntrospectionException (io.micronaut.core.beans.exceptions.IntrospectionException)13 Argument (io.micronaut.core.type.Argument)7 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)4 SerdeConfig (io.micronaut.serde.config.annotation.SerdeConfig)4 SerdeException (io.micronaut.serde.exceptions.SerdeException)4 NonNull (io.micronaut.core.annotation.NonNull)3 BeanProperty (io.micronaut.core.beans.BeanProperty)3 Encoder (io.micronaut.serde.Encoder)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 List (java.util.List)3 Map (java.util.Map)3 BeanIntrospector (io.micronaut.core.beans.BeanIntrospector)2 ArgumentUtils (io.micronaut.core.util.ArgumentUtils)2 DataAccessException (io.micronaut.data.exceptions.DataAccessException)2 Serializer (io.micronaut.serde.Serializer)2 Optional (java.util.Optional)2 Parameter (io.micronaut.context.annotation.Parameter)1 Primary (io.micronaut.context.annotation.Primary)1 AnnotationMetadataProvider (io.micronaut.core.annotation.AnnotationMetadataProvider)1