Search in sources :

Example 1 with MappingConstructor

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

the class ViewMetamodelTest method testMappingConstructor.

@Test
public void testMappingConstructor() throws Exception {
    ViewMetamodel viewMetamodel = getViewMetamodel();
    ViewType<DocumentViewAbstractClass> viewType = viewMetamodel.view(DocumentViewAbstractClass.class);
    Set<MappingConstructor<DocumentViewAbstractClass>> constructors = viewType.getConstructors();
    MappingConstructor<DocumentViewAbstractClass> constructor = constructors.iterator().next();
    assertNotNull(constructor);
    assertEquals(3, constructor.getParameterAttributes().size());
    assertEquals(Long.class, constructor.getParameterAttributes().get(0).getJavaType());
    assertEquals(constructor, constructor.getParameterAttributes().get(0).getDeclaringConstructor());
    assertEquals(viewType, constructor.getParameterAttributes().get(0).getDeclaringType());
    assertEquals(0, constructor.getParameterAttributes().get(0).getIndex());
    assertFalse(constructor.getParameterAttributes().get(0).isSubquery());
    assertEquals("age + 1", ((MappingAttribute<?, ?>) constructor.getParameterAttributes().get(0)).getMapping());
    assertFalse(constructor.getParameterAttributes().get(0).isCollection());
    assertFalse(((SingularAttribute<?, ?>) constructor.getParameterAttributes().get(0)).isQueryParameter());
    assertEquals(Integer.class, constructor.getParameterAttributes().get(1).getJavaType());
    assertEquals(constructor, constructor.getParameterAttributes().get(1).getDeclaringConstructor());
    assertEquals(viewType, constructor.getParameterAttributes().get(1).getDeclaringType());
    assertEquals(1, constructor.getParameterAttributes().get(1).getIndex());
    assertFalse(constructor.getParameterAttributes().get(1).isSubquery());
    assertEquals("contactPersonNumber", ((MappingAttribute<?, ?>) constructor.getParameterAttributes().get(1)).getMapping());
    assertFalse(constructor.getParameterAttributes().get(1).isCollection());
    assertTrue(((SingularAttribute<?, ?>) constructor.getParameterAttributes().get(1)).isQueryParameter());
    assertEquals(String.class, constructor.getParameterAttributes().get(2).getJavaType());
    assertEquals(constructor, constructor.getParameterAttributes().get(2).getDeclaringConstructor());
    assertEquals(viewType, constructor.getParameterAttributes().get(2).getDeclaringType());
    assertEquals(2, constructor.getParameterAttributes().get(2).getIndex());
    assertFalse(constructor.getParameterAttributes().get(2).isSubquery());
    assertEquals("optionalParameter", ((MappingAttribute<?, ?>) constructor.getParameterAttributes().get(2)).getMapping());
    assertFalse(constructor.getParameterAttributes().get(2).isCollection());
    assertTrue(((SingularAttribute<?, ?>) constructor.getParameterAttributes().get(2)).isQueryParameter());
    assertEquals(DocumentViewAbstractClass.class.getConstructor(Long.class, Integer.class, String.class), constructor.getJavaConstructor());
    assertEquals(viewType, constructor.getDeclaringType());
}
Also used : MappingConstructor(com.blazebit.persistence.view.metamodel.MappingConstructor) ViewMetamodel(com.blazebit.persistence.view.metamodel.ViewMetamodel) DocumentViewAbstractClass(com.blazebit.persistence.view.testsuite.basic.model.DocumentViewAbstractClass) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Example 2 with MappingConstructor

use of com.blazebit.persistence.view.metamodel.MappingConstructor 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)

Example 3 with MappingConstructor

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

the class ProxyFactory method createSerializationSubclass.

private void createSerializationSubclass(ManagedViewTypeImplementor<?> managedViewType, CtClass cc) throws Exception {
    boolean hasSelfConstructor = false;
    OUTER: for (MappingConstructor<?> constructor : managedViewType.getConstructors()) {
        for (ParameterAttribute<?, ?> parameterAttribute : constructor.getParameterAttributes()) {
            if (parameterAttribute.isSelfParameter()) {
                hasSelfConstructor = true;
                break OUTER;
            }
        }
    }
    if (hasSelfConstructor) {
        String serializableClassName = cc.getName() + "Ser";
        Set<AbstractMethodAttribute<?, ?>> attributes = (Set<AbstractMethodAttribute<?, ?>>) (Set<?>) managedViewType.getAttributes();
        CtClass[] attributeTypes = new CtClass[attributes.size()];
        createSerializableClass(managedViewType, cc, serializableClassName, attributes, attributeTypes);
        ConstPool cp = cc.getClassFile().getConstPool();
        MethodInfo minfo = new MethodInfo(cp, "createSelf", Descriptor.ofMethod(pool.get(managedViewType.getJavaType().getName()), attributeTypes));
        CtMethod method = CtMethod.make(minfo, cc);
        minfo.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC);
        StringBuilder sb = new StringBuilder();
        sb.append("{\n");
        sb.append("\t").append(serializableClassName).append(" self = (").append(serializableClassName).append(") new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(");
        sb.append(serializableClassName).append(".EMPTY_INSTANCE_BYTES)).readObject();\n");
        int index = 1;
        for (MethodAttribute<?, ?> attribute : attributes) {
            sb.append("\tself.").append(attribute.getName()).append(" = $").append(index).append(";\n");
            index++;
        }
        sb.append("\treturn self;\n");
        sb.append("}");
        method.setBody(sb.toString());
        cc.addMethod(method);
    }
}
Also used : ParameterAttribute(com.blazebit.persistence.view.metamodel.ParameterAttribute) ConstPool(javassist.bytecode.ConstPool) RecordingNavigableSet(com.blazebit.persistence.view.impl.collection.RecordingNavigableSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) RecordingSet(com.blazebit.persistence.view.impl.collection.RecordingSet) MappingConstructor(com.blazebit.persistence.view.metamodel.MappingConstructor) CtClass(javassist.CtClass) AbstractMethodAttribute(com.blazebit.persistence.view.impl.metamodel.AbstractMethodAttribute) MethodInfo(javassist.bytecode.MethodInfo) CtMethod(javassist.CtMethod)

Example 4 with MappingConstructor

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

the class ViewMetamodelTest method testGetConstructorsClassView.

@Test
public void testGetConstructorsClassView() {
    ViewMetamodel viewMetamodel = getViewMetamodel();
    ViewType<DocumentViewAbstractClass> viewType = viewMetamodel.view(DocumentViewAbstractClass.class);
    Set<MappingConstructor<DocumentViewAbstractClass>> constructors = viewType.getConstructors();
    assertEquals(1, constructors.size());
    assertNotNull(viewType.getConstructor(Long.class, Integer.class, String.class));
    assertTrue(constructors.contains(viewType.getConstructor(Long.class, Integer.class, String.class)));
}
Also used : MappingConstructor(com.blazebit.persistence.view.metamodel.MappingConstructor) ViewMetamodel(com.blazebit.persistence.view.metamodel.ViewMetamodel) DocumentViewAbstractClass(com.blazebit.persistence.view.testsuite.basic.model.DocumentViewAbstractClass) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Example 5 with MappingConstructor

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

the class ViewMetamodelTest method testGetConstructorsInterfaceView.

@Test
public void testGetConstructorsInterfaceView() {
    ViewMetamodel viewMetamodel = getViewMetamodel();
    ViewType<DocumentViewInterface> viewType = viewMetamodel.view(DocumentViewInterface.class);
    Set<MappingConstructor<DocumentViewInterface>> constructors = viewType.getConstructors();
    assertEquals(0, constructors.size());
}
Also used : DocumentViewInterface(com.blazebit.persistence.view.testsuite.basic.model.DocumentViewInterface) MappingConstructor(com.blazebit.persistence.view.metamodel.MappingConstructor) ViewMetamodel(com.blazebit.persistence.view.metamodel.ViewMetamodel) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Aggregations

MappingConstructor (com.blazebit.persistence.view.metamodel.MappingConstructor)5 ViewMetamodel (com.blazebit.persistence.view.metamodel.ViewMetamodel)3 AbstractEntityViewTest (com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)3 Test (org.junit.Test)3 DocumentViewAbstractClass (com.blazebit.persistence.view.testsuite.basic.model.DocumentViewAbstractClass)2 EntityViewBuilder (com.blazebit.persistence.view.EntityViewBuilder)1 StaticBuilder (com.blazebit.persistence.view.StaticBuilder)1 RecordingNavigableSet (com.blazebit.persistence.view.impl.collection.RecordingNavigableSet)1 RecordingSet (com.blazebit.persistence.view.impl.collection.RecordingSet)1 AbstractMethodAttribute (com.blazebit.persistence.view.impl.metamodel.AbstractMethodAttribute)1 ParameterAttribute (com.blazebit.persistence.view.metamodel.ParameterAttribute)1 DocumentViewInterface (com.blazebit.persistence.view.testsuite.basic.model.DocumentViewInterface)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Constructor (java.lang.reflect.Constructor)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1