diff --git a/src/main/java/org/redkale/util/Creator.java b/src/main/java/org/redkale/util/Creator.java
index 847e3f8ae..8e6b55c2e 100644
--- a/src/main/java/org/redkale/util/Creator.java
+++ b/src/main/java/org/redkale/util/Creator.java
@@ -1,825 +1,812 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-package org.redkale.util;
-
-import java.io.*;
-import java.lang.reflect.*;
-import java.net.*;
-import java.util.AbstractMap.SimpleEntry;
-import java.util.*;
-import java.util.concurrent.*;
-import java.util.function.*;
-import java.util.logging.*;
-import java.util.stream.Stream;
-import org.redkale.annotation.ConstructorParameters;
-import org.redkale.asm.*;
-import static org.redkale.asm.Opcodes.*;
-import org.redkale.asm.Type;
-
-/**
- *
- * 实现一个类的构造方法。 代替低效的反射实现方式。 不支持数组类。
- * 常见的无参数的构造函数类都可以自动生成Creator, 对应自定义的类可以提供一个静态构建Creator方法。
- * 例如:
- *
- * public class Record {
- *
- * private final int id;
- *
- * private String name;
- *
- * Record(int id, String name) {
- * this.id = id;
- * this.name = name;
- * }
- *
- * private static Creator createCreator() {
- * return new Creator<Record>() {
- * @Override
- * @ConstructorParameters({"id", "name"})
- * public Record create(Object... params) {
- * if(params[0] == null) params[0] = 0;
- * return new Record((Integer) params[0], (String) params[1]);
- * }
- * };
- * }
- * }
- *
- *
- * 或者:
- *
- * public class Record {
- *
- * private final int id;
- *
- * private String name;
- *
- * @ConstructorParameters({"id", "name"})
- * public Record(int id, String name) {
- * this.id = id;
- * this.name = name;
- * }
- * }
- *
- *
- *
- * 详情见: https://redkale.org
- *
- * @author zhangjx
- * @param 构建对象的数据类型
- */
-public interface Creator {
-
- @SuppressWarnings("unchecked")
- static class CreatorInner {
-
- static final Logger logger = Logger.getLogger(Creator.class.getSimpleName());
-
- static final Map creatorCacheMap = new HashMap<>();
-
- static final Map arrayCacheMap = new ConcurrentHashMap<>();
-
- static {
- creatorCacheMap.put(Object.class, p -> new Object());
- creatorCacheMap.put(ArrayList.class, p -> new ArrayList<>());
- creatorCacheMap.put(HashMap.class, p -> new HashMap<>());
- creatorCacheMap.put(HashSet.class, p -> new HashSet<>());
- creatorCacheMap.put(Stream.class, p -> new ArrayList<>().stream());
- creatorCacheMap.put(ConcurrentHashMap.class, p -> new ConcurrentHashMap<>());
- creatorCacheMap.put(CompletableFuture.class, p -> new CompletableFuture<>());
- creatorCacheMap.put(CompletionStage.class, p -> new CompletableFuture<>());
- creatorCacheMap.put(Map.Entry.class, new Creator() {
- @Override
- @ConstructorParameters({"key", "value"})
- public Map.Entry create(Object... params) {
- return new AbstractMap.SimpleEntry(params[0], params[1]);
- }
-
- @Override
- public Class[] paramTypes() {
- return new Class[]{Object.class, Object.class};
- }
- });
- creatorCacheMap.put(AbstractMap.SimpleEntry.class, new Creator() {
- @Override
- @ConstructorParameters({"key", "value"})
- public AbstractMap.SimpleEntry create(Object... params) {
- return new AbstractMap.SimpleEntry(params[0], params[1]);
- }
-
- @Override
- public Class[] paramTypes() {
- return new Class[]{Object.class, Object.class};
- }
- });
- creatorCacheMap.put(AnyValue.DefaultAnyValue.class, p -> new AnyValue.DefaultAnyValue());
- creatorCacheMap.put(AnyValue.class, p -> new AnyValue.DefaultAnyValue());
-
- arrayCacheMap.put(int.class, t -> new int[t]);
- arrayCacheMap.put(byte.class, t -> new byte[t]);
- arrayCacheMap.put(long.class, t -> new long[t]);
- arrayCacheMap.put(String.class, t -> new String[t]);
- arrayCacheMap.put(Object.class, t -> new Object[t]);
- arrayCacheMap.put(boolean.class, t -> new boolean[t]);
- arrayCacheMap.put(short.class, t -> new short[t]);
- arrayCacheMap.put(char.class, t -> new char[t]);
- arrayCacheMap.put(float.class, t -> new float[t]);
- arrayCacheMap.put(double.class, t -> new double[t]);
- }
-
- static class SimpleClassVisitor extends ClassVisitor {
-
- private final String constructorDesc;
-
- private final List fieldnames;
-
- private boolean started;
-
- public SimpleClassVisitor(int api, List fieldnames, String constructorDesc) {
- super(api);
- this.fieldnames = fieldnames;
- this.constructorDesc = constructorDesc;
- }
-
- @Override
- public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
- if (java.lang.reflect.Modifier.isStatic(access) || !"".equals(name)) {
- return null;
- }
- if (constructorDesc != null && !constructorDesc.equals(desc)) {
- return null;
- }
- if (this.started) {
- return null;
- }
- this.started = true;
- //返回的List中参数列表可能会比方法参数量多,因为方法内的临时变量也会存入list中, 所以需要list的元素集合比方法的参数多
- return new MethodVisitor(Opcodes.ASM6) {
- @Override
- public void visitLocalVariable(String name, String description, String signature, Label start, Label end, int index) {
- if (index < 1) {
- return;
- }
- int size = fieldnames.size();
- //index不会按顺序执行的
- if (index > size) {
- for (int i = size; i < index; i++) {
- fieldnames.add(" ");
- }
- fieldnames.set(index - 1, name);
- }
- fieldnames.set(index - 1, name);
- }
- };
- }
- }
-
- public static SimpleEntry[] getConstructorField(Class clazz, int paramcount, String constructorDesc) {
- String n = clazz.getName();
- InputStream in = clazz.getResourceAsStream(n.substring(n.lastIndexOf('.') + 1) + ".class");
- if (in == null) {
- return null;
- }
- ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
- byte[] bytes = new byte[1024];
- int pos;
- try {
- while ((pos = in.read(bytes)) != -1) {
- out.write(bytes, 0, pos);
- }
- in.close();
- } catch (IOException io) {
- return null;
- }
- final List fieldnames = new ArrayList<>();
- new ClassReader(out.toByteArray()).accept(new SimpleClassVisitor(Opcodes.ASM6, fieldnames, constructorDesc), 0);
- while (fieldnames.remove(" ")); //删掉空元素
- if (fieldnames.isEmpty()) {
- return null;
- }
- if (paramcount == fieldnames.size()) {
- return getConstructorField(clazz, paramcount, fieldnames.toArray(new String[fieldnames.size()]));
- } else {
- String[] fs = new String[paramcount];
- for (int i = 0; i < fs.length; i++) {
- fs[i] = fieldnames.get(i);
- }
- return getConstructorField(clazz, paramcount, fs);
- }
- }
-
- public static SimpleEntry[] getConstructorField(Class clazz, int paramcount, String[] names) {
- SimpleEntry[] se = new SimpleEntry[names.length];
- for (int i = 0; i < names.length; i++) { //查询参数名对应的Field
- try {
- Field field = clazz.getDeclaredField(names[i]);
- se[i] = new SimpleEntry<>(field.getName(), field.getType());
- } catch (NoSuchFieldException fe) {
- Class cz = clazz;
- Field field = null;
- while ((cz = cz.getSuperclass()) != Object.class) {
- try {
- field = cz.getDeclaredField(names[i]);
- break;
- } catch (NoSuchFieldException nsfe) {
- }
- }
- if (field == null) {
- return null;
- }
- se[i] = new SimpleEntry<>(field.getName(), field.getType());
- } catch (Exception e) {
- if (logger.isLoggable(Level.FINE)) {
- logger.log(Level.FINE, clazz + " getConstructorField error", e);
- }
- return null;
- }
- }
- return se;
- }
-
- public static SimpleEntry[] getConstructorField(Class clazz, int paramcount, Parameter[] params) {
- SimpleEntry[] se = new SimpleEntry[params.length];
- for (int i = 0; i < params.length; i++) { //查询参数名对应的Field
- try {
- Field field = clazz.getDeclaredField(params[i].getName());
- se[i] = new SimpleEntry<>(field.getName(), field.getType());
- } catch (Exception e) {
- return null;
- }
- }
- return se;
- }
-
- static class TypeArrayIntFunction implements IntFunction {
-
- private final Class type;
-
- public TypeArrayIntFunction(Class type) {
- this.type = type;
- }
-
- @Override
- public T[] apply(int value) {
- return (T[]) Array.newInstance(type, value);
- }
-
- }
-
- public static IntFunction createArrayFunction(final Class clazz) {
- final String interName = clazz.getName().replace('.', '/');
- final String interDesc = Type.getDescriptor(clazz);
- final ClassLoader loader = clazz.getClassLoader();
- final String newDynName = "org/redkaledyn/creator/_DynArrayFunction__" + clazz.getName().replace('.', '_').replace('$', '_');
- try {
- Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.'));
- return (IntFunction) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz).getDeclaredConstructor().newInstance();
- } catch (Throwable ex) {
- }
-
- //-------------------------------------------------------------
- ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
- MethodVisitor mv;
- cw.visit(V11, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, "Ljava/lang/Object;Ljava/util/function/IntFunction<[" + interDesc + ">;", "java/lang/Object", new String[]{"java/util/function/IntFunction"});
-
- {//IntFunction自身的构造方法
- mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null);
- mv.visitVarInsn(ALOAD, 0);
- mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V", false);
- mv.visitInsn(RETURN);
- mv.visitMaxs(1, 1);
- mv.visitEnd();
- }
- {//apply 方法
- mv = cw.visitMethod(ACC_PUBLIC, "apply", "(I)[" + interDesc, null, null);
- mv.visitVarInsn(ILOAD, 1);
- mv.visitTypeInsn(ANEWARRAY, interName);
- mv.visitInsn(ARETURN);
- mv.visitMaxs(1, 2);
- mv.visitEnd();
- }
- { //虚拟 apply 方法
- mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "apply", "(I)Ljava/lang/Object;", null, null);
- mv.visitVarInsn(ALOAD, 0);
- mv.visitVarInsn(ILOAD, 1);
- mv.visitMethodInsn(INVOKEVIRTUAL, newDynName, "apply", "(I)[" + interDesc, false);
- mv.visitInsn(ARETURN);
- mv.visitMaxs(2, 2);
- mv.visitEnd();
- }
- cw.visitEnd();
- final byte[] bytes = cw.toByteArray();
- try {
- Class> resultClazz = 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, resultClazz);
- RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.'));
- return (IntFunction) resultClazz.getDeclaredConstructor().newInstance();
- } catch (Exception ex) {
- //ex.printStackTrace(); //一般不会发生
- //return t -> (T[]) Array.newInstance(clazz, t);
- throw new RuntimeException(ex);
- }
- }
- }
-
- /**
- * 创建对象
- *
- * @param params 构造函数的参数
- *
- * @return 构建的对象
- */
- public T create(Object... params);
-
- /**
- * 参数类型数组
- *
- * @return 参数类型数组
- */
- default Class[] paramTypes() {
- return new Class[0];
- }
-
- /**
- * 创建指定类型对象数组的IntFunction
- *
- * @param 泛型
- * @param type 类型
- *
- * @return IntFunction
- */
- public static IntFunction arrayFunction(final Class type) {
- return CreatorInner.arrayCacheMap.computeIfAbsent(type, t -> CreatorInner.createArrayFunction(t));
- }
-
- /**
- * 创建指定大小的对象数组
- *
- * @param 泛型
- * @param type 类型
- * @param size 数组大小
- *
- * @return 数组
- */
- public static T[] newArray(final Class type, final int size) {
- if (type == int.class) {
- return (T[]) (Object) new int[size];
- }
- if (type == byte.class) {
- return (T[]) (Object) new byte[size];
- }
- if (type == long.class) {
- return (T[]) (Object) new long[size];
- }
- if (type == String.class) {
- return (T[]) new String[size];
- }
- if (type == Object.class) {
- return (T[]) new Object[size];
- }
- if (type == boolean.class) {
- return (T[]) (Object) new boolean[size];
- }
- if (type == short.class) {
- return (T[]) (Object) new short[size];
- }
- if (type == char.class) {
- return (T[]) (Object) new char[size];
- }
- if (type == float.class) {
- return (T[]) (Object) new float[size];
- }
- if (type == double.class) {
- return (T[]) (Object) new double[size];
- }
- //return (T[]) Array.newInstance(type, size);
- return arrayFunction(type).apply(size);
- }
-
- /**
- * 根据Supplier生产Creator
- *
- * @param 构建类的数据类型
- * @param supplier Supplier
- *
- * @return Creator对象
- */
- public static Creator create(final Supplier supplier) {
- Objects.requireNonNull(supplier);
- return (Object... params) -> supplier.get();
- }
-
- /**
- * 根据Function生产Creator
- *
- * @param 构建类的数据类型
- * @param func Function
- *
- * @return Creator对象
- */
- public static Creator create(final Function