This commit is contained in:
redkale
2024-10-11 01:04:57 +08:00
parent 335933a0f4
commit 1dc07638e5
6 changed files with 60 additions and 18 deletions

View File

@@ -66,7 +66,10 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
}
}
final Map<String, AccessibleObject> mixedNames = mixedNames0;
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (String.class.getClassLoader() != clazz.getClassLoader()) {
loader = clazz.getClassLoader();
}
final String newDynName = "org/redkaledyn/convert/json/_Dyn" + JsonDynEncoder.class.getSimpleName() + "__"
+ clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_"
+ Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert

View File

@@ -4,6 +4,7 @@
*/
package org.redkale.convert.pb;
import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.math.BigDecimal;
@@ -260,6 +261,28 @@ public abstract class ProtobufCoders {
}
// ------------------------------------- simple object -------------------------------------
public static class ProtobufFileSimpledCoder extends FileSimpledCoder<ProtobufReader, ProtobufWriter>
implements ProtobufEncodeable<ProtobufWriter, File> {
public static final ProtobufFileSimpledCoder instance = new ProtobufFileSimpledCoder();
@Override
public int computeSize(ProtobufWriter out, int tagSize, File value) {
if (value == null) {
return 0;
}
String val = value.getPath();
int len = Utility.encodeUTF8Length(val);
return len + ProtobufFactory.computeSInt32SizeNoTag(len);
}
@Override
public Type getType() {
return File.class;
}
}
public static class ProtobufNumberSimpledCoder extends NumberSimpledCoder<ProtobufReader, ProtobufWriter>
implements ProtobufPrimitivable<Number>, ProtobufEncodeable<ProtobufWriter, Number> {

View File

@@ -68,7 +68,10 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
}
}
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (String.class.getClassLoader() != clazz.getClassLoader()) {
loader = clazz.getClassLoader();
}
final String newDynName = "org/redkaledyn/convert/pb/_Dyn" + ProtobufDynEncoder.class.getSimpleName() + "__"
+ clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_"
+ Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert

View File

@@ -5,6 +5,7 @@
*/
package org.redkale.convert.pb;
import java.io.File;
import java.io.Serializable;
import java.lang.reflect.*;
import java.math.BigDecimal;
@@ -85,6 +86,7 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
this.register(InetSocketAddress.class, ProtobufCoders.ProtobufInetSocketAddressSimpledCoder.instance);
this.register(LongAdder.class, ProtobufCoders.ProtobufLongAdderSimpledCoder.instance);
this.register(Uint128.class, ProtobufCoders.ProtobufUint128SimpledCoder.instance);
this.register(File.class, ProtobufCoders.ProtobufFileSimpledCoder.instance);
this.register(Serializable.class, ProtobufCoders.ProtobufSerializableSimpledCoder.instance);
this.register(boolean[].class, ProtobufCoders.ProtobufBoolArraySimpledCoder.instance);

View File

@@ -74,6 +74,7 @@ public abstract class SncpActionServlet extends SncpServlet {
this.actionid = actionid;
this.method = method;
this.paramComposeBeanType = SncpRemoteAction.createParamComposeBeanType(
Thread.currentThread().getContextClassLoader(),
Sncp.getServiceType(service),
method,
actionid,
@@ -355,13 +356,14 @@ public abstract class SncpActionServlet extends SncpServlet {
final boolean boolReturnTypeFuture = Future.class.isAssignableFrom(method.getReturnType());
final String newDynName = "org/redkaledyn/sncp/servlet/action/_DynSncpActionServlet__"
+ resourceType.getSimpleName() + "_" + method.getName() + "_" + actionid;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (String.class.getClassLoader() != serviceClass.getClassLoader()) {
loader = serviceClass.getClassLoader();
}
Class<?> newClazz = null;
try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.'));
newClazz = clz == null
? Thread.currentThread().getContextClassLoader().loadClass(newDynName.replace('/', '.'))
: clz;
newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz;
} catch (Throwable ex) {
// do nothing
}
@@ -373,7 +375,7 @@ public abstract class SncpActionServlet extends SncpServlet {
final Class[] paramClasses = method.getParameterTypes();
java.lang.reflect.Type paramComposeBeanType0 = SncpRemoteAction.createParamComposeBeanType(
serviceImplClass, method, actionid, originalParamTypes, paramClasses);
loader, serviceImplClass, method, actionid, originalParamTypes, paramClasses);
if (paramComposeBeanType0 != null && paramComposeBeanType0 == originalParamTypes[0]) {
paramComposeBeanType0 = null;
}
@@ -621,7 +623,7 @@ public abstract class SncpActionServlet extends SncpServlet {
paramComposeBeanClass.getName().replace('.', '/');
mv.visitVarInsn(ALOAD, 3); // convert
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, "paramComposeType", reflectTypeDesc);
mv.visitFieldInsn(GETFIELD, newDynName, "paramComposeBeanType", reflectTypeDesc);
mv.visitVarInsn(ALOAD, 4); // reader
mv.visitMethodInsn(INVOKEVIRTUAL, convertName, "convertFrom", convertFromDesc, false);
mv.visitTypeInsn(CHECKCAST, paramComposeBeanName);
@@ -711,7 +713,7 @@ public abstract class SncpActionServlet extends SncpServlet {
cw.visitEnd();
byte[] bytes = cw.toByteArray();
newClazz = new ClassLoader(serviceClass.getClassLoader()) {
newClazz = new ClassLoader(loader) {
public final Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}

View File

@@ -98,10 +98,11 @@ public final class SncpRemoteAction {
this.returnObjectType = rt == void.class || rt == Void.class ? null : rt;
this.paramTypes = TypeToken.getGenericType(method.getGenericParameterTypes(), serviceImplClass);
this.paramClasses = method.getParameterTypes();
Type pt = createParamComposeBeanType(serviceImplClass, method, actionid, paramTypes, paramClasses);
Type pt = createParamComposeBeanType(
serviceImplClass.getClassLoader(), serviceImplClass, method, actionid, paramTypes, paramClasses);
this.paramComposeBeanType = pt;
this.paramComposeBeanCreator =
(pt == null || pt == paramTypes[0]) ? null : Creator.create(TypeToken.typeToClass(pt), 1);
(pt == null || pt == paramTypes[0]) ? null : Creator.load(TypeToken.typeToClass(pt), 1);
this.method = method;
Annotation[][] anns = method.getParameterAnnotations();
int topicAddrIndex = -1;
@@ -241,7 +242,12 @@ public final class SncpRemoteAction {
}
public static Type createParamComposeBeanType(
Class resourceType, Method method, Uint128 actionid, Type[] paramTypes, Class[] paramClasses) {
ClassLoader loader,
Class resourceType,
Method method,
Uint128 actionid,
Type[] paramTypes,
Class[] paramClasses) {
if (paramTypes == null || paramTypes.length == 0) {
return null;
}
@@ -250,15 +256,18 @@ public final class SncpRemoteAction {
}
// 动态生成组合JavaBean类
final Class serviceClass = resourceType.getClass();
if (loader == null) {
loader = Thread.currentThread().getContextClassLoader();
if (String.class.getClassLoader() != resourceType.getClassLoader()) {
loader = resourceType.getClassLoader();
}
}
final String columnDesc = org.redkale.asm.Type.getDescriptor(ConvertColumn.class);
final String newDynName = "org/redkaledyn/sncp/servlet/action/_DynSncpActionParamBean_"
+ resourceType.getSimpleName() + "_" + method.getName() + "_" + actionid;
try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.'));
Class<?> newClazz = clz == null
? Thread.currentThread().getContextClassLoader().loadClass(newDynName.replace('/', '.'))
: clz;
Class<?> newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz;
return newClazz;
} catch (Throwable ex) {
// do nothing
@@ -311,14 +320,14 @@ public final class SncpRemoteAction {
cw.visitEnd();
byte[] bytes = cw.toByteArray();
Class newClazz = new ClassLoader(Thread.currentThread().getContextClassLoader()) {
Class newClazz = new ClassLoader(loader) {
public final Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
}.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
Creator.load(newClazz);
Creator.load(newClazz, 1); // 只一个Object[]参数
ProtobufFactory.root().loadDecoder(newClazz);
ProtobufFactory.root().loadEncoder(newClazz);
return newClazz;