String[] args = (fieldManager)
? new String[]{ OIDFCTYPE.getName(), Object.class.getName() }
: new String[]{ Object.class.getName() };
BCMethod method = _pc.declareMethod(PRE + "CopyKeyFieldsFromObjectId",
void.class.getName(), args);
Code code = method.getCode(true);
// call superclass method
if (_meta.getPCSuperclass() != null && !getCreateSubclass()) {
loadManagedInstance(code, false);
for (int i = 0; i < args.length; i++)
code.aload().setParam(i);
code.invokespecial().setMethod(getType(_meta.
getPCSuperclassMetaData()).getName(),
PRE + "CopyKeyFieldsFromObjectId", void.class.getName(), args);
}
if (fieldManager)
code.aload().setParam(1);
else
code.aload().setParam(0);
if (!_meta.isOpenJPAIdentity() && _meta.isObjectIdTypeShared()) {
// oid = ((ObjectId) id).getId ();
code.checkcast().setType(ObjectId.class);
code.invokevirtual().setMethod(ObjectId.class, "getId",
Object.class, null);
}
// <oid type> cast = (<oid type>) oid;
int id = code.getNextLocalsIndex();
Class oidType = _meta.getObjectIdType();
code.checkcast().setType(oidType);
code.astore().setLocal(id);
// fs.store<type>Field (<index>, id.<field>); or...
// this.<field> = id.<field>
// or for single field identity: id.getId ()
FieldMetaData[] fmds = getCreateSubclass() ? _meta.getFields()
: _meta.getDeclaredFields();
String name;
Class type;
Class unwrapped;
Field field;
Method getter;
for (int i = 0; i < fmds.length; i++) {
if (!fmds[i].isPrimaryKey())
continue;
name = fmds[i].getName();
type = fmds[i].getObjectIdFieldType();
if (!fieldManager
&& fmds[i].getDeclaredTypeCode() == JavaTypes.PC) {
// sm.getPCPrimaryKey(oid, i + pcInheritedFieldCount);
loadManagedInstance(code, false);
code.dup(); // leave orig on stack to set value into
code.getfield().setField(SM, SMTYPE);
code.aload().setLocal(id);
code.constant().setValue(i);
code.getstatic().setField(INHERIT, int.class);
code.iadd();
code.invokeinterface().setMethod(StateManager.class,
"getPCPrimaryKey", Object.class,
new Class[] { Object.class, int.class });
code.checkcast().setType(fmds[i].getDeclaredType());
} else {
unwrapped = (fmds[i].getDeclaredTypeCode() == JavaTypes.PC)
? type : unwrapSingleFieldIdentity(fmds[i]);
if (fieldManager) {
code.aload().setParam(0);
code.constant().setValue(i);
code.getstatic().setField(INHERIT, int.class);
code.iadd();
} else
loadManagedInstance(code, false);
if (unwrapped != type) {
code.anew().setType(type);
code.dup();
}
code.aload().setLocal(id);
if (_meta.isOpenJPAIdentity()) {
if (oidType == ObjectId.class) {
code.invokevirtual().setMethod(oidType, "getId",
Object.class, null);
if (!fieldManager && type != Object.class)
code.checkcast().setType(fmds[i].getDeclaredType());
} else if (oidType == DateId.class) {
code.invokevirtual().setMethod(oidType, "getId",
Date.class, null);
if (!fieldManager && type != Date.class)
code.checkcast().setType(fmds[i].getDeclaredType());
} else {
code.invokevirtual().setMethod(oidType, "getId",
unwrapped, null);
if (unwrapped != type)
code.invokespecial().setMethod(type, "<init>",
void.class, new Class[]{ unwrapped });
}
} else if (_meta.getAccessType() == ClassMetaData.ACCESS_FIELD){
field = Reflection.findField(oidType, name, true);
if (Modifier.isPublic(field.getModifiers()))
code.getfield().setField(field);
else {
// Reflection.getXXX(oid, Reflection.findField(...));
code.classconstant().setClass(oidType);
code.constant().setValue(name);
code.constant().setValue(true);
code.invokestatic().setMethod(Reflection.class,
"findField", Field.class, new Class[] {
Class.class, String.class, boolean.class });
code.invokestatic().setMethod
(getReflectionGetterMethod(type, Field.class));
if (!type.isPrimitive() && type != Object.class)
code.checkcast().setType(type);
}
} else {
getter = Reflection.findGetter(oidType, name, true);
if (Modifier.isPublic(getter.getModifiers()))
code.invokevirtual().setMethod(getter);
else {
// Reflection.getXXX(oid, Reflection.findGetter(...));
code.classconstant().setClass(oidType);
code.constant().setValue(name);
code.constant().setValue(true);
code.invokestatic().setMethod(Reflection.class,
"findGetter", Method.class, new Class[] {
Class.class, String.class, boolean.class });
code.invokestatic().setMethod
(getReflectionGetterMethod(type, Method.class));
if (!type.isPrimitive() && type != Object.class)
code.checkcast().setType(type);
}
}
}
if (fieldManager)
code.invokeinterface().setMethod(getFieldConsumerMethod(type));
else
addSetManagedValueCode(code, fmds[i]);
}
code.vreturn();
code.calculateMaxStack();
code.calculateMaxLocals();
}