我需要编写代码,模拟在运行时在类中提供用户定义的字段数。我们的想法是能够将指向那些“动态”字段的java.reflect.Field对象返回给客户端代码。
class DynamicFieldClass {
/**
* fieldNames is the list of names of the fields we want to "exist" in the class
* they will all be of the same type (say String)
*/
public DynamicFieldClass(List<String> fieldNames) {
// ... what do we do here
}
public Field getFieldObjectFor(String desiredFieldName) {
// ... what do we do here
}
}
是否有类似于DynamicProxy(但对于字段)? 感谢
答案 0 :(得分:0)
我最终使用Javassist: - 在运行时创建一个继承自原始类的新类定义 - 将我需要的字段注入新的类定义
我还用静态工厂方法替换了公共构造函数,该方法生成并返回新类定义的实例。总而言之,代码如下所示:
class DynamicFieldClass {
protected DynamicFieldClass() {
}
public Field getFieldObjectFor(String desiredFieldName) {
return null;
}
/**
* fieldNames is the list of names of the fields we want to "exist" in the class
* they will all be of the same type (say String)
*/
public static createInstance (List<String> fieldNames) {
ClassPool defaultClassPool = ClassPool.getDefault();
CtClass originalClass = defaultClassPool.get("DynamicFieldClass");
CtClass newClass = defaultClassPool.makeClass("modified_DynamicFieldClass", originalClass);
StringBuilder getterCore = new StringBuilder();
for (String item : fieldNames) {
CtField addedField = CtField.make(String.format("private String %s;", item), newClass);
newClass.addField(addedField);
getterCore.append(String.format("if \"%s\".equals(%1) { return this.class.getDeclaredField(%s);}", item, item));
}
getterCore.append("throw new IllegalArgumentException(\"Unknown field name: \" + $1);");
final String sourceGeneralGetter = String.format("{%s}", getterCore.toString());
CtMethod mold = originalClass.getDeclaredMethod("getFieldObjectFor");
CtMethod copiedMeth = CtNewMethod.copy(mold, newClass, null);
newClass.addMethod(copiedMeth);
CtMethod getMeth = newClass.getDeclaredMethod("getFieldObjectFor");
getMeth.setBody(sourceGeneralGetter);
CtConstructor defaultCtor = new CtConstructor(new CtClass[0], newClass);
defaultCtor.setBody("{}");
newClass.addConstructor(defaultCtor);
Class modifiedClass = newClass.toClass();
return modifiedClass.newInstance();
}
}