MessageAsmMethodBoost优化

This commit is contained in:
redkale
2024-08-12 20:05:59 +08:00
parent 6ad3ba192b
commit 45548f2de9
9 changed files with 92 additions and 23 deletions

View File

@@ -91,6 +91,7 @@ public abstract class AsmMethodBoost<T> {
* *
* @param classLoader ClassLoader * @param classLoader ClassLoader
* @param cw 动态字节码Writer * @param cw 动态字节码Writer
* @param serviceImplClass 原始实现类
* @param newDynName 动态新类名 * @param newDynName 动态新类名
* @param fieldPrefix 动态字段的前缀 * @param fieldPrefix 动态字段的前缀
* @param filterAnns 需要过滤的注解 * @param filterAnns 需要过滤的注解
@@ -101,6 +102,7 @@ public abstract class AsmMethodBoost<T> {
public abstract String doMethod( public abstract String doMethod(
ClassLoader classLoader, ClassLoader classLoader,
ClassWriter cw, ClassWriter cw,
Class serviceImplClass,
String newDynName, String newDynName,
String fieldPrefix, String fieldPrefix,
List<Class<? extends Annotation>> filterAnns, List<Class<? extends Annotation>> filterAnns,
@@ -180,7 +182,10 @@ public abstract class AsmMethodBoost<T> {
for (Annotation ann : anns) { for (Annotation ann : anns) {
if (ann.annotationType() != selfAnnType if (ann.annotationType() != selfAnnType
&& (filterAnns == null || !filterAnns.contains(ann.annotationType()))) { && (filterAnns == null || !filterAnns.contains(ann.annotationType()))) {
Asms.visitAnnotation(mv.visitAnnotation(Type.getDescriptor(ann.annotationType()), true), ann); Asms.visitAnnotation(
mv.visitAnnotation(Type.getDescriptor(ann.annotationType()), true),
ann.annotationType(),
ann);
} }
} }
// 给参数加上原有的Annotation // 给参数加上原有的Annotation
@@ -188,7 +193,9 @@ public abstract class AsmMethodBoost<T> {
for (int k = 0; k < annss.length; k++) { for (int k = 0; k < annss.length; k++) {
for (Annotation ann : annss[k]) { for (Annotation ann : annss[k]) {
Asms.visitAnnotation( Asms.visitAnnotation(
mv.visitParameterAnnotation(k, Type.getDescriptor(ann.annotationType()), true), ann); mv.visitParameterAnnotation(k, Type.getDescriptor(ann.annotationType()), true),
ann.annotationType(),
ann);
} }
} }
} }
@@ -301,6 +308,7 @@ public abstract class AsmMethodBoost<T> {
public String doMethod( public String doMethod(
ClassLoader classLoader, ClassLoader classLoader,
ClassWriter cw, ClassWriter cw,
Class serviceImplClass,
String newDynName, String newDynName,
String fieldPrefix, String fieldPrefix,
List<Class<? extends Annotation>> filterAnns, List<Class<? extends Annotation>> filterAnns,
@@ -309,7 +317,15 @@ public abstract class AsmMethodBoost<T> {
String newName = newMethodName; String newName = newMethodName;
for (AsmMethodBoost item : items) { for (AsmMethodBoost item : items) {
if (item != null) { if (item != null) {
newName = item.doMethod(classLoader, cw, newDynName, fieldPrefix, filterAnns, method, newName); newName = item.doMethod(
classLoader,
cw,
serviceImplClass,
newDynName,
fieldPrefix,
filterAnns,
method,
newName);
} }
} }
return newName; return newName;

View File

@@ -3,6 +3,10 @@
*/ */
package org.redkale.asm; package org.redkale.asm;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import static org.redkale.asm.Opcodes.BIPUSH; import static org.redkale.asm.Opcodes.BIPUSH;
import static org.redkale.asm.Opcodes.CHECKCAST; import static org.redkale.asm.Opcodes.CHECKCAST;
import static org.redkale.asm.Opcodes.GETSTATIC; import static org.redkale.asm.Opcodes.GETSTATIC;
@@ -10,9 +14,6 @@ import static org.redkale.asm.Opcodes.ICONST_0;
import static org.redkale.asm.Opcodes.INVOKESTATIC; import static org.redkale.asm.Opcodes.INVOKESTATIC;
import static org.redkale.asm.Opcodes.INVOKEVIRTUAL; import static org.redkale.asm.Opcodes.INVOKEVIRTUAL;
import static org.redkale.asm.Opcodes.SIPUSH; import static org.redkale.asm.Opcodes.SIPUSH;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.redkale.util.RedkaleException; import org.redkale.util.RedkaleException;
/** /**
@@ -36,9 +37,18 @@ public final class Asms {
false); false);
} }
public static void visitAnnotation(final AnnotationVisitor av, final Annotation ann) { // annType与annValue不一定是同一类型
public static <T extends Annotation> void visitAnnotation(
final AnnotationVisitor av, final Class<T> annType, final Annotation annValue) {
try { try {
for (Method anm : ann.annotationType().getMethods()) { Set<String> methods = null;
if (annType != annValue.annotationType()) {
methods = new HashSet<>();
for (Method anm : annType.getMethods()) {
methods.add(anm.getName());
}
}
for (Method anm : annValue.annotationType().getMethods()) {
final String mname = anm.getName(); final String mname = anm.getName();
if ("equals".equals(mname) if ("equals".equals(mname)
|| "hashCode".equals(mname) || "hashCode".equals(mname)
@@ -46,7 +56,10 @@ public final class Asms {
|| "annotationType".equals(mname)) { || "annotationType".equals(mname)) {
continue; continue;
} }
final Object r = anm.invoke(ann); if (methods != null && !methods.contains(mname)) {
continue;
}
final Object r = anm.invoke(annValue);
if (r instanceof String[]) { if (r instanceof String[]) {
AnnotationVisitor av1 = av.visitArray(mname); AnnotationVisitor av1 = av.visitArray(mname);
for (String item : (String[]) r) { for (String item : (String[]) r) {
@@ -62,14 +75,15 @@ public final class Asms {
} else if (r instanceof Enum[]) { } else if (r instanceof Enum[]) {
AnnotationVisitor av1 = av.visitArray(mname); AnnotationVisitor av1 = av.visitArray(mname);
for (Enum item : (Enum[]) r) { for (Enum item : (Enum[]) r) {
av1.visitEnum(null, Type.getDescriptor(item.getClass()), ((Enum) item).name()); av1.visitEnum(null, Type.getDescriptor(item.getClass()), item.name());
} }
av1.visitEnd(); av1.visitEnd();
} else if (r instanceof Annotation[]) { } else if (r instanceof Annotation[]) {
AnnotationVisitor av1 = av.visitArray(mname); AnnotationVisitor av1 = av.visitArray(mname);
for (Annotation item : (Annotation[]) r) { for (Annotation item : (Annotation[]) r) {
visitAnnotation( visitAnnotation(
av1.visitAnnotation(null, Type.getDescriptor(((Annotation) item).annotationType())), av1.visitAnnotation(null, Type.getDescriptor(((Annotation) r).annotationType())),
item.annotationType(),
item); item);
} }
av1.visitEnd(); av1.visitEnd();
@@ -80,6 +94,7 @@ public final class Asms {
} else if (r instanceof Annotation) { } else if (r instanceof Annotation) {
visitAnnotation( visitAnnotation(
av.visitAnnotation(null, Type.getDescriptor(((Annotation) r).annotationType())), av.visitAnnotation(null, Type.getDescriptor(((Annotation) r).annotationType())),
((Annotation) r).annotationType(),
(Annotation) r); (Annotation) r);
} else { } else {
av.visit(mname, r); av.visit(mname, r);

View File

@@ -340,6 +340,16 @@ public final class ClassFilter<T> {
if (className.startsWith("java.") || className.startsWith("javax.")) { if (className.startsWith("java.") || className.startsWith("javax.")) {
return false; return false;
} }
return acceptPattern(className);
}
/**
* 判断class是否符合正则表达式
*
* @param className Class
* @return boolean
*/
public boolean acceptPattern(String className) {
if (excludePatterns != null) { if (excludePatterns != null) {
for (Pattern reg : excludePatterns) { for (Pattern reg : excludePatterns) {
if (reg.matcher(className).matches()) { if (reg.matcher(className).matches()) {

View File

@@ -63,6 +63,7 @@ public class CachedAsmMethodBoost extends AsmMethodBoost {
public String doMethod( public String doMethod(
final ClassLoader classLoader, final ClassLoader classLoader,
final ClassWriter cw, final ClassWriter cw,
final Class serviceImplClass,
final String newDynName, final String newDynName,
final String fieldPrefix, final String fieldPrefix,
final List filterAnns, final List filterAnns,
@@ -105,7 +106,7 @@ public class CachedAsmMethodBoost extends AsmMethodBoost {
// mv.setDebug(true); // mv.setDebug(true);
AnnotationVisitor av = mv.visitAnnotation(cacheDynDesc, true); AnnotationVisitor av = mv.visitAnnotation(cacheDynDesc, true);
av.visit("dynField", dynFieldName); av.visit("dynField", dynFieldName);
Asms.visitAnnotation(av, cached); Asms.visitAnnotation(av, DynForCached.class, cached);
visitRawAnnotation(method, newMethodName, mv, Cached.class, filterAnns); visitRawAnnotation(method, newMethodName, mv, Cached.class, filterAnns);
Label l0 = new Label(); Label l0 = new Label();

View File

@@ -39,6 +39,7 @@ public class LockedAsmMethodBoost extends AsmMethodBoost {
public String doMethod( public String doMethod(
ClassLoader classLoader, ClassLoader classLoader,
ClassWriter cw, ClassWriter cw,
Class serviceImplClass,
String newDynName, String newDynName,
String fieldPrefix, String fieldPrefix,
List filterAnns, List filterAnns,
@@ -75,7 +76,7 @@ public class LockedAsmMethodBoost extends AsmMethodBoost {
mv.visitLabel(l0); mv.visitLabel(l0);
AnnotationVisitor av = mv.visitAnnotation(lockDynDesc, true); AnnotationVisitor av = mv.visitAnnotation(lockDynDesc, true);
av.visit("dynField", dynFieldName); av.visit("dynField", dynFieldName);
Asms.visitAnnotation(av, locked); Asms.visitAnnotation(av, DynForLocked.class, locked);
visitRawAnnotation(method, newMethodName, mv, Locked.class, filterAnns); visitRawAnnotation(method, newMethodName, mv, Locked.class, filterAnns);
mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0);
List<Integer> insns = visitVarInsnParamTypes(mv, method, 0); List<Integer> insns = visitVarInsnParamTypes(mv, method, 0);

View File

@@ -550,10 +550,10 @@ public abstract class MessageAgent implements MessageManager {
} }
public Future onMessage(MessageConext context, String traceid, byte[] message) { public Future onMessage(MessageConext context, String traceid, byte[] message) {
Convert c = this.convert;
MessageConsumer m = this.consumer;
return messageAgent.submit(() -> { return messageAgent.submit(() -> {
Traces.computeIfAbsent(traceid); Traces.computeIfAbsent(traceid);
Convert c = this.convert;
MessageConsumer m = this.consumer;
try { try {
m.onMessage(context, (T) c.convertFrom(messageType, message)); m.onMessage(context, (T) c.convertFrom(messageType, message));
} catch (Throwable t) { } catch (Throwable t) {
@@ -563,12 +563,17 @@ public abstract class MessageAgent implements MessageManager {
Level.SEVERE, Level.SEVERE,
m.getClass().getSimpleName() m.getClass().getSimpleName()
+ " onMessage error, topic: " + context.getTopic() + " onMessage error, topic: " + context.getTopic()
+ ", messages: " + new String(message, StandardCharsets.UTF_8)); + ", messages: " + new String(message, StandardCharsets.UTF_8),
t);
} }
Traces.removeTraceid(); Traces.removeTraceid();
}); });
} }
public MessageConsumer getConsumer() {
return consumer;
}
public void destroy(AnyValue config) { public void destroy(AnyValue config) {
consumer.destroy(config); consumer.destroy(config);
} }

View File

@@ -12,6 +12,7 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.redkale.annotation.AutoLoad;
import org.redkale.asm.AnnotationVisitor; import org.redkale.asm.AnnotationVisitor;
import org.redkale.asm.AsmMethodBean; import org.redkale.asm.AsmMethodBean;
import org.redkale.asm.AsmMethodBoost; import org.redkale.asm.AsmMethodBoost;
@@ -83,6 +84,7 @@ public class MessageAsmMethodBoost extends AsmMethodBoost {
public String doMethod( public String doMethod(
ClassLoader classLoader, ClassLoader classLoader,
ClassWriter cw, ClassWriter cw,
Class serviceImplClass,
String newDynName, String newDynName,
String fieldPrefix, String fieldPrefix,
List filterAnns, List filterAnns,
@@ -183,7 +185,12 @@ public class MessageAsmMethodBoost extends AsmMethodBoost {
new String[] {messageConsumerName}); new String[] {messageConsumerName});
{ {
AnnotationVisitor av = cw.visitAnnotation(org.redkale.asm.Type.getDescriptor(ResourceConsumer.class), true); AnnotationVisitor av = cw.visitAnnotation(org.redkale.asm.Type.getDescriptor(ResourceConsumer.class), true);
Asms.visitAnnotation(av, messaged); Asms.visitAnnotation(av, ResourceConsumer.class, messaged);
av.visitEnd();
}
{ // 必须设置成@AutoLoad(false) 否则预编译打包后会被自动加载
AnnotationVisitor av = cw.visitAnnotation(org.redkale.asm.Type.getDescriptor(AutoLoad.class), true);
av.visit("value", false);
av.visitEnd(); av.visitEnd();
} }
cw.visitInnerClass(innerFullName, newDynName, innerClassName, ACC_PUBLIC + ACC_STATIC); cw.visitInnerClass(innerFullName, newDynName, innerClassName, ACC_PUBLIC + ACC_STATIC);

View File

@@ -18,6 +18,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level; import java.util.logging.Level;
import org.redkale.annotation.AutoLoad;
import org.redkale.asm.AsmMethodBoost; import org.redkale.asm.AsmMethodBoost;
import org.redkale.boot.Application; import org.redkale.boot.Application;
import org.redkale.boot.ClassFilter; import org.redkale.boot.ClassFilter;
@@ -266,6 +267,10 @@ public class MessageModuleEngine extends ModuleEngine {
List<ClassFilter.FilterEntry<? extends MessageConsumer>> allEntrys = new ArrayList(allFilter.getFilterEntrys()); List<ClassFilter.FilterEntry<? extends MessageConsumer>> allEntrys = new ArrayList(allFilter.getFilterEntrys());
for (ClassFilter.FilterEntry<? extends MessageConsumer> en : allEntrys) { for (ClassFilter.FilterEntry<? extends MessageConsumer> en : allEntrys) {
Class<? extends MessageConsumer> clazz = en.getType(); Class<? extends MessageConsumer> clazz = en.getType();
AutoLoad auto = clazz.getAnnotation(AutoLoad.class);
if (auto != null && !auto.value()) {
continue;
}
ResourceConsumer res = clazz.getAnnotation(ResourceConsumer.class); ResourceConsumer res = clazz.getAnnotation(ResourceConsumer.class);
if (res != null && res.required() && findMessageAgent(res.mq()) == null) { if (res != null && res.required() && findMessageAgent(res.mq()) == null) {
throw new RedkaleException("Not found " + MessageAgent.class.getSimpleName() + "(name = " + res.mq() throw new RedkaleException("Not found " + MessageAgent.class.getSimpleName() + "(name = " + res.mq()
@@ -461,6 +466,11 @@ public class MessageModuleEngine extends ModuleEngine {
|| !Objects.equals(agent.getName(), environment.getPropertyValue(res.mq()))) { || !Objects.equals(agent.getName(), environment.getPropertyValue(res.mq()))) {
continue; continue;
} }
AutoLoad auto = clazz.getAnnotation(AutoLoad.class);
if ((auto != null && !auto.value())
&& (filter.getIncludePatterns() == null || !filter.acceptPattern(clazz.getName()))) {
continue;
}
RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName()); RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName());
final MessageConsumer consumer = final MessageConsumer consumer =
clazz.getDeclaredConstructor().newInstance(); clazz.getDeclaredConstructor().newInstance();

View File

@@ -563,7 +563,8 @@ public abstract class Sncp {
if (ann instanceof Resource || ann instanceof SncpDyn || ann instanceof ResourceType) { if (ann instanceof Resource || ann instanceof SncpDyn || ann instanceof ResourceType) {
continue; continue;
} }
Asms.visitAnnotation(cw.visitAnnotation(Type.getDescriptor(ann.annotationType()), true), ann); Asms.visitAnnotation(
cw.visitAnnotation(Type.getDescriptor(ann.annotationType()), true), ann.annotationType(), ann);
} }
} }
{ {
@@ -658,7 +659,7 @@ public abstract class Sncp {
methodKeys.add(mk); methodKeys.add(mk);
List<Class<? extends Annotation>> filterAnns = methodBoost.filterMethodAnnotations(method); List<Class<? extends Annotation>> filterAnns = methodBoost.filterMethodAnnotations(method);
String newMethodName = String newMethodName =
methodBoost.doMethod(classLoader, cw, newDynName, FIELDPREFIX, filterAnns, method, null); methodBoost.doMethod(classLoader, cw, clazz, newDynName, FIELDPREFIX, filterAnns, method, null);
if (newMethodName != null) { if (newMethodName != null) {
String desc = Type.getMethodDescriptor(method); String desc = Type.getMethodDescriptor(method);
AsmMethodBean methodBean = AsmMethodBean.get(methodBeans, method); AsmMethodBean methodBean = AsmMethodBean.get(methodBeans, method);
@@ -1030,7 +1031,8 @@ public abstract class Sncp {
if (ann instanceof Resource || ann instanceof SncpDyn || ann instanceof ResourceType) { if (ann instanceof Resource || ann instanceof SncpDyn || ann instanceof ResourceType) {
continue; continue;
} }
Asms.visitAnnotation(cw.visitAnnotation(Type.getDescriptor(ann.annotationType()), true), ann); Asms.visitAnnotation(
cw.visitAnnotation(Type.getDescriptor(ann.annotationType()), true), ann.annotationType(), ann);
} }
} }
{ {
@@ -1107,8 +1109,8 @@ public abstract class Sncp {
String newMethodName = null; String newMethodName = null;
if (methodBoost != null) { if (methodBoost != null) {
List<Class<? extends Annotation>> filterAnns = methodBoost.filterMethodAnnotations(method); List<Class<? extends Annotation>> filterAnns = methodBoost.filterMethodAnnotations(method);
newMethodName = newMethodName = methodBoost.doMethod(
methodBoost.doMethod(classLoader, cw, newDynName, FIELDPREFIX, filterAnns, method, null); classLoader, cw, serviceTypeOrImplClass, newDynName, FIELDPREFIX, filterAnns, method, null);
} }
if (newMethodName != null) { if (newMethodName != null) {
acc = ACC_PRIVATE; acc = ACC_PRIVATE;
@@ -1125,7 +1127,9 @@ public abstract class Sncp {
for (int k = 0; k < anns.length; k++) { for (int k = 0; k < anns.length; k++) {
for (Annotation ann : anns[k]) { for (Annotation ann : anns[k]) {
Asms.visitAnnotation( Asms.visitAnnotation(
mv.visitParameterAnnotation(k, Type.getDescriptor(ann.annotationType()), true), ann); mv.visitParameterAnnotation(k, Type.getDescriptor(ann.annotationType()), true),
ann.annotationType(),
ann);
} }
} }
} }