byte-buddy(字节代码操作)拦截器不工作:没有

时间:2018-06-18 16:08:31

标签: java java-7 bytecode-manipulation byte-buddy

我必须在列表类型的字段中添加注释XmlElementWrapper和XmlElement,但这些注释需要名称。我想将属性名称设置为字段名称。 我这样做:

   new ByteBuddy()
                                .redefine(className)
                                .method(ElementMatchers.isGetter().and(ElementMatchers.nameStartsWith("get")))
                                .intercept(MethodDelegation.to(SetterListInterceptor.class))
                                .make();
                       //save in .class file (overwrite)
                        List<String> setFields = SetterListInterceptor.get_fieldsList();
                        for (String field : setFields) {
                            new ByteBuddy(ClassFileVersion.JAVA_V7)
                                    .redefine(className)
                                    .field(ElementMatchers.named(field))
                                    .annotateField(AnnotationDescription.Builder.ofType(XmlElementWrapper.class).define("name", "Wrapper" + field).build(),
                                            AnnotationDescription.Builder.ofType(XmlElement.class).define("name",  "element" + field ).build()
                                ).make()
                                .saveIn(_outputDirectory.getParent().toFile());
                        }

这是我的拦截器:

public class SetterListInterceptor {

    public static final String GET_SIGNATURE = "get";
    //questa è la lista delle proprietà di tipo java.util.List' trovate nella classe in oggetto (una istanza di questa classe per classe)
    private static List<String> _fieldsList = new LinkedList<>();


    public static void getter(@Origin Method m) {
        String mname = m.getName();
        if (m.getReturnType().getCanonicalName().equals("java.util.List")) {
            String fieldname = mname.substring(mname.indexOf(GET_SIGNATURE) + 3);
            _fieldsList.add(fieldname);
        }
    }


    public static List<String> get_fieldsList() {
        List<String> temp = new LinkedList<>();
        temp.addAll(_fieldsList);
        _fieldsList = new LinkedList<>();
        return temp;
    }
}

这是目标类的一部分:

package drift.drift.thrift.api.sigs534;

@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"})
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.11.0)", date = "2018-06-18")
public class tTOPR implements org.apache.thrift.TBase<tTOPR, tTOPR._Fields>, java.io.Serializable, Cloneable, Comparable<tTOPR> {
  private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("tTOPR");

  private static final org.apache.thrift.protocol.TField CDIPAPP_FIELD_DESC = new org.apache.thrift.protocol.TField("CDIPAPP", org.apache.thrift.protocol.TType.I64, (short)1);
  private static final org.apache.thrift.protocol.TField COPR_FIELD_DESC = new org.apache.thrift.protocol.TField("COPR", org.apache.thrift.protocol.TType.STRING, (short)2);
  private static final org.apache.thrift.protocol.TField CTERATT_FIELD_DESC = new org.apache.thrift.protocol.TField("CTERATT", org.apache.thrift.protocol.TType.STRING, (short)3);
  private static final org.apache.thrift.protocol.TField CUBS_FIELD_DESC = new org.apache.thrift.protocol.TField("CUBS", org.apache.thrift.protocol.TType.STRING, (short)4);
  private static final org.apache.thrift.protocol.TField DSESATT_FIELD_DESC = new org.apache.thrift.protocol.TField("DSESATT", org.apache.thrift.protocol.TType.STRING, (short)5);
  private static final org.apache.thrift.protocol.TField SAZILST_FIELD_DESC = new org.apache.thrift.protocol.TField("SAZILST", org.apache.thrift.protocol.TType.STRING, (short)6);
  private static final org.apache.thrift.protocol.TField XOPR_FIELD_DESC = new org.apache.thrift.protocol.TField("XOPR", org.apache.thrift.protocol.TType.STRING, (short)7);
  private static final org.apache.thrift.protocol.TField XSTT_FIELD_DESC = new org.apache.thrift.protocol.TField("XSTT", org.apache.thrift.protocol.TType.STRING, (short)8);

  private static final org.apache.thrift.scheme.SchemeFactory STANDARD_SCHEME_FACTORY = new tTOPRStandardSchemeFactory();
  private static final org.apache.thrift.scheme.SchemeFactory TUPLE_SCHEME_FACTORY = new tTOPRTupleSchemeFactory();

  public long CDIPAPP; // optional
  public java.lang.String COPR; // optional
  public java.lang.String CTERATT; // optional
  public java.lang.String CUBS; // optional
  public java.lang.String DSESATT; // optional
  public java.lang.String SAZILST; // optional
  public java.lang.String XOPR; // optional
  public java.lang.String XSTT; // optional

  /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */
  public enum _Fields implements org.apache.thrift.TFieldIdEnum {
...
}
....
..
  private static class tTOPRStandardSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
    public tTOPRStandardScheme getScheme() {
      return new tTOPRStandardScheme();
    }
  }
..
..
}

但是我得到了这个例外:

[public static void factory.framework.SetterListInterceptor.getter(java.lang.reflect.Method),public static java.util.List factory.framework.SetterListInterceptor.get_fieldsList()]都不允许来自public drift.drift.thrift.api.sigs534.tTOPR $ tTOPRStandardScheme drift.drift.thrift.api.sigs534.tTOPR $ tTOPRStandardSchemeFactory.getScheme() - &gt; [帮助1]

出了什么问题? 任何解决方案都表示赞赏。

抱歉错误的类名&#34; SetterInterceptor&#34;,我必须重命名为GetterInterceptor(不要混淆) 非常感谢你

拉​​吉

1 个答案:

答案 0 :(得分:0)

您的getter拦截器返回void,而被拦截的方法应该返回一个值。因此,Byte Buddy不会映射这些方法。