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());
}
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());
}
}
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);
}
}
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)));
}
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());
}
Aggregations