This commit is contained in:
redkale
2024-10-11 22:02:58 +08:00
parent 3f172824b6
commit bb611413a5
26 changed files with 112 additions and 163 deletions

View File

@@ -92,7 +92,8 @@ public class AsmMethodBean {
return null; return null;
} }
Parameter[] ps = method == null ? null : method.getParameters(); Parameter[] ps = method == null ? null : method.getParameters();
String[] rs = new String[params.size()]; // params包含了throws, size()可能比ps.length大
String[] rs = new String[ps == null ? params.size() : ps.length];
for (int i = 0; i < rs.length; i++) { for (int i = 0; i < rs.length; i++) {
Param pann = ps == null ? null : ps[i].getAnnotation(Param.class); Param pann = ps == null ? null : ps[i].getAnnotation(Param.class);
rs[i] = pann == null ? params.get(i).getName() : pann.value(); rs[i] = pann == null ? params.get(i).getName() : pann.value();

View File

@@ -118,8 +118,7 @@ public abstract class AsmMethodBoost<T> {
* @param newDynName 动态新类名 * @param newDynName 动态新类名
* @param fieldPrefix 动态字段的前缀 * @param fieldPrefix 动态字段的前缀
*/ */
public void doAfterMethods( public void doAfterMethods(RedkaleClassLoader classLoader, ClassWriter cw, String newDynName, String fieldPrefix) {}
RedkaleClassLoader classLoader, ClassWriter cw, String newDynName, String fieldPrefix) {}
/** /**
* 处理所有动态方法后调用 * 处理所有动态方法后调用
@@ -437,14 +436,23 @@ public abstract class AsmMethodBoost<T> {
// 返回的List中参数列表可能会比方法参数量多因为方法内的临时变量也会存入list中 所以需要list的元素集合比方法的参数多 // 返回的List中参数列表可能会比方法参数量多因为方法内的临时变量也会存入list中 所以需要list的元素集合比方法的参数多
static Map<String, AsmMethodBean> getMethodParamNames(Map<String, AsmMethodBean> map, Class clazz) { static Map<String, AsmMethodBean> getMethodParamNames(Map<String, AsmMethodBean> map, Class clazz) {
String n = clazz.getName(); String n = clazz.getName();
InputStream in = clazz.getResourceAsStream(n.substring(n.lastIndexOf('.') + 1) + ".class"); byte[] bs = RedkaleClassLoader.getDynClassBytes(n);
if (in == null) { if (bs == null) {
return map; InputStream in = clazz.getResourceAsStream(n.substring(n.lastIndexOf('.') + 1) + ".class");
if (in == null) {
return map;
}
try {
bs = Utility.readBytesThenClose(in);
} catch (Exception e) {
// do nothing
}
} }
try { try {
new ClassReader(Utility.readBytesThenClose(in)) new ClassReader(bs).accept(new MethodParamClassVisitor(Opcodes.ASM6, clazz, map), 0);
.accept(new MethodParamClassVisitor(Opcodes.ASM6, clazz, map), 0); } catch (Exception e) {
} catch (Exception e) { // 无需理会 e.printStackTrace();
// do nothing
} }
Class superClass = clazz.getSuperclass(); Class superClass = clazz.getSuperclass();
if (superClass == null || superClass == Object.class) { // 接口的getSuperclass为null if (superClass == null || superClass == Object.class) { // 接口的getSuperclass为null

View File

@@ -143,15 +143,14 @@ class AppConfig {
if (cacheClasses == null) { if (cacheClasses == null) {
this.classLoader = new RedkaleClassLoader(currClassLoader); this.classLoader = new RedkaleClassLoader(currClassLoader);
} else { } else {
this.classLoader = new RedkaleClassLoader.RedkaleCacheClassLoader( this.classLoader = new RedkaleClassLoader.RedkaleCacheClassLoader(currClassLoader, cacheClasses);
RedkaleClassLoader.getRedkaleClassLoader(currClassLoader), cacheClasses);
} }
Thread.currentThread().setContextClassLoader(this.classLoader); Thread.currentThread().setContextClassLoader(this.classLoader);
} }
if (compileMode || this.classLoader instanceof RedkaleClassLoader.RedkaleCacheClassLoader) { if (compileMode || this.classLoader instanceof RedkaleClassLoader.RedkaleCacheClassLoader) {
this.serverClassLoader = this.classLoader; this.serverClassLoader = this.classLoader;
} else { } else {
//this.serverClassLoader = RedkaleClassLoader.getRedkaleClassLoader(this.classLoader); // this.serverClassLoader = RedkaleClassLoader.getRedkaleClassLoader(this.classLoader);
this.serverClassLoader = this.classLoader; this.serverClassLoader = this.classLoader;
} }
} }

View File

@@ -828,7 +828,7 @@ public final class Application {
ExecutorService clientWorkExecutor = this.workExecutor; ExecutorService clientWorkExecutor = this.workExecutor;
if (executorName.contains("VirtualExecutor") || executorName.contains("PerTaskExecutor")) { if (executorName.contains("VirtualExecutor") || executorName.contains("PerTaskExecutor")) {
executorLog.append(", clientWorkExecutor: [workExecutor]"); executorLog.append(", clientWorkExecutor: [virtual]");
} else { } else {
// 给所有client给一个新的默认ExecutorService // 给所有client给一个新的默认ExecutorService
int clientThreads = executorConf.getIntValue("clients", WorkThread.DEFAULT_WORK_POOL_SIZE); int clientThreads = executorConf.getIntValue("clients", WorkThread.DEFAULT_WORK_POOL_SIZE);

View File

@@ -71,8 +71,7 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
+ clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_" + clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_"
+ Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert + Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert
try { try {
Class clz = loader.findDynClass(newDynName.replace('/', '.')); Class newClazz = loader.loadClass(newDynName.replace('/', '.'));
Class newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz;
JsonDynEncoder resultEncoder = JsonDynEncoder resultEncoder =
(JsonDynEncoder) newClazz.getConstructor(JsonFactory.class, Type.class, ObjectEncoder.class) (JsonDynEncoder) newClazz.getConstructor(JsonFactory.class, Type.class, ObjectEncoder.class)
.newInstance(factory, clazz, selfObjEncoder); .newInstance(factory, clazz, selfObjEncoder);
@@ -681,7 +680,6 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes);
loader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors( RedkaleClassLoader.putReflectionDeclaredConstructors(
newClazz, newDynName.replace('/', '.'), JsonFactory.class, Type.class); newClazz, newDynName.replace('/', '.'), JsonFactory.class, Type.class);
try { try {

View File

@@ -74,8 +74,7 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
+ clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_" + clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_"
+ Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert + Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
Class newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
ProtobufDynEncoder resultEncoder = (ProtobufDynEncoder) ProtobufDynEncoder resultEncoder = (ProtobufDynEncoder)
newClazz.getConstructor(ProtobufFactory.class, Type.class, ProtobufObjectEncoder.class) newClazz.getConstructor(ProtobufFactory.class, Type.class, ProtobufObjectEncoder.class)
.newInstance(factory, clazz, selfObjEncoder); .newInstance(factory, clazz, selfObjEncoder);
@@ -322,7 +321,6 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<ProtobufDynEncoder> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<ProtobufDynEncoder> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
ProtobufDynEncoder resultEncoder = (ProtobufDynEncoder) ProtobufDynEncoder resultEncoder = (ProtobufDynEncoder)

View File

@@ -328,7 +328,6 @@ public class MessageAsmMethodBoost extends AsmMethodBoost {
consumerBytes.forEach((innerFullName, bytes) -> { consumerBytes.forEach((innerFullName, bytes) -> {
String clzName = innerFullName.replace('/', '.'); String clzName = innerFullName.replace('/', '.');
Class clazz = classLoader.loadClass(clzName, bytes); Class clazz = classLoader.loadClass(clzName, bytes);
classLoader.putDynClass(clzName, bytes, clazz);
RedkaleClassLoader.putReflectionPublicConstructors(clazz, clzName); 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));

View File

@@ -459,7 +459,6 @@ public class MessageModuleEngine extends ModuleEngine {
try { try {
String clzName = innerFullName.replace('/', '.'); String clzName = innerFullName.replace('/', '.');
Class<? extends MessageConsumer> clazz = classLoader.loadClass(clzName, bytes); Class<? extends MessageConsumer> clazz = classLoader.loadClass(clzName, bytes);
classLoader.putDynClass(clzName, bytes, clazz);
RedkaleClassLoader.putReflectionPublicConstructors(clazz, clzName); RedkaleClassLoader.putReflectionPublicConstructors(clazz, clzName);
MessageConsumer consumer = (MessageConsumer) clazz.getConstructors()[0].newInstance(service); MessageConsumer consumer = (MessageConsumer) clazz.getConstructors()[0].newInstance(service);
addMessageConsumer(consumer); addMessageConsumer(consumer);

View File

@@ -110,9 +110,7 @@ public class HttpContext extends Context {
+ handlerClass.getName().replace('.', '/').replace('$', '_'); + handlerClass.getName().replace('.', '/').replace('$', '_');
RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); return (Creator<H>) Creator.create(classLoader.loadClass(newDynName.replace('/', '.')));
Class newHandlerClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
return Creator.create(newHandlerClazz);
} catch (Throwable ex) { } catch (Throwable ex) {
// do nothing // do nothing
} }
@@ -214,7 +212,6 @@ public class HttpContext extends Context {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<CompletionHandler> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<CompletionHandler> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
return (Creator<H>) Creator.create(newClazz); return (Creator<H>) Creator.create(newClazz);
} }

View File

@@ -564,10 +564,10 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
final String newDynName = "org/redkaledyn/http/servlet/action/_DynHttpActionServlet__" final String newDynName = "org/redkaledyn/http/servlet/action/_DynHttpActionServlet__"
+ this.getClass().getName().replace('.', '_').replace('$', '_') + "__" + method.getName() + tmpps; + this.getClass().getName().replace('.', '_').replace('$', '_') + "__" + method.getName() + tmpps;
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); HttpServlet instance = (HttpServlet) classLoader
Class newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz; .loadClass(newDynName.replace('/', '.'))
HttpServlet instance = .getDeclaredConstructor()
(HttpServlet) newClazz.getDeclaredConstructor().newInstance(); .newInstance();
instance.getClass().getField("_factServlet").set(instance, this); instance.getClass().getField("_factServlet").set(instance, this);
return instance; return instance;
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -627,7 +627,6 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
HttpServlet instance = HttpServlet instance =
@@ -636,7 +635,7 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
field.set(instance, this); field.set(instance, this);
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), field); RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), field);
NonBlocking non = method.getAnnotation(NonBlocking.class); NonBlocking non = method.getAnnotation(NonBlocking.class);
instance._nonBlocking = typeNonBlocking ? (non == null ? typeNonBlocking : non.value()) : false; instance._nonBlocking = typeNonBlocking && (non == null ? typeNonBlocking : non.value());
return instance; return instance;
} catch (Exception ex) { } catch (Exception ex) {
throw new HttpException(ex); throw new HttpException(ex);

View File

@@ -455,10 +455,7 @@ public final class Rest {
final String newDynConsumerSimpleName = "_DynRestOnMessageConsumer"; final String newDynConsumerSimpleName = "_DynRestOnMessageConsumer";
final String newDynConsumerFullName = newDynName + "$" + newDynConsumerSimpleName; final String newDynConsumerFullName = newDynName + "$" + newDynConsumerSimpleName;
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.loadClass(newDynName.replace('/', '.'));
if (clz == null) {
clz = classLoader.loadClass(newDynName.replace('/', '.'));
}
T servlet = (T) clz.getDeclaredConstructor().newInstance(); T servlet = (T) clz.getDeclaredConstructor().newInstance();
Map<String, Annotation[]> msgclassToAnnotations = new HashMap<>(); Map<String, Annotation[]> msgclassToAnnotations = new HashMap<>();
for (int i = 0; i < messageMethods.size(); i++) { // _DyncXXXWebSocketMessage 子消息List for (int i = 0; i < messageMethods.size(); i++) { // _DyncXXXWebSocketMessage 子消息List
@@ -899,8 +896,7 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
Class cz = classLoader.loadClass((newDynSuperMessageFullName).replace('/', '.'), bytes); classLoader.loadClass((newDynSuperMessageFullName).replace('/', '.'), bytes);
classLoader.putDynClass((newDynSuperMessageFullName).replace('/', '.'), bytes, cz);
} }
if (wildcardMethod == null) { // _DynXXXWebSocketMessage class if (wildcardMethod == null) { // _DynXXXWebSocketMessage class
@@ -957,8 +953,7 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
Class cz = classLoader.loadClass(newDynMessageFullName.replace('/', '.'), bytes); classLoader.loadClass(newDynMessageFullName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynMessageFullName.replace('/', '.'), bytes, cz);
} }
{ // _DynXXXWebSocket class { // _DynXXXWebSocket class
@@ -1000,8 +995,7 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
Class cz = classLoader.loadClass(newDynWebSokcetFullName.replace('/', '.'), bytes); classLoader.loadClass(newDynWebSokcetFullName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynWebSokcetFullName.replace('/', '.'), bytes, cz);
} }
{ // _DynRestOnMessageConsumer class { // _DynRestOnMessageConsumer class
@@ -1108,14 +1102,12 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
Class cz = classLoader.loadClass(newDynConsumerFullName.replace('/', '.'), bytes); classLoader.loadClass(newDynConsumerFullName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynConsumerFullName.replace('/', '.'), bytes, cz);
} }
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
JsonFactory.root().loadDecoder(newClazz.getAnnotation(RestDyn.class).types()[2]); // 固定Message类 JsonFactory.root().loadDecoder(newClazz.getAnnotation(RestDyn.class).types()[2]); // 固定Message类
@@ -1301,10 +1293,7 @@ public final class Rest {
+ (namePostfix.isEmpty() ? "" : ("_" + namePostfix) + "DynServlet"); + (namePostfix.isEmpty() ? "" : ("_" + namePostfix) + "DynServlet");
try { try {
Class newClazz = classLoader.findClass(newDynName.replace('/', '.')); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
if (newClazz == null) {
newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
}
T obj = (T) newClazz.getDeclaredConstructor().newInstance(); T obj = (T) newClazz.getDeclaredConstructor().newInstance();
final String defModuleName = getWebModuleNameLowerCase(serviceType); final String defModuleName = getWebModuleNameLowerCase(serviceType);
@@ -4234,16 +4223,15 @@ public final class Rest {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
classLoader.addDynClass(newDynName.replace('/', '.'), bytes); classLoader.addDynClass(newDynName.replace('/', '.'), bytes);
try { try {
Class<?> newClazz = classLoader.findClass(newDynName.replace('/', '.')); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
innerClassBytesMap.forEach((n, bs) -> { innerClassBytesMap.forEach((n, bs) -> {
try { try {
classLoader.putDynClass(n, bs, classLoader.findClass(n)); classLoader.loadClass(n, bs);
RedkaleClassLoader.putReflectionClass(n); RedkaleClassLoader.putReflectionClass(n);
} catch (Exception e) { } catch (Exception e) {
throw new RestException(e); throw new RestException(e);
} }
}); });
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
for (java.lang.reflect.Type t : retvalTypes) { for (java.lang.reflect.Type t : retvalTypes) {
JsonFactory.root().loadEncoder(t); JsonFactory.root().loadEncoder(t);

View File

@@ -529,12 +529,9 @@ public abstract class Sncp {
} }
// if (Utility.inNativeImage() || methodBoost == null) { // 加强动态时不能重复加载 // if (Utility.inNativeImage() || methodBoost == null) { // 加强动态时不能重复加载
try { try {
final String newDynClass = newDynName.replace('/', '.'); return (Class<T>)classLoader.loadClass(newDynName.replace('/', '.'));
return (Class<T>) classLoader.findClass(newDynClass);
} catch (ClassNotFoundException e) {
// do nothing
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); // do nothing
} }
// } // }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@@ -602,8 +599,6 @@ public abstract class Sncp {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
final String newDynClass = newDynName.replace('/', '.'); final String newDynClass = newDynName.replace('/', '.');
Class<?> newClazz = classLoader.loadClass(newDynClass, bytes); Class<?> newClazz = classLoader.loadClass(newDynClass, bytes);
classLoader.putDynClass(newDynClass, bytes, newClazz);
RedkaleClassLoader.putReflectionPublicClasses(newDynClass); RedkaleClassLoader.putReflectionPublicClasses(newDynClass);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynClass); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynClass);
try { try {
@@ -953,9 +948,7 @@ public abstract class Sncp {
newDynName += "_" + (normal ? name : hash(name)); newDynName += "_" + (normal ? name : hash(name));
} }
try { try {
final String newDynClass = newDynName.replace('/', '.'); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
Class clz = classLoader.findDynClass(newDynClass);
Class newClazz = clz == null ? classLoader.loadClass(newDynClass) : clz;
T service = (T) newClazz.getDeclaredConstructor().newInstance(); T service = (T) newClazz.getDeclaredConstructor().newInstance();
{ {
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_conf"); Field c = newClazz.getDeclaredField(FIELDPREFIX + "_conf");
@@ -1237,7 +1230,6 @@ public abstract class Sncp {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
final String newDynClass = newDynName.replace('/', '.'); final String newDynClass = newDynName.replace('/', '.');
Class<?> newClazz = classLoader.loadClass(newDynClass, bytes); Class<?> newClazz = classLoader.loadClass(newDynClass, bytes);
classLoader.putDynClass(newDynClass, bytes, newClazz);
RedkaleClassLoader.putReflectionPublicConstructors(newClazz, newDynClass); RedkaleClassLoader.putReflectionPublicConstructors(newClazz, newDynClass);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynClass); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynClass);
try { try {

View File

@@ -359,8 +359,7 @@ public abstract class SncpActionServlet extends SncpServlet {
RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
Class<?> newClazz = null; Class<?> newClazz = null;
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
} catch (Throwable ex) { } catch (Throwable ex) {
// do nothing // do nothing
} }
@@ -371,7 +370,8 @@ public abstract class SncpActionServlet extends SncpServlet {
TypeToken.getGenericType(method.getGenericReturnType(), serviceClass); TypeToken.getGenericType(method.getGenericReturnType(), serviceClass);
final Class[] paramClasses = method.getParameterTypes(); final Class[] paramClasses = method.getParameterTypes();
java.lang.reflect.Type paramComposeBeanType0 = SncpRemoteAction.createParamComposeBeanType(classLoader, serviceImplClass, method, actionid, originalParamTypes, paramClasses); java.lang.reflect.Type paramComposeBeanType0 = SncpRemoteAction.createParamComposeBeanType(
classLoader, serviceImplClass, method, actionid, originalParamTypes, paramClasses);
if (paramComposeBeanType0 != null && paramComposeBeanType0 == originalParamTypes[0]) { if (paramComposeBeanType0 != null && paramComposeBeanType0 == originalParamTypes[0]) {
paramComposeBeanType0 = null; paramComposeBeanType0 = null;
} }
@@ -712,7 +712,6 @@ public abstract class SncpActionServlet extends SncpServlet {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {

View File

@@ -56,8 +56,7 @@ public interface SncpAsyncHandler<V, A> extends CompletionHandler<V, A> {
+ "__" + handlerClass.getName().replace('.', '/').replace('$', '_'); + "__" + handlerClass.getName().replace('.', '/').replace('$', '_');
RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); Class newHandlerClazz = classLoader.loadClass(newDynName.replace('/', '.'));
Class newHandlerClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
return (Creator<SncpAsyncHandler>) Creator.create(newHandlerClazz); return (Creator<SncpAsyncHandler>) Creator.create(newHandlerClazz);
} catch (Throwable ex) { } catch (Throwable ex) {
// do nothing // do nothing
@@ -207,7 +206,6 @@ public interface SncpAsyncHandler<V, A> extends CompletionHandler<V, A> {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
return (Creator<SncpAsyncHandler>) Creator.create(newClazz); return (Creator<SncpAsyncHandler>) Creator.create(newClazz);
}) })
.create(factHandler); .create(factHandler);

View File

@@ -273,9 +273,7 @@ public final class SncpRemoteAction {
final String newDynName = "org/redkaledyn/sncp/servlet/action/_DynSncpActionParamBean_" final String newDynName = "org/redkaledyn/sncp/servlet/action/_DynSncpActionParamBean_"
+ resourceType.getSimpleName() + "_" + method.getName() + "_" + actionid; + resourceType.getSimpleName() + "_" + method.getName() + "_" + actionid;
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); return classLoader.loadClass(newDynName.replace('/', '.'));
Class<?> newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
return newClazz;
} catch (Throwable ex) { } catch (Throwable ex) {
// do nothing // do nothing
} }
@@ -328,7 +326,6 @@ public final class SncpRemoteAction {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
Creator.load(newClazz, 1); // 只一个Object[]参数 Creator.load(newClazz, 1); // 只一个Object[]参数
ProtobufFactory.root().loadDecoder(newClazz); ProtobufFactory.root().loadDecoder(newClazz);

View File

@@ -84,8 +84,8 @@ public abstract class EntityFullFunc<T> {
final String newDynName = "org/redkaledyn/source/_Dyn" + EntityFullFunc.class.getSimpleName() + "__" final String newDynName = "org/redkaledyn/source/_Dyn" + EntityFullFunc.class.getSimpleName() + "__"
+ entityType.getName().replace('.', '_').replace('$', '_'); + entityType.getName().replace('.', '_').replace('$', '_');
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); return (EntityFullFunc) classLoader
return (EntityFullFunc) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz) .loadClass(newDynName.replace('/', '.'))
.getConstructor(Class.class, Creator.class, Attribute[].class) .getConstructor(Class.class, Creator.class, Attribute[].class)
.newInstance(entityType, creator, attrs); .newInstance(entityType, creator, attrs);
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -794,7 +794,6 @@ public abstract class EntityFullFunc<T> {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<EntityFullFunc> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<EntityFullFunc> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
return newClazz.getConstructor(Class.class, Creator.class, Attribute[].class) return newClazz.getConstructor(Class.class, Creator.class, Attribute[].class)

View File

@@ -66,8 +66,7 @@ public final class DataSqlMapperBuilder {
final String newDynName = "org/redkaledyn/source/mapper/_DynDataSqlMapper_" final String newDynName = "org/redkaledyn/source/mapper/_DynDataSqlMapper_"
+ mapperType.getName().replace('.', '_').replace('$', '_'); + mapperType.getName().replace('.', '_').replace('$', '_');
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
Class newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
M mapper = (M) newClazz.getDeclaredConstructor().newInstance(); M mapper = (M) newClazz.getDeclaredConstructor().newInstance();
{ // DataSqlSource { // DataSqlSource
Field c = newClazz.getDeclaredField("_source"); Field c = newClazz.getDeclaredField("_source");
@@ -364,7 +363,6 @@ public final class DataSqlMapperBuilder {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionPublicConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionPublicConstructors(newClazz, newDynName.replace('/', '.'));
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {

View File

@@ -1021,8 +1021,8 @@ public interface Attribute<T, F> {
final String newDynName = "org/redkaledyn/attribute/" + pkgname + "_Dyn" + Attribute.class.getSimpleName() final String newDynName = "org/redkaledyn/attribute/" + pkgname + "_Dyn" + Attribute.class.getSimpleName()
+ "__" + clzname + "__" + fieldkey.substring(fieldkey.indexOf('.') + 1); + "__" + clzname + "__" + fieldkey.substring(fieldkey.indexOf('.') + 1);
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); Attribute rs = (Attribute) classLoader
Attribute rs = (Attribute) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz) .loadClass(newDynName.replace('/', '.'))
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
java.lang.reflect.Field _gtype = rs.getClass().getDeclaredField("_gtype"); java.lang.reflect.Field _gtype = rs.getClass().getDeclaredField("_gtype");
@@ -1234,7 +1234,6 @@ public interface Attribute<T, F> {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<Attribute> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<Attribute> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
Attribute rs = newClazz.getDeclaredConstructor().newInstance(); Attribute rs = newClazz.getDeclaredConstructor().newInstance();

View File

@@ -688,8 +688,8 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
: ("__" + destClass.getName().replace('.', '_').replace('$', '_'))) : ("__" + destClass.getName().replace('.', '_').replace('$', '_')))
+ (extendInfo.length() == 0 ? "" : Utility.md5Hex(extendInfo.toString())); + (extendInfo.length() == 0 ? "" : Utility.md5Hex(extendInfo.toString()));
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); return (Copier) classLoader
return (Copier) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz) .loadClass(newDynName.replace('/', '.'))
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -698,10 +698,7 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES); ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
FieldVisitor fv;
MethodVisitor mv; MethodVisitor mv;
AnnotationVisitor av0;
cw.visit( cw.visit(
V11, V11,
ACC_PUBLIC + ACC_FINAL + ACC_SUPER, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
@@ -1338,7 +1335,6 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
return (Copier) newClazz.getDeclaredConstructor().newInstance(); return (Copier) newClazz.getDeclaredConstructor().newInstance();

View File

@@ -296,8 +296,8 @@ public interface Creator<T> {
final String newDynName = "org/redkaledyn/creator/_Dyn" + Creator.class.getSimpleName() + "__" final String newDynName = "org/redkaledyn/creator/_Dyn" + Creator.class.getSimpleName() + "__"
+ clazz.getName().replace('.', '_').replace('$', '_') + (paramCount < 0 ? "" : ("_" + paramCount)); + clazz.getName().replace('.', '_').replace('$', '_') + (paramCount < 0 ? "" : ("_" + paramCount));
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); return (Creator) classLoader
return (Creator) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz) .loadClass(newDynName.replace('/', '.'))
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -569,7 +569,6 @@ public interface Creator<T> {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
try { try {
Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
return (Creator) newClazz.getDeclaredConstructor().newInstance(); return (Creator) newClazz.getDeclaredConstructor().newInstance();
} catch (Exception ex) { } catch (Exception ex) {

View File

@@ -115,55 +115,34 @@ public abstract class Flows {
if (!"executable".equals(System.getProperty("org.graalvm.nativeimage.kind"))) { // not native-image if (!"executable".equals(System.getProperty("org.graalvm.nativeimage.kind"))) { // not native-image
try { try {
// //
reactorMonoClass0 = RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
Thread.currentThread().getContextClassLoader().loadClass("reactor.core.publisher.Mono"); reactorMonoClass0 = classLoader.loadClass("reactor.core.publisher.Mono");
Class<Function<Object, CompletableFuture>> monoFuncClass = null; Class<Function<Object, CompletableFuture>> monoFuncClass = null;
try { try {
monoFuncClass = (Class) Thread.currentThread() monoFuncClass = classLoader.loadClass("org.redkale.util.AnonymousMonoFutureFunction");
.getContextClassLoader()
.loadClass("org.redkale.util.AnonymousMonoFutureFunction");
} catch (Throwable t) { } catch (Throwable t) {
// do nothing // do nothing
} }
if (monoFuncClass == null) { if (monoFuncClass == null) {
byte[] classBytes = hexToBin(FUNCTION_MONO_FUTRUE_BINARY); byte[] classBytes = hexToBin(FUNCTION_MONO_FUTRUE_BINARY);
monoFuncClass = (Class<Function<Object, CompletableFuture>>) monoFuncClass = classLoader.loadClass("org.redkale.util.AnonymousMonoFutureFunction", classBytes);
new ClassLoader() {
public final Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
}.loadClass("org.redkale.util.AnonymousMonoFutureFunction", classBytes);
RedkaleClassLoader.getRedkaleClassLoader()
.putDynClass(monoFuncClass.getName(), classBytes, monoFuncClass);
} }
RedkaleClassLoader.putReflectionDeclaredConstructors(monoFuncClass, monoFuncClass.getName()); RedkaleClassLoader.putReflectionDeclaredConstructors(monoFuncClass, monoFuncClass.getName());
reactorMonoFunction0 = (Function<Object, CompletableFuture>) reactorMonoFunction0 = monoFuncClass.getDeclaredConstructor().newInstance();
monoFuncClass.getDeclaredConstructor().newInstance();
// //
reactorFluxClass0 = reactorFluxClass0 = classLoader.loadClass("reactor.core.publisher.Flux");
Thread.currentThread().getContextClassLoader().loadClass("reactor.core.publisher.Flux");
Class<Function<Object, CompletableFuture>> fluxFuncClass = null; Class<Function<Object, CompletableFuture>> fluxFuncClass = null;
try { try {
fluxFuncClass = (Class) Thread.currentThread() fluxFuncClass = classLoader.loadClass("org.redkale.util.AnonymousFluxFutureFunction");
.getContextClassLoader()
.loadClass("org.redkale.util.AnonymousFluxFutureFunction");
} catch (Throwable t) { } catch (Throwable t) {
// do nothing // do nothing
} }
if (fluxFuncClass == null) { if (fluxFuncClass == null) {
byte[] classBytes = hexToBin(FUNCTION_FLUX_FUTRUE_BINARY); byte[] classBytes = hexToBin(FUNCTION_FLUX_FUTRUE_BINARY);
fluxFuncClass = (Class<Function<Object, CompletableFuture>>) fluxFuncClass = classLoader.loadClass("org.redkale.util.AnonymousFluxFutureFunction", classBytes);
new ClassLoader() {
public final Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
}.loadClass("org.redkale.util.AnonymousFluxFutureFunction", classBytes);
RedkaleClassLoader.getRedkaleClassLoader()
.putDynClass(fluxFuncClass.getName(), classBytes, fluxFuncClass);
} }
RedkaleClassLoader.putReflectionDeclaredConstructors(fluxFuncClass, fluxFuncClass.getName()); RedkaleClassLoader.putReflectionDeclaredConstructors(fluxFuncClass, fluxFuncClass.getName());
reactorFluxFunction0 = (Function<Object, CompletableFuture>) reactorFluxFunction0 = fluxFuncClass.getDeclaredConstructor().newInstance();
fluxFuncClass.getDeclaredConstructor().newInstance();
} catch (Throwable t) { } catch (Throwable t) {
// do nothing // do nothing
} }

View File

@@ -235,8 +235,8 @@ class Inners {
final String newDynName = "org/redkaledyn/creator/_DynArrayFunction__" final String newDynName = "org/redkaledyn/creator/_DynArrayFunction__"
+ clazz.getName().replace('.', '_').replace('$', '_'); + clazz.getName().replace('.', '_').replace('$', '_');
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); return (IntFunction) classLoader
return (IntFunction) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz) .loadClass(newDynName.replace('/', '.'))
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -284,7 +284,6 @@ class Inners {
final byte[] bytes = cw.toByteArray(); final byte[] bytes = cw.toByteArray();
try { try {
Class<?> resultClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> resultClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, resultClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.'));
return (IntFunction<T[]>) resultClazz.getDeclaredConstructor().newInstance(); return (IntFunction<T[]>) resultClazz.getDeclaredConstructor().newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {

View File

@@ -99,8 +99,8 @@ public interface Invoker<C, R> {
final String newDynName = "org/redkaledyn/invoker/_Dyn" + Invoker.class.getSimpleName() + "_" final String newDynName = "org/redkaledyn/invoker/_Dyn" + Invoker.class.getSimpleName() + "_"
+ clazz.getName().replace('.', '_').replace('$', '_') + "_" + method.getName() + sbpts; + clazz.getName().replace('.', '_').replace('$', '_') + "_" + method.getName() + sbpts;
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); return (Invoker<C, T>) classLoader
return (Invoker<C, T>) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz) .loadClass(newDynName.replace('/', '.'))
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -204,13 +204,9 @@ public interface Invoker<C, R> {
mv.visitEnd(); mv.visitEnd();
} }
cw.visitEnd(); cw.visitEnd();
final byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> resultClazz = null;
try { try {
if (resultClazz == null) { Class<?> resultClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
resultClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
}
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, resultClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.'));
return (Invoker<C, T>) resultClazz.getDeclaredConstructor().newInstance(); return (Invoker<C, T>) resultClazz.getDeclaredConstructor().newInstance();
} catch (Exception ex) { } catch (Exception ex) {

View File

@@ -175,28 +175,6 @@ public class RedkaleClassLoader extends URLClassLoader {
} }
} }
static void putDynClass0(String name, byte[] bs, Class clazz) {
Objects.requireNonNull(name);
Objects.requireNonNull(bs);
Objects.requireNonNull(clazz);
allDynClassTypeMap.put(name, clazz);
allDynClassBytesMap.put(name, bs);
}
public void putDynClass(String name, byte[] bs, Class clazz) {
Objects.requireNonNull(name);
Objects.requireNonNull(bs);
Objects.requireNonNull(clazz);
dynClassTypeMap.put(name, clazz);
dynClassBytesMap.put(name, bs);
allDynClassTypeMap.put(name, clazz);
allDynClassBytesMap.put(name, bs);
}
public Class findDynClass(String name) {
return dynClassTypeMap.get(name);
}
public static void forEachDynClass(BiConsumer<String, byte[]> action) { public static void forEachDynClass(BiConsumer<String, byte[]> action) {
allDynClassBytesMap.forEach(action); allDynClassBytesMap.forEach(action);
} }
@@ -479,6 +457,10 @@ public class RedkaleClassLoader extends URLClassLoader {
} }
} }
public static byte[] getDynClassBytes(String clazzName) {
return allDynClassBytesMap.get(clazzName);
}
// https://www.graalvm.org/reference-manual/native-image/Reflection/#manual-configuration // https://www.graalvm.org/reference-manual/native-image/Reflection/#manual-configuration
private static Map<String, Object> createMap(String name, Class... cts) { private static Map<String, Object> createMap(String name, Class... cts) {
Map<String, Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
@@ -495,8 +477,44 @@ public class RedkaleClassLoader extends URLClassLoader {
reflectionMap.forEach(action); reflectionMap.forEach(action);
} }
static void putDynClass0(String name, byte[] bs, Class clazz) {
Objects.requireNonNull(name);
Objects.requireNonNull(bs);
Objects.requireNonNull(clazz);
allDynClassTypeMap.put(name, clazz);
allDynClassBytesMap.put(name, bs);
}
public void putDynClass2(String name, byte[] bs, Class clazz) {
Objects.requireNonNull(name);
Objects.requireNonNull(bs);
Objects.requireNonNull(clazz);
dynClassTypeMap.put(name, clazz);
dynClassBytesMap.put(name, bs);
allDynClassTypeMap.put(name, clazz);
allDynClassBytesMap.put(name, bs);
}
public Class loadClass(String name, byte[] bs) {
Class clz = defineClass(name, bs, 0, bs.length);
dynClassTypeMap.put(name, clz);
dynClassBytesMap.put(name, bs);
allDynClassTypeMap.put(name, clz);
allDynClassBytesMap.put(name, bs);
return clz;
}
@Override @Override
public Class<?> findClass(String name) throws ClassNotFoundException { public Class loadClass(String name) throws ClassNotFoundException {
Class clazz = allDynClassTypeMap.get(name);
if (clazz != null) {
return clazz;
}
return super.loadClass(name);
}
@Override
public Class findClass(String name) throws ClassNotFoundException {
byte[] classData = dynClassBytesMap.get(name); byte[] classData = dynClassBytesMap.get(name);
if (classData == null) { if (classData == null) {
Class clazz = dynClassTypeMap.get(name); Class clazz = dynClassTypeMap.get(name);
@@ -508,10 +526,6 @@ public class RedkaleClassLoader extends URLClassLoader {
return super.defineClass(name, classData, 0, classData.length); return super.defineClass(name, classData, 0, classData.length);
} }
public Class loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
public void forEachCacheClass(Consumer<String> action) { // getAllURLs返回URL_NONE时需要重载此方法 public void forEachCacheClass(Consumer<String> action) { // getAllURLs返回URL_NONE时需要重载此方法
if (this.getParent() instanceof RedkaleClassLoader) { if (this.getParent() instanceof RedkaleClassLoader) {
((RedkaleClassLoader) getParent()).forEachCacheClass(action); ((RedkaleClassLoader) getParent()).forEachCacheClass(action);

View File

@@ -72,8 +72,8 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
+ "__" + destClass.getName().replace('.', '_').replace('$', '_') + "__" + destClass.getName().replace('.', '_').replace('$', '_')
+ "__" + srcClass.getName().replace('.', '_').replace('$', '_'); + "__" + srcClass.getName().replace('.', '_').replace('$', '_');
try { try {
Class clz = classLoader.findDynClass(newDynName.replace('/', '.')); return (Reproduce) classLoader
return (Reproduce) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz) .loadClass(newDynName.replace('/', '.'))
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -232,7 +232,6 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
return (Reproduce) newClazz.getDeclaredConstructor().newInstance(); return (Reproduce) newClazz.getDeclaredConstructor().newInstance();

View File

@@ -716,7 +716,6 @@ public abstract class TypeToken<T> {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionPublicFields(newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionPublicFields(newDynName.replace('/', '.'));
try { try {
return newClazz.getField("field").getGenericType(); return newClazz.getField("field").getGenericType();