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