MessageAsmMethodBoost优化
This commit is contained in:
@@ -166,7 +166,7 @@ class NodeWebSocketNodeLoader implements ResourceTypeLoader {
|
|||||||
MessageAgent messageAgent = null;
|
MessageAgent messageAgent = null;
|
||||||
try {
|
try {
|
||||||
Field c = WebSocketServlet.class.getDeclaredField("messageAgent");
|
Field c = WebSocketServlet.class.getDeclaredField("messageAgent");
|
||||||
RedkaleClassLoader.putReflectionField("messageAgent", c);
|
RedkaleClassLoader.putReflectionField(WebSocketServlet.class.getName(), c);
|
||||||
c.setAccessible(true);
|
c.setAccessible(true);
|
||||||
messageAgent = (MessageAgent) c.get(srcObj);
|
messageAgent = (MessageAgent) c.get(srcObj);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
|||||||
@@ -766,7 +766,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
try {
|
try {
|
||||||
Field instanceField = enClazz.getField("instance");
|
Field instanceField = enClazz.getField("instance");
|
||||||
if (instanceField.getType() == enClazz && Modifier.isStatic(instanceField.getModifiers())) {
|
if (instanceField.getType() == enClazz && Modifier.isStatic(instanceField.getModifiers())) {
|
||||||
RedkaleClassLoader.putReflectionField("instance", instanceField);
|
RedkaleClassLoader.putReflectionField(enClazz.getName(), instanceField);
|
||||||
encoder = (Encodeable) instanceField.get(null);
|
encoder = (Encodeable) instanceField.get(null);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -842,7 +842,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
try {
|
try {
|
||||||
Field instanceField = deClazz.getField("instance");
|
Field instanceField = deClazz.getField("instance");
|
||||||
if (instanceField.getType() == deClazz && Modifier.isStatic(instanceField.getModifiers())) {
|
if (instanceField.getType() == deClazz && Modifier.isStatic(instanceField.getModifiers())) {
|
||||||
RedkaleClassLoader.putReflectionField("instance", instanceField);
|
RedkaleClassLoader.putReflectionField(deClazz.getName(), instanceField);
|
||||||
decoder = (Decodeable) instanceField.get(null);
|
decoder = (Decodeable) instanceField.get(null);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public final class EnumSimpledCoder<R extends Reader, W extends Writer, E extend
|
|||||||
try {
|
try {
|
||||||
String fieldName = cev.value();
|
String fieldName = cev.value();
|
||||||
Field field = type.getDeclaredField(fieldName);
|
Field field = type.getDeclaredField(fieldName);
|
||||||
RedkaleClassLoader.putReflectionField(fieldName, field);
|
RedkaleClassLoader.putReflectionField(type.getName(), field);
|
||||||
char[] chs = fieldName.toCharArray();
|
char[] chs = fieldName.toCharArray();
|
||||||
chs[0] = Character.toUpperCase(chs[0]);
|
chs[0] = Character.toUpperCase(chs[0]);
|
||||||
String methodName = "get" + new String(chs);
|
String methodName = "get" + new String(chs);
|
||||||
|
|||||||
@@ -24,7 +24,9 @@ import org.redkale.mq.MessageConsumer;
|
|||||||
@Repeatable(DynForMessaged.DynForMessageds.class)
|
@Repeatable(DynForMessaged.DynForMessageds.class)
|
||||||
public @interface DynForMessaged {
|
public @interface DynForMessaged {
|
||||||
|
|
||||||
Class<? extends MessageConsumer> value();
|
String dynField();
|
||||||
|
|
||||||
|
Class<? extends MessageConsumer> consumer();
|
||||||
|
|
||||||
@Inherited
|
@Inherited
|
||||||
@Documented
|
@Documented
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package org.redkale.mq.spi;
|
package org.redkale.mq.spi;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@@ -335,24 +336,58 @@ public class MessageAsmMethodBoost extends AsmMethodBoost {
|
|||||||
String clzName = innerFullName.replace('/', '.');
|
String clzName = innerFullName.replace('/', '.');
|
||||||
Class clazz = classLoader.loadClass(clzName, bytes);
|
Class clazz = classLoader.loadClass(clzName, bytes);
|
||||||
RedkaleClassLoader.putDynClass(clzName, bytes, clazz);
|
RedkaleClassLoader.putDynClass(clzName, bytes, clazz);
|
||||||
|
RedkaleClassLoader.putReflectionPublicConstructors(clazz, clzName);
|
||||||
AnnotationVisitor av2 =
|
AnnotationVisitor av2 =
|
||||||
av1.visitAnnotation(null, org.redkale.asm.Type.getDescriptor(DynForMessaged.class));
|
av1.visitAnnotation(null, org.redkale.asm.Type.getDescriptor(DynForMessaged.class));
|
||||||
av2.visit("value", org.redkale.asm.Type.getType("L" + innerFullName + ";"));
|
av2.visit("dynField", getFieldName(innerFullName));
|
||||||
|
av2.visit("consumer", org.redkale.asm.Type.getType("L" + innerFullName + ";"));
|
||||||
av2.visitEnd();
|
av2.visitEnd();
|
||||||
});
|
});
|
||||||
av1.visitEnd();
|
av1.visitEnd();
|
||||||
av0.visitEnd();
|
av0.visitEnd();
|
||||||
|
String consumerDesc = org.redkale.asm.Type.getDescriptor(MessageConsumer.class);
|
||||||
|
consumerBytes.forEach((innerFullName, bytes) -> {
|
||||||
|
FieldVisitor fv = cw.visitField(ACC_PRIVATE, getFieldName(innerFullName), consumerDesc, null, null);
|
||||||
|
fv.visitEnd();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doConstructorMethod(
|
||||||
|
DynBytesClassLoader classLoader,
|
||||||
|
ClassWriter cw,
|
||||||
|
MethodVisitor mv,
|
||||||
|
String newDynName,
|
||||||
|
String fieldPrefix,
|
||||||
|
boolean remote) {
|
||||||
|
if (remote || Utility.isEmpty(consumerBytes)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String consumerDesc = org.redkale.asm.Type.getDescriptor(MessageConsumer.class);
|
||||||
|
consumerBytes.forEach((innerFullName, bytes) -> {
|
||||||
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
|
mv.visitTypeInsn(NEW, innerFullName);
|
||||||
|
mv.visitInsn(DUP);
|
||||||
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
|
mv.visitMethodInsn(INVOKESPECIAL, innerFullName, "<init>", "(L" + newDynName + ";)V", false);
|
||||||
|
mv.visitFieldInsn(PUTFIELD, newDynName, getFieldName(innerFullName), consumerDesc);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doInstance(DynBytesClassLoader classLoader, ResourceFactory resourceFactory, Object service) {
|
public void doInstance(DynBytesClassLoader classLoader, ResourceFactory resourceFactory, Object service) {
|
||||||
DynForMessaged[] dyns = service.getClass().getAnnotationsByType(DynForMessaged.class);
|
DynForMessaged[] dyns = service.getClass().getAnnotationsByType(DynForMessaged.class);
|
||||||
if (Utility.isNotEmpty(dyns)) {
|
if (Utility.isNotEmpty(dyns)) {
|
||||||
try {
|
try {
|
||||||
for (DynForMessaged item : dyns) {
|
for (DynForMessaged item : dyns) {
|
||||||
Class<? extends MessageConsumer> clazz = item.value();
|
// Class<? extends MessageConsumer> clazz = item.value();
|
||||||
MessageConsumer consumer = (MessageConsumer) clazz.getConstructors()[0].newInstance(service);
|
// MessageConsumer consumer = (MessageConsumer) clazz.getConstructors()[0].newInstance(service);
|
||||||
|
String fieldName = item.dynField();
|
||||||
|
Field field = service.getClass().getDeclaredField(fieldName);
|
||||||
|
RedkaleClassLoader.putReflectionField(service.getClass().getName(), field);
|
||||||
|
field.setAccessible(true);
|
||||||
|
MessageConsumer consumer = (MessageConsumer) field.get(service);
|
||||||
messageEngine.addMessageConsumer(consumer);
|
messageEngine.addMessageConsumer(consumer);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -360,4 +395,9 @@ public class MessageAsmMethodBoost extends AsmMethodBoost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getFieldName(String innerFullName) {
|
||||||
|
String simpleClassName = innerFullName.substring(innerFullName.lastIndexOf('$') + 1);
|
||||||
|
return "_dyn" + simpleClassName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,14 +168,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
dynClassBytesMap.forEach(action);
|
dynClassBytesMap.forEach(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionClass(String name) {
|
public static void putReflectionClass(String className) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap<>();
|
map = new LinkedHashMap<>();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
reflectionLock.unlock();
|
reflectionLock.unlock();
|
||||||
@@ -191,14 +191,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
serviceLoaderMap.forEach(action);
|
serviceLoaderMap.forEach(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionField(String name, Field field) {
|
public static void putReflectionField(String className, Field field) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
List<Map<String, Object>> list = (List) map.get("fields");
|
List<Map<String, Object>> list = (List) map.get("fields");
|
||||||
if (list == null) {
|
if (list == null) {
|
||||||
@@ -222,14 +222,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionMethod(String name, Method method) {
|
public static void putReflectionMethod(String className, Method method) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
List<Map<String, Object>> list = (List) map.get("methods");
|
List<Map<String, Object>> list = (List) map.get("methods");
|
||||||
if (list == null) {
|
if (list == null) {
|
||||||
@@ -259,14 +259,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionDeclaredConstructors(Class clazz, String name, Class... cts) {
|
public static void putReflectionDeclaredConstructors(Class clazz, String className, Class... cts) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
map.put("allDeclaredConstructors", true);
|
map.put("allDeclaredConstructors", true);
|
||||||
|
|
||||||
@@ -310,14 +310,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionPublicConstructors(Class clazz, String name) {
|
public static void putReflectionPublicConstructors(Class clazz, String className) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
map.put("allPublicConstructors", true);
|
map.put("allPublicConstructors", true);
|
||||||
|
|
||||||
@@ -356,14 +356,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionDeclaredMethods(String name) {
|
public static void putReflectionDeclaredMethods(String className) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
map.put("allDeclaredMethods", true);
|
map.put("allDeclaredMethods", true);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -371,14 +371,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionPublicMethods(String name) {
|
public static void putReflectionPublicMethods(String className) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
map.put("allPublicMethods", true);
|
map.put("allPublicMethods", true);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -386,14 +386,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionDeclaredFields(String name) {
|
public static void putReflectionDeclaredFields(String className) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
map.put("allDeclaredFields", true);
|
map.put("allDeclaredFields", true);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -401,14 +401,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionPublicFields(String name) {
|
public static void putReflectionPublicFields(String className) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
map.put("allPublicFields", true);
|
map.put("allPublicFields", true);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -416,14 +416,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionDeclaredClasses(String name) {
|
public static void putReflectionDeclaredClasses(String className) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
map.put("allDeclaredClasses", true);
|
map.put("allDeclaredClasses", true);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -431,14 +431,14 @@ public class RedkaleClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putReflectionPublicClasses(String name) {
|
public static void putReflectionPublicClasses(String className) {
|
||||||
reflectionLock.lock();
|
reflectionLock.lock();
|
||||||
try {
|
try {
|
||||||
Map<String, Object> map = reflectionMap.get(name);
|
Map<String, Object> map = reflectionMap.get(className);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new LinkedHashMap();
|
map = new LinkedHashMap();
|
||||||
map.put("name", name);
|
map.put("name", className);
|
||||||
reflectionMap.put(name, map);
|
reflectionMap.put(className, map);
|
||||||
}
|
}
|
||||||
map.put("allPublicClasses", true);
|
map.put("allPublicClasses", true);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user