【问题标题】:Javaassist source error while instrumenting a method检测方法时出现 Javassist 源错误
【发布时间】:2013-04-01 13:12:46
【问题描述】:

我正在尝试在我的一个实体 (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

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: java reflection instrumentation


    【解决方案1】:

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

    return (java.lang.String) new java.beans.PropertyDescriptor(mappingFieldName, com.xxx.entity.CustomFieldValue.class).getReadMethod().invoke(cfv, null);}").toString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      相关资源
      最近更新 更多