1

我正在尝试在我的一个实体(com.xxx.entity.Order)中使用一种方法:

private String getLookupMethodString() {
    return new StringBuilder(
            "public java.lang.String lookupCustomValue(java.lang.String fieldName) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.beans.IntrospectionException {").append(
            "java.lang.String mappingFieldName =  \"value1\";").append(
            "com.xxx.entity.CustomFieldValue cfv = (com.xxx.entity.CustomFieldValue) new java.beans.PropertyDescriptor(\"customFieldValue\", getClass()).getReadMethod().invoke(this);").append(
            "return (java.lang.String) new java.beans.PropertyDescriptor(mappingFieldName, com.xxx.entity.CustomFieldValue.class).getReadMethod().invoke(cfv);}").toString();
}

public byte[] transform(ClassLoader clazzLoader, String className, Class<?> clazz, ProtectionDomain arg3, byte[] rawBytes) throws IllegalClassFormatException {
    CtClass cl = pool.makeClass(new java.io.ByteArrayInputStream(rawBytes));
    String lookupMethodString = getLookupMethodString();
    CtMethod lookupMethod = CtMethod.make(lookupMethodString, cl); // Error encountered at this line.
    cl.addMethod(lookupMethod);
}

字段 customFieldValue 也已在此类中进行检测,因此我使用反射来获取其值。我在执行此操作时遇到的错误是:

[source error] invoke(com.xxx.entity.CustomFieldValue) not found in java.lang.reflect.Method

有人可以帮我吗?

4

1 回答 1

0

让它工作,问题在于调用方法的调用。当尝试调用第二个参数为 null 的调用方法时,它起作用了。(不太清楚为什么?)

return (java.lang.String) new java.beans.PropertyDescriptor(mappingFieldName, com.xxx.entity.CustomFieldValue.class).getReadMethod().invoke(cfv, null);}").toString();
于 2013-04-01T17:18:57.687 回答