Reputation: 491
I have to add annotation XmlElementWrapper and XmlElement to field of list type, but these annotation required name. 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]
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
Roby
Upvotes: 0
Views: 446
Reputation: 44077
Your getter interceptor returns void
whereas the intercepted methods are supposed to return a value. Therefore, Byte Buddy does not map the methods.
Upvotes: 1