Search in sources :

Example 1 with EntityViewBuilder

use of com.blazebit.persistence.view.EntityViewBuilder in project blaze-persistence by Blazebit.

the class EntityViewManagerImpl method initializeStaticBuilder.

private static void initializeStaticBuilder(Set<String> errors, ManagedViewType<?> managedView, Map<ViewBuilderKey, Constructor<? extends EntityViewBuilder<?>>> viewBuilderConstructors) {
    Class<?> javaType = managedView.getJavaType();
    Class<?> builderClass;
    try {
        builderClass = javaType.getClassLoader().loadClass(getBuilderClassName(javaType));
        StaticBuilder annotation = builderClass.getAnnotation(StaticBuilder.class);
        if (annotation != null) {
            if (annotation.value() != javaType) {
                errors.add("The static builder class '" + builderClass.getName() + "' was expected to be defined for the entity view type '" + javaType.getName() + "' but was defined for: " + annotation.value().getName());
                return;
            }
        }
    } catch (ClassNotFoundException e) {
        // Ignore
        return;
    }
    try {
        int size = managedView.getConstructors().size();
        if (size == 0) {
            Class<?> constructorClass = javaType.getClassLoader().loadClass(builderClass.getName() + "$Init");
            viewBuilderConstructors.put(new ViewBuilderKey(managedView, null), (Constructor<? extends EntityViewBuilder<?>>) constructorClass.getDeclaredConstructor(Map.class));
        } else if (size == 1) {
            MappingConstructor<?> constructor = managedView.getConstructors().iterator().next();
            Class<?> constructorClass = javaType.getClassLoader().loadClass(builderClass.getName() + "$" + Character.toUpperCase(constructor.getName().charAt(0)) + constructor.getName().substring(1));
            viewBuilderConstructors.put(new ViewBuilderKey(managedView, null), (Constructor<? extends EntityViewBuilder<?>>) constructorClass.getDeclaredConstructor(Map.class));
            viewBuilderConstructors.put(new ViewBuilderKey(managedView, constructor), (Constructor<? extends EntityViewBuilder<?>>) constructorClass.getDeclaredConstructor(Map.class));
        } else {
            for (MappingConstructor<?> constructor : managedView.getConstructors()) {
                Class<?> constructorClass = javaType.getClassLoader().loadClass(builderClass.getName() + "$" + Character.toUpperCase(constructor.getName().charAt(0)) + constructor.getName().substring(1));
                viewBuilderConstructors.put(new ViewBuilderKey(managedView, constructor), (Constructor<? extends EntityViewBuilder<?>>) constructorClass.getDeclaredConstructor(Map.class));
            }
        }
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        errors.add("The initialization of the static builder class '" + builderClass.getName() + "' failed: " + sw.toString());
    }
}
Also used : MappingConstructor(com.blazebit.persistence.view.metamodel.MappingConstructor) StaticBuilder(com.blazebit.persistence.view.StaticBuilder) Constructor(java.lang.reflect.Constructor) MappingConstructor(com.blazebit.persistence.view.metamodel.MappingConstructor) StringWriter(java.io.StringWriter) EntityViewBuilder(com.blazebit.persistence.view.EntityViewBuilder) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) PrintWriter(java.io.PrintWriter)

Aggregations

EntityViewBuilder (com.blazebit.persistence.view.EntityViewBuilder)1 StaticBuilder (com.blazebit.persistence.view.StaticBuilder)1 MappingConstructor (com.blazebit.persistence.view.metamodel.MappingConstructor)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Constructor (java.lang.reflect.Constructor)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1