From 0ab02d696593b1507e0fcfdbfd2af50e1ed5e74b Mon Sep 17 00:00:00 2001 From: Mark Struberg Date: Fri, 14 Jul 2023 12:15:54 +0200 Subject: [PATCH] OPENJPA-2911 AuxilaryEnhancer in ASM --- .../apache/openjpa/enhance/PCEnhancer.java | 10 ++--- .../stats/FetchStatisticsAuxEnhancer.java | 39 ++++++++++--------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java index a3593904e..7ea61e1c0 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java @@ -605,10 +605,9 @@ public class PCEnhancer { addAttachDetachCode(); addSerializationCode(); addCloningCode(); + runAuxiliaryEnhancers(); AsmHelper.readIntoBCClass(pc, _pc); - - runAuxiliaryEnhancers(); return ENHANCE_PC; } return ENHANCE_AWARE; @@ -4209,7 +4208,7 @@ public class PCEnhancer { */ private void runAuxiliaryEnhancers() { for (AuxiliaryEnhancer auxEnhancer : _auxEnhancers) { - auxEnhancer.run(_pc, _meta); + auxEnhancer.run(pc.getClassNode(), _meta); } } @@ -5747,10 +5746,7 @@ public class PCEnhancer { */ public interface AuxiliaryEnhancer { - void run (BCClass bc, ClassMetaData meta); - - @Deprecated - boolean skipEnhance(BCMethod m); + void run (ClassNode classNode, ClassMetaData meta); boolean skipEnhance(MethodNode m); } diff --git a/openjpa-tools/openjpa-fetch-statistics/src/main/java/org/apache/openjpa/enhance/stats/FetchStatisticsAuxEnhancer.java b/openjpa-tools/openjpa-fetch-statistics/src/main/java/org/apache/openjpa/enhance/stats/FetchStatisticsAuxEnhancer.java index 07989ea61..3c4ad9a5b 100644 --- a/openjpa-tools/openjpa-fetch-statistics/src/main/java/org/apache/openjpa/enhance/stats/FetchStatisticsAuxEnhancer.java +++ b/openjpa-tools/openjpa-fetch-statistics/src/main/java/org/apache/openjpa/enhance/stats/FetchStatisticsAuxEnhancer.java @@ -28,10 +28,13 @@ import org.apache.openjpa.meta.AccessCode; import org.apache.openjpa.meta.ClassMetaData; 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 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 @@ -43,13 +46,8 @@ public class FetchStatisticsAuxEnhancer implements AuxiliaryEnhancer { + "(pc(.)*DetachedState)?(pc(.)*EnhancementContractVersion)?(pc(.)*ManagedFieldCount)?(pc(.)*GetVersion)?"; @Override - public void run(BCClass bcc, ClassMetaData cmd) { - addEnhancement(bcc, cmd); - } - - @Override - public boolean skipEnhance(BCMethod arg0) { - return false; + public void run(ClassNode classNode, ClassMetaData cmd) { + addEnhancement(classNode, cmd); } @Override @@ -57,21 +55,26 @@ public class FetchStatisticsAuxEnhancer implements AuxiliaryEnhancer { 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); FetchStatsCollector.setlogger(log); - for (BCMethod meth : bcc.getMethods()) { - String methodName = meth.getName(); + String className = classNode.name.replace("/", "."); + for (MethodNode meth : classNode.methods) { + String methodName = meth.name; FieldMetaData fmd = getFieldName(methodName, cmd); if (fmd != null && needsTracking(fmd, methodName, cmd)) { - String fqn = bcc.getName() + "." + fmd.getName(); + String fqn = className + "." + fmd.getName(); FetchStatsCollector.registerField(fqn); FetchStatsCollector.registerEntity(cmd); - Code code = meth.getCode(false); - code.constant().setValue(fqn); - code.invokestatic().setMethod(FetchStatsCollector.class, "hit", void.class, - new Class[] { String.class }); + InsnList instructions = new InsnList(); + + instructions.add(AsmHelper.getLoadConstantInsn(fqn)); + instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, + Type.getInternalName(FetchStatsCollector.class), + "hit", + Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)))); + meth.instructions.insert(instructions); } } }