Java Code Examples for io.micronaut.core.reflect.ReflectionUtils

The following examples show how to use io.micronaut.core.reflect.ReflectionUtils. These examples are extracted from open source projects. 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 Project: micronaut-kafka   Source File: CompositeSerdeRegistry.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nonnull
public <T> Serde<T> getSerde(Class<T> type) {
    Serde serde = serdeMap.get(type);

    if (serde != null) {
       return serde;
    } else {
        type = ReflectionUtils.getWrapperType(type);
        try {
            return Serdes.serdeFrom(type);
        } catch (IllegalArgumentException e) {
            for (SerdeRegistry registry : registries) {
                serde = registry.getSerde(type);
                if (serde != null) {
                    serdeMap.put(type, serde);
                    return serde;
                }
            }
        }
        throw new SerializationException("No available serde for type: " + type);
    }
}
 
Example 2
Source Project: micronaut-data   Source File: DefaultUpdateInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, Object> context) {
    PreparedQuery<?, Number> preparedQuery = (PreparedQuery<?, Number>) prepareQuery(methodKey, context);
    Number number = operations.executeUpdate(preparedQuery).orElse(null);
    final Argument<Object> returnType = context.getReturnType().asArgument();
    final Class<Object> type = ReflectionUtils.getWrapperType(returnType.getType());
    if (Number.class.isAssignableFrom(type)) {
        if (type.isInstance(number)) {
            return number;
        } else {
            return ConversionService.SHARED.
                    convert(number, returnType)
                    .orElse(0);
        }
    } else if (Boolean.class.isAssignableFrom(type)) {
        return number == null || number.longValue() < 0;
    } else {
        return null;
    }
}
 
Example 3
Source Project: micronaut-data   Source File: TypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Is the type a number.
 * @param type The type
 * @return True if is a number
 */
public static boolean isNumber(@Nullable ClassElement type) {
    if (type == null) {
        return false;
    }
    if (type.isPrimitive()) {
        return ClassUtils.getPrimitiveType(type.getName()).map(aClass ->
                Number.class.isAssignableFrom(ReflectionUtils.getWrapperType(aClass))
        ).orElse(false);
    } else {
        return type.isAssignable(Number.class);
    }
}
 
Example 4
Source Project: micronaut-data   Source File: TypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the type for the given class element, wrapping primitives types if necessary.
 * @param type The type
 * @return The ID type
 */
public static @NonNull String getTypeName(@NonNull ClassElement type) {
    String typeName = type.getName();
    return ClassUtils.getPrimitiveType(typeName).map(t ->
        ReflectionUtils.getWrapperType(t).getName()
    ).orElse(typeName);
}
 
Example 5
Source Project: micronaut-data   Source File: RuntimePersistentProperty.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param owner The owner
 * @param property The property
 * @param constructorArg whether it is a constructor arg
 */
RuntimePersistentProperty(RuntimePersistentEntity<T> owner, BeanProperty<T, ?> property, boolean constructorArg) {
    this.owner = owner;
    this.property = property;
    this.type = ReflectionUtils.getWrapperType(property.getType());
    this.dataType = PersistentProperty.super.getDataType();
    this.constructorArg = constructorArg;
    this.argument = property.asArgument();
}
 
Example 6
Source Project: micronaut-data   Source File: ColumnNameResultSetReader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T> T convertRequired(Object value, Class<T> type) {
    Class wrapperType = ReflectionUtils.getWrapperType(type);
    if (wrapperType.isInstance(value)) {
        return (T) value;
    }
    return conversionService.convert(
            value,
            type
    ).orElseThrow(() ->
            new DataAccessException("Cannot convert type [" + value.getClass() + "] with value [" + value + "] to target type: " + type + ". Consider defining a TypeConverter bean to handle this case.")
    );
}
 
Example 7
Source Project: micronaut-test   Source File: AnnotationUtils.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <A extends Annotation> void findRepeatableAnnotations(Annotation[] candidates,
                                                                     Class<A> annotationType,
                                                                     Class<? extends Annotation> containerType,
                                                                     boolean inherited,
                                                                     Set<A> found,
                                                                     Set<Annotation> visited) {
    for (Annotation candidate : candidates) {
        Class<? extends Annotation> candidateAnnotationType = candidate.annotationType();
        if (!isInJavaLangAnnotationPackage(candidateAnnotationType) && visited.add(candidate)) {
            if (candidateAnnotationType.equals(annotationType)) { // Exact match?
                found.add(annotationType.cast(candidate));
            } else if (candidateAnnotationType.equals(containerType)) { // Container?
                // Note: it's not a legitimate containing annotation type if it doesn't declare
                // a 'value' attribute that returns an array of the contained annotation type.
                // See https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.6.3
                Method method = ReflectionUtils.getMethod(containerType, "value").orElseThrow(
                        () -> new IllegalStateException(String.format(
                                "Container annotation type '%s' must declare a 'value' attribute of type %s[].",
                                containerType, annotationType)));

                Annotation[] containedAnnotations = ReflectionUtils.invokeMethod(candidate, method);
                found.addAll((Collection<? extends A>) asList(containedAnnotations));
            } else { // Otherwise search recursively through the meta-annotation hierarchy...
                findRepeatableAnnotations(candidateAnnotationType, annotationType, containerType, inherited, found, visited);
            }
        }
    }
}
 
Example 8
Source Project: micronaut-data   Source File: StoredQuery.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * The argument types to the method that invokes the query.
 *
 * @return The argument types
 */
@NonNull
default Class<?>[] getArgumentTypes() {
    return ReflectionUtils.EMPTY_CLASS_ARRAY;
}