use of com.blazebit.persistence.view.metamodel.ParameterAttribute 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);
}
}
Aggregations