Search in sources :

Example 1 with ParameterAttribute

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

Aggregations

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 MappingConstructor (com.blazebit.persistence.view.metamodel.MappingConstructor)1 ParameterAttribute (com.blazebit.persistence.view.metamodel.ParameterAttribute)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 CtClass (javassist.CtClass)1 CtMethod (javassist.CtMethod)1 ConstPool (javassist.bytecode.ConstPool)1 MethodInfo (javassist.bytecode.MethodInfo)1