diff --git a/android-jdk6-redkale/src/org/redkale/util/Creator.java b/android-jdk6-redkale/src/org/redkale/util/Creator.java index acbf9370e..8fd859010 100644 --- a/android-jdk6-redkale/src/org/redkale/util/Creator.java +++ b/android-jdk6-redkale/src/org/redkale/util/Creator.java @@ -12,7 +12,30 @@ import static org.objectweb.asm.Opcodes.*; import org.objectweb.asm.Type; /** - * 实现一个类的构造方法。 代替低效的反射实现方式。 不支持数组类 + * 实现一个类的构造方法。 代替低效的反射实现方式。 不支持数组类。 + * 常见的无参数的构造函数类都可以自动生成Creator, 对应自定义的类可以提供一个静态构建Creator方法。 + * + * 例如: + * public class Record { + * + * private final int id; + * + * private String name; + * + * public Record(int id, String name) { + * this.id = id; + * this.name = name; + * } + * + * private static Creator createCreator() { + * return new Creator() { + * @Override + * public Object create(Object... params) { + * return new Record((Integer) params[0], (String) params[1]); + * } + * }; + * } + * } * * @see http://www.redkale.org * @author zhangjx @@ -36,6 +59,17 @@ public interface Creator { if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { throw new RuntimeException("[" + clazz + "] is a interface or abstract class, cannot create it's Creator."); } + for (final Method method : clazz.getDeclaredMethods()) { + if (!Modifier.isStatic(method.getModifiers())) continue; + if (method.getParameterTypes().length != 0) continue; + if (method.getReturnType() != Creator.class) continue; + try { + method.setAccessible(true); + return (Creator) method.invoke(null); + } catch (Exception e) { + throw new RuntimeException(e); + } + } final String supDynName = Creator.class.getName().replace('.', '/'); final String interName = clazz.getName().replace('.', '/'); final String interDesc = Type.getDescriptor(clazz);