简体   繁体   English

字节伙伴(字节码操作)拦截器不起作用:无

[英]byte-buddy (byte code manipulation) interceptor dont work: None of

I have to add annotation XmlElementWrapper and XmlElement to field of list type, but these annotation required name. 我必须将注释XmlElementWrapper和XmlElement添加到列表类型的字段,但是这些注释是必需的名称。 I would to set property name to field name. 我将属性名称设置为字段名称。 I do: 我做:

   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());
                        }

This is my interceptor: 这是我的拦截器:

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;
    }
}

This is part of the target class: 这是目标类的一部分:

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();
    }
  }
..
..
}

But i get this Exception: 但是我得到这个异常:

None of [public static void factory.framework.SetterListInterceptor.getter(java.lang.reflect.Method), public static java.util.List factory.framework.SetterListInterceptor.get_fieldsList()] allows for delegation from public drift.drift.thrift.api.sigs534.tTOPR$tTOPRStandardScheme drift.drift.thrift.api.sigs534.tTOPR$tTOPRStandardSchemeFactory.getScheme() -> [Help 1] [public static void factory.framework.SetterListInterceptor.getter(java.lang.reflect.Method),public static java.util.List factory.framework.SetterListInterceptor.get_fieldsList()]均不允许从publicdrift.drift.thrift委托。 .api.sigs534.tTOPR $ tTOPRStandardSchemedrift.drift.thrift.api.sigs534.tTOPR $ tTOPRStandardSchemeFactory.getScheme()-> [帮助1]

What's wrong? 怎么了? Any solution appreciated. 任何解决方案表示赞赏。

Sorry for wrong class name "SetterInterceptor", i have to rename to GetterInterceptor (dont be confused) Thank you very much 对不起,错误的类名称“ SetterInterceptor”,我必须重命名为GetterInterceptor(不要混淆)非常感谢

Roby 罗比

Your getter interceptor returns void whereas the intercepted methods are supposed to return a value. 您的getter拦截器返回void而被拦截的方法应该返回一个值。 Therefore, Byte Buddy does not map the methods. 因此,字节好友不会映射这些方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM