OPENJPA-2911 AuxilaryEnhancer in ASM

This commit is contained in:
Mark Struberg 2023-07-14 12:15:54 +02:00
parent fb20da5063
commit 0ab02d6965
2 changed files with 24 additions and 25 deletions

View File

@ -605,10 +605,9 @@ public class PCEnhancer {
addAttachDetachCode(); addAttachDetachCode();
addSerializationCode(); addSerializationCode();
addCloningCode(); addCloningCode();
runAuxiliaryEnhancers();
AsmHelper.readIntoBCClass(pc, _pc); AsmHelper.readIntoBCClass(pc, _pc);
runAuxiliaryEnhancers();
return ENHANCE_PC; return ENHANCE_PC;
} }
return ENHANCE_AWARE; return ENHANCE_AWARE;
@ -4209,7 +4208,7 @@ public class PCEnhancer {
*/ */
private void runAuxiliaryEnhancers() { private void runAuxiliaryEnhancers() {
for (AuxiliaryEnhancer auxEnhancer : _auxEnhancers) { for (AuxiliaryEnhancer auxEnhancer : _auxEnhancers) {
auxEnhancer.run(_pc, _meta); auxEnhancer.run(pc.getClassNode(), _meta);
} }
} }
@ -5747,10 +5746,7 @@ public class PCEnhancer {
*/ */
public interface AuxiliaryEnhancer public interface AuxiliaryEnhancer
{ {
void run (BCClass bc, ClassMetaData meta); void run (ClassNode classNode, ClassMetaData meta);
@Deprecated
boolean skipEnhance(BCMethod m);
boolean skipEnhance(MethodNode m); boolean skipEnhance(MethodNode m);
} }

View File

@ -28,10 +28,13 @@ import org.apache.openjpa.meta.AccessCode;
import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.FieldMetaData; import org.apache.openjpa.meta.FieldMetaData;
import org.apache.openjpa.util.asm.AsmHelper;
import org.apache.xbean.asm9.Opcodes;
import org.apache.xbean.asm9.Type;
import org.apache.xbean.asm9.tree.ClassNode;
import org.apache.xbean.asm9.tree.InsnList;
import org.apache.xbean.asm9.tree.MethodInsnNode;
import org.apache.xbean.asm9.tree.MethodNode; import org.apache.xbean.asm9.tree.MethodNode;
import serp.bytecode.BCClass;
import serp.bytecode.BCMethod;
import serp.bytecode.Code;
/** /**
* FetchStatisticsAuxEnhancer adds the call back function to each persistent fields in the persistent entity which * FetchStatisticsAuxEnhancer adds the call back function to each persistent fields in the persistent entity which
@ -43,13 +46,8 @@ public class FetchStatisticsAuxEnhancer implements AuxiliaryEnhancer {
+ "(pc(.)*DetachedState)?(pc(.)*EnhancementContractVersion)?(pc(.)*ManagedFieldCount)?(pc(.)*GetVersion)?"; + "(pc(.)*DetachedState)?(pc(.)*EnhancementContractVersion)?(pc(.)*ManagedFieldCount)?(pc(.)*GetVersion)?";
@Override @Override
public void run(BCClass bcc, ClassMetaData cmd) { public void run(ClassNode classNode, ClassMetaData cmd) {
addEnhancement(bcc, cmd); addEnhancement(classNode, cmd);
}
@Override
public boolean skipEnhance(BCMethod arg0) {
return false;
} }
@Override @Override
@ -57,21 +55,26 @@ public class FetchStatisticsAuxEnhancer implements AuxiliaryEnhancer {
return false; return false;
} }
private void addEnhancement(BCClass bcc, ClassMetaData cmd) { private void addEnhancement(ClassNode classNode, ClassMetaData cmd) {
Log log = cmd.getRepository().getConfiguration().getLog(OpenJPAConfiguration.LOG_RUNTIME); Log log = cmd.getRepository().getConfiguration().getLog(OpenJPAConfiguration.LOG_RUNTIME);
FetchStatsCollector.setlogger(log); FetchStatsCollector.setlogger(log);
for (BCMethod meth : bcc.getMethods()) { String className = classNode.name.replace("/", ".");
String methodName = meth.getName(); for (MethodNode meth : classNode.methods) {
String methodName = meth.name;
FieldMetaData fmd = getFieldName(methodName, cmd); FieldMetaData fmd = getFieldName(methodName, cmd);
if (fmd != null && needsTracking(fmd, methodName, cmd)) { if (fmd != null && needsTracking(fmd, methodName, cmd)) {
String fqn = bcc.getName() + "." + fmd.getName(); String fqn = className + "." + fmd.getName();
FetchStatsCollector.registerField(fqn); FetchStatsCollector.registerField(fqn);
FetchStatsCollector.registerEntity(cmd); FetchStatsCollector.registerEntity(cmd);
Code code = meth.getCode(false); InsnList instructions = new InsnList();
code.constant().setValue(fqn);
code.invokestatic().setMethod(FetchStatsCollector.class, "hit", void.class, instructions.add(AsmHelper.getLoadConstantInsn(fqn));
new Class[] { String.class }); instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
Type.getInternalName(FetchStatsCollector.class),
"hit",
Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class))));
meth.instructions.insert(instructions);
} }
} }
} }