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

The following examples show how to use io.micronaut.core.reflect.ClassUtils. 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: JsonSerdeRegistry.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> Serde<T> getSerde(Class<T> type) {
    if (ClassUtils.isJavaBasicType(type)) {
        return (Serde<T>) Serdes.serdeFrom(String.class);
    } else {
        JsonSerde jsonSerde = serdes.get(type);
        if (jsonSerde != null) {
            return jsonSerde;
        } else {
            jsonSerde = beanContext.createBean(JsonSerde.class, type);
            serdes.put(type, jsonSerde);
            return jsonSerde;
        }
    }
}
 
Example 2
Source Project: micronaut-aws   Source File: AnnotatedRequestHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
default boolean canHandle(HandlerInput handlerInput) {
    final Class<? extends AnnotatedRequestHandler> type = getClass();
    final String annotationMetadata = type.getPackage().getName() + ".$" + type.getSimpleName() + "DefinitionClass";
    final AnnotationMetadata metadata = ClassUtils.forName(annotationMetadata, type.getClassLoader()).flatMap(aClass -> {
        final Object o = InstantiationUtils.tryInstantiate(aClass).orElse(null);
        if (o instanceof AnnotationMetadataProvider) {
            return Optional.of(((AnnotationMetadataProvider) o).getAnnotationMetadata());
        }
        return Optional.empty();
    }).orElse(AnnotationMetadata.EMPTY_METADATA);
    final String[] names = metadata.getValue(IntentHandler.class, String[].class).orElse(StringUtils.EMPTY_STRING_ARRAY);
    return Arrays.stream(names).anyMatch(n -> handlerInput.matches(intentName(n)));
}
 
Example 3
/**
 *
 * @param handler handler in format file.method, where file is the name of the file without an extension, and method is the name of a method or function that's defined in the file.
 * @return Empty or an Optional with the referenced class.
 */
protected Optional<Class> parseHandlerClass(@NonNull String handler) {
    String[] arr = handler.split("::");
    if (arr.length > 0) {
        return ClassUtils.forName(arr[0], null);
    }
    return Optional.empty();
}
 
Example 4
Source Project: micronaut-sql   Source File: JdbcDatabaseManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Searches the provided classloader for an embedded database driver.
 *
 * @param classLoader The classloader to search
 * @return An optional {@link EmbeddedJdbcDatabase}
 */
public static Optional<EmbeddedJdbcDatabase> get(ClassLoader classLoader) {
    return databases
        .stream()
        .filter(JdbcDatabase::isEmbedded)
        .map(EmbeddedJdbcDatabase.class::cast)
        .filter(embeddedDatabase -> ClassUtils.isPresent(embeddedDatabase.getDriverClassName(), classLoader))
        .findFirst();
}
 
Example 5
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 6
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 7
Source Project: micronaut-data   Source File: FindByFinder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Is the return type compatible.
 * @param methodElement The method element
 * @param matchContext The match context
 * @return The return type
 */
protected boolean isCompatibleReturnType(@NonNull MethodElement methodElement, @NonNull MatchContext matchContext) {
    ClassElement returnType = methodElement.getGenericReturnType();
    if (!returnType.getName().equals("void")) {
        return returnType.hasStereotype(Introspected.class) ||
                returnType.isPrimitive() ||
                ClassUtils.isJavaBasicType(returnType.getName()) ||
                TypeUtils.isContainerType(returnType);
    }
    return false;
}
 
Example 8
Source Project: micronaut-sql   Source File: JdbiFactory.java    License: Apache License 2.0 4 votes vote down vote up
private boolean h2IsPresent() {
    return ClassUtils.isPresent("org.h2.Driver", this.getClass().getClassLoader());
}
 
Example 9
Source Project: micronaut-sql   Source File: HibernatePresenceCondition.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context) {
    BeanContext beanContext = context.getBeanContext();
    return !ClassUtils.isPresent("io.micronaut.configuration.hibernate.jpa.HibernateTransactionManagerFactory", beanContext.getClassLoader());
}
 
Example 10
Source Project: micronaut-sql   Source File: CalculatedSettings.java    License: Apache License 2.0 4 votes vote down vote up
private boolean driverClassIsPresent(String className) {
    return ClassUtils.isPresent(className, this.getClass().getClassLoader());
}
 
Example 11
Source Project: micronaut-data   Source File: SaveOneMethod.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isRequiredProperty(SourcePersistentProperty pp) {
    return pp.isRequired() &&
            !ClassUtils.getPrimitiveType(pp.getTypeName()).isPresent();
}
 
Example 12
Source Project: micronaut-data   Source File: AbstractPatternBasedMethod.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isValidResultType(ClassElement returnType) {
    return returnType.hasStereotype(Introspected.class) || ClassUtils.isJavaBasicType(returnType.getName()) || returnType.isPrimitive();
}
 
Example 13
Source Project: micronaut-test   Source File: AbstractMicronautExtension.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * @param requiredTestClass The test class
 * @return true if the te given class has a bean definition class in the classpath (ie: the annotation processor has been run correctly)
 */
protected boolean isTestSuiteBeanPresent(Class<?> requiredTestClass) {
    return ClassUtils.isPresent(requiredTestClass.getPackage().getName() + ".$" + requiredTestClass.getSimpleName() + "Definition", requiredTestClass.getClassLoader());
}