Search in sources :

Example 1 with SetViaMediate

use of org.datanucleus.enhancer.methods.SetViaMediate in project datanucleus-core by datanucleus.

the class EnhancerClassAdapter method visitEnd.

/**
 * Method called at the end of the class.
 */
public void visitEnd() {
    AbstractClassMetaData cmd = enhancer.getClassMetaData();
    if (cmd.getPersistenceModifier() == ClassPersistenceModifier.PERSISTENCE_CAPABLE) {
        // Add any new fields
        List fields = enhancer.getFieldsList();
        Iterator fieldsIter = fields.iterator();
        while (fieldsIter.hasNext()) {
            ClassField field = (ClassField) fieldsIter.next();
            if (field.getName().equals(enhancer.getNamer().getDetachedStateFieldName()) && hasDetachedState) {
                // No need to add this field since exists
                continue;
            }
            if (DataNucleusEnhancer.LOGGER.isDebugEnabled()) {
                DataNucleusEnhancer.LOGGER.debug(Localiser.msg("005021", ((Class) field.getType()).getName() + " " + field.getName()));
            }
            cv.visitField(field.getAccess(), field.getName(), Type.getDescriptor((Class) field.getType()), null, null);
        }
        if (!hasStaticInitialisation) {
            // Add a static initialisation block for the class since nothing added yet
            InitClass method = InitClass.getInstance(enhancer);
            method.initialise(cv);
            method.execute();
            method.close();
        }
        if (!hasDefaultConstructor && enhancer.hasOption(ClassEnhancer.OPTION_GENERATE_DEFAULT_CONSTRUCTOR)) {
            // Add a default constructor
            DefaultConstructor ctr = DefaultConstructor.getInstance(enhancer);
            ctr.initialise(cv);
            ctr.execute();
            ctr.close();
        }
        // Add any new methods
        List methods = enhancer.getMethodsList();
        Iterator<ClassMethod> methodsIter = methods.iterator();
        while (methodsIter.hasNext()) {
            ClassMethod method = methodsIter.next();
            method.initialise(cv);
            method.execute();
            method.close();
        }
        if (Serializable.class.isAssignableFrom(enhancer.getClassBeingEnhanced())) {
            // Class is Serializable
            if (!hasSerialVersionUID) {
                // Needs "serialVersionUID" field
                Long uid = null;
                try {
                    uid = (Long) AccessController.doPrivileged(new PrivilegedAction() {

                        public Object run() {
                            return Long.valueOf(ObjectStreamClass.lookup(enhancer.getClassBeingEnhanced()).getSerialVersionUID());
                        }
                    });
                } catch (Throwable e) {
                    DataNucleusEnhancer.LOGGER.warn(StringUtils.getStringFromStackTrace(e));
                }
                ClassField cf = new ClassField(enhancer, enhancer.getNamer().getSerialVersionUidFieldName(), Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, long.class, uid);
                if (DataNucleusEnhancer.LOGGER.isDebugEnabled()) {
                    DataNucleusEnhancer.LOGGER.debug(Localiser.msg("005021", ((Class) cf.getType()).getName() + " " + cf.getName()));
                }
                cv.visitField(cf.getAccess(), cf.getName(), Type.getDescriptor((Class) cf.getType()), null, cf.getInitialValue());
            }
            // pc class that implements Serializable in the inheritance hierarchy needs to be modified or generated to call it.
            if (cmd.getSuperAbstractClassMetaData() == null && !hasWriteObject) {
                // User hasn't provided their own writeObject, so provide the default but with a call to dnPreSerialize first
                ClassMethod method = WriteObject.getInstance(enhancer);
                method.initialise(cv);
                method.execute();
                method.close();
            }
        }
        // Add dnGetXXX, dnSetXXX for each of the (managed) fields/properties
        for (AbstractMemberMetaData mmd : cmd.getManagedMembers()) {
            if (mmd.getPersistenceModifier() == FieldPersistenceModifier.NONE) {
                // Field/Property is not persistent so ignore
                continue;
            }
            ClassMethod getMethod = null;
            ClassMethod setMethod = null;
            if (mmd instanceof PropertyMetaData) {
            // dnGetXXX, dnSetXXX for property are typically generated when processing existing getXXX, setXXX methods
            // TODO What if the user overrode the getter and not the setter? or vice-versa?
            } else {
                // Generate dnGetXXX, dnSetXXX for field
                byte persistenceFlags = mmd.getPersistenceFlags();
                if ((persistenceFlags & Persistable.MEDIATE_READ) == Persistable.MEDIATE_READ) {
                    getMethod = new GetViaMediate(enhancer, mmd);
                } else if ((persistenceFlags & Persistable.CHECK_READ) == Persistable.CHECK_READ) {
                    getMethod = new GetViaCheck(enhancer, mmd);
                } else {
                    getMethod = new GetNormal(enhancer, mmd);
                }
                if ((persistenceFlags & Persistable.MEDIATE_WRITE) == Persistable.MEDIATE_WRITE) {
                    setMethod = new SetViaMediate(enhancer, mmd);
                } else if ((persistenceFlags & Persistable.CHECK_WRITE) == Persistable.CHECK_WRITE) {
                    setMethod = new SetViaCheck(enhancer, mmd);
                } else {
                    setMethod = new SetNormal(enhancer, mmd);
                }
            }
            if (getMethod != null) {
                getMethod.initialise(cv);
                getMethod.execute();
                getMethod.close();
            }
            if (setMethod != null) {
                setMethod.initialise(cv);
                setMethod.execute();
                setMethod.close();
            }
        }
    }
    cv.visitEnd();
}
Also used : InitClass(org.datanucleus.enhancer.methods.InitClass) SetViaCheck(org.datanucleus.enhancer.methods.SetViaCheck) SetViaMediate(org.datanucleus.enhancer.methods.SetViaMediate) PropertyMetaData(org.datanucleus.metadata.PropertyMetaData) SetNormal(org.datanucleus.enhancer.methods.SetNormal) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) GetNormal(org.datanucleus.enhancer.methods.GetNormal) GetViaCheck(org.datanucleus.enhancer.methods.GetViaCheck) GetViaMediate(org.datanucleus.enhancer.methods.GetViaMediate) PrivilegedAction(java.security.PrivilegedAction) Iterator(java.util.Iterator) List(java.util.List) ObjectStreamClass(java.io.ObjectStreamClass) InitClass(org.datanucleus.enhancer.methods.InitClass) WriteObject(org.datanucleus.enhancer.methods.WriteObject) DefaultConstructor(org.datanucleus.enhancer.methods.DefaultConstructor) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

ObjectStreamClass (java.io.ObjectStreamClass)1 PrivilegedAction (java.security.PrivilegedAction)1 Iterator (java.util.Iterator)1 List (java.util.List)1 DefaultConstructor (org.datanucleus.enhancer.methods.DefaultConstructor)1 GetNormal (org.datanucleus.enhancer.methods.GetNormal)1 GetViaCheck (org.datanucleus.enhancer.methods.GetViaCheck)1 GetViaMediate (org.datanucleus.enhancer.methods.GetViaMediate)1 InitClass (org.datanucleus.enhancer.methods.InitClass)1 SetNormal (org.datanucleus.enhancer.methods.SetNormal)1 SetViaCheck (org.datanucleus.enhancer.methods.SetViaCheck)1 SetViaMediate (org.datanucleus.enhancer.methods.SetViaMediate)1 WriteObject (org.datanucleus.enhancer.methods.WriteObject)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 PropertyMetaData (org.datanucleus.metadata.PropertyMetaData)1