From ebaf4ebe16f8022873422719ff69b7552bf5946e Mon Sep 17 00:00:00 2001 From: redkale Date: Wed, 20 Dec 2023 18:52:41 +0800 Subject: [PATCH] asm --- src/main/java/org/redkale/asm/Asms.java | 3 +- .../cache/spi/CacheAsmMethodBoost.java | 31 ++++++++----------- .../org/redkale/cache/spi/DynForCache.java | 19 ++++++++++++ .../java/org/redkale/lock/spi/DynForLock.java | 5 +++ .../redkale/lock/spi/LockAsmMethodBoost.java | 9 ++++-- 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/redkale/asm/Asms.java b/src/main/java/org/redkale/asm/Asms.java index f1cc00e82..9686702de 100644 --- a/src/main/java/org/redkale/asm/Asms.java +++ b/src/main/java/org/redkale/asm/Asms.java @@ -12,6 +12,7 @@ import static org.redkale.asm.Opcodes.ICONST_0; import static org.redkale.asm.Opcodes.INVOKESTATIC; import static org.redkale.asm.Opcodes.INVOKEVIRTUAL; import static org.redkale.asm.Opcodes.SIPUSH; +import org.redkale.util.RedkaleException; /** * ASM简单的工具方法
@@ -72,7 +73,7 @@ public final class Asms { } av.visitEnd(); } catch (Exception e) { - e.printStackTrace(); + throw new RedkaleException(e); } } diff --git a/src/main/java/org/redkale/cache/spi/CacheAsmMethodBoost.java b/src/main/java/org/redkale/cache/spi/CacheAsmMethodBoost.java index eab088601..c7bfed72d 100644 --- a/src/main/java/org/redkale/cache/spi/CacheAsmMethodBoost.java +++ b/src/main/java/org/redkale/cache/spi/CacheAsmMethodBoost.java @@ -9,28 +9,16 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import org.redkale.asm.AnnotationVisitor; import org.redkale.asm.AsmMethodBean; import org.redkale.asm.AsmMethodBoost; import org.redkale.asm.Asms; import org.redkale.asm.ClassWriter; +import org.redkale.asm.FieldVisitor; import org.redkale.asm.Label; import org.redkale.asm.MethodDebugVisitor; -import static org.redkale.asm.Opcodes.ACC_PRIVATE; -import static org.redkale.asm.Opcodes.ACC_PROTECTED; -import static org.redkale.asm.Opcodes.ACC_PUBLIC; -import static org.redkale.asm.Opcodes.ALOAD; -import static org.redkale.asm.Opcodes.ARETURN; -import static org.redkale.asm.Opcodes.DLOAD; -import static org.redkale.asm.Opcodes.DRETURN; -import static org.redkale.asm.Opcodes.FLOAD; -import static org.redkale.asm.Opcodes.FRETURN; -import static org.redkale.asm.Opcodes.ILOAD; -import static org.redkale.asm.Opcodes.INVOKESPECIAL; -import static org.redkale.asm.Opcodes.IRETURN; -import static org.redkale.asm.Opcodes.LLOAD; -import static org.redkale.asm.Opcodes.LRETURN; -import static org.redkale.asm.Opcodes.RETURN; +import static org.redkale.asm.Opcodes.*; import org.redkale.asm.Type; import org.redkale.cache.Cached; import org.redkale.util.RedkaleException; @@ -41,7 +29,9 @@ import org.redkale.util.RedkaleException; */ public class CacheAsmMethodBoost implements AsmMethodBoost { - private static final List> FILTER_ANN = List.of(Cached.class); + private static final List> FILTER_ANN = List.of(Cached.class, DynForCache.class); + + private final AtomicInteger fieldIndex = new AtomicInteger(); protected final Class serviceType; @@ -75,8 +65,8 @@ public class CacheAsmMethodBoost implements AsmMethodBoost { acc = ACC_PRIVATE; nowMethodName = newMethodName; } - final String rsMethodName = method.getName() + "_afterCache"; + final String dynFieldName = fieldPrefix + "_" + method.getName() + "CacheAction" + fieldIndex.incrementAndGet(); { Map methodBeans = AsmMethodBoost.getMethodBeans(serviceType); AsmMethodBean methodBean = AsmMethodBean.get(methodBeans, method); @@ -104,7 +94,8 @@ public class CacheAsmMethodBoost implements AsmMethodBoost { Label l0 = new Label(); mv.visitLabel(l0); av = mv.visitAnnotation(cacheDynDesc, true); - av.visitEnd(); + av.visit("dynField", dynFieldName); + Asms.visitAnnotation(av, cached); if (newMethodName == null) { //给方法加上原有的Annotation final Annotation[] anns = method.getAnnotations(); @@ -175,6 +166,10 @@ public class CacheAsmMethodBoost implements AsmMethodBoost { mv.visitMaxs(20, 20); mv.visitEnd(); } + { + FieldVisitor fv = cw.visitField(ACC_PRIVATE, dynFieldName, Type.getDescriptor(CacheAction.class), null, null); + fv.visitEnd(); + } return rsMethodName; } diff --git a/src/main/java/org/redkale/cache/spi/DynForCache.java b/src/main/java/org/redkale/cache/spi/DynForCache.java index 233e5966c..1f5f9bf34 100644 --- a/src/main/java/org/redkale/cache/spi/DynForCache.java +++ b/src/main/java/org/redkale/cache/spi/DynForCache.java @@ -9,14 +9,33 @@ import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Target; +import java.util.concurrent.TimeUnit; /** + * {@link org.redkale.cache.Cached}注解的动态扩展版,会多一个字段信息 + * 用于识别方法是否已经动态处理过 * * @author zhangjx + * + * @since 2.8.0 */ @Inherited @Documented @Target({METHOD}) @Retention(RUNTIME) public @interface DynForCache { + + String dynField(); + + String key(); + + String map(); + + String localExpire(); + + String remoteExpire(); + + TimeUnit timeUnit(); + + boolean nullable(); } diff --git a/src/main/java/org/redkale/lock/spi/DynForLock.java b/src/main/java/org/redkale/lock/spi/DynForLock.java index 8cde7a87b..5021b5bdc 100644 --- a/src/main/java/org/redkale/lock/spi/DynForLock.java +++ b/src/main/java/org/redkale/lock/spi/DynForLock.java @@ -11,8 +11,12 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Target; /** + * {@link org.redkale.lock.Locked}注解的动态扩展版,会多一个字段信息 + * 用于识别方法是否已经动态处理过 * * @author zhangjx + * + * @since 2.8.0 */ @Inherited @Documented @@ -20,4 +24,5 @@ import java.lang.annotation.Target; @Retention(RUNTIME) public @interface DynForLock { + String dynField(); } diff --git a/src/main/java/org/redkale/lock/spi/LockAsmMethodBoost.java b/src/main/java/org/redkale/lock/spi/LockAsmMethodBoost.java index 18dbc6dcb..32793fa2c 100644 --- a/src/main/java/org/redkale/lock/spi/LockAsmMethodBoost.java +++ b/src/main/java/org/redkale/lock/spi/LockAsmMethodBoost.java @@ -9,6 +9,7 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import org.redkale.asm.AnnotationVisitor; import org.redkale.asm.AsmMethodBean; import org.redkale.asm.AsmMethodBoost; @@ -27,7 +28,9 @@ import org.redkale.util.RedkaleException; */ public class LockAsmMethodBoost implements AsmMethodBoost { - private static final List> FILTER_ANN = List.of(Locked.class); + private static final List> FILTER_ANN = List.of(Locked.class, DynForLock.class); + + private final AtomicInteger fieldIndex = new AtomicInteger(); protected final Class serviceType; @@ -63,6 +66,7 @@ public class LockAsmMethodBoost implements AsmMethodBoost { } final String rsMethodName = method.getName() + "_afterLock"; + final String dynFieldName = fieldPrefix + "_" + method.getName() + "LockAction" + fieldIndex.incrementAndGet(); { Map methodBeans = AsmMethodBoost.getMethodBeans(serviceType); AsmMethodBean methodBean = AsmMethodBean.get(methodBeans, method); @@ -90,7 +94,8 @@ public class LockAsmMethodBoost implements AsmMethodBoost { Label l0 = new Label(); mv.visitLabel(l0); av = mv.visitAnnotation(lockDynDesc, true); - av.visitEnd(); + av.visit("dynField", dynFieldName); + Asms.visitAnnotation(av, cached); if (newMethodName == null) { //给方法加上原有的Annotation final Annotation[] anns = method.getAnnotations();