Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法委派和添加字段_Java_Byte Buddy - Fatal编程技术网

Java 方法委派和添加字段

Java 方法委派和添加字段,java,byte-buddy,Java,Byte Buddy,我试图实现方法委托,并通过委托所有setter来添加一个字段来跟踪类中的脏字段。我的类实例化如下所示: T t = new ByteBuddy() .subclass (typeClass) .defineField ("dirtyFields", List.class, Visibility.PUBLIC) .method(ElementMatchers.isSetter()) .intercept(Met

我试图实现方法委托,并通过委托所有setter来添加一个字段来跟踪类中的脏字段。我的类实例化如下所示:

        T t = new ByteBuddy()
            .subclass (typeClass)
            .defineField ("dirtyFields", List.class, Visibility.PUBLIC)
    .method(ElementMatchers.isSetter())
        .intercept(MethodDelegation.to(SetterInterceptor.class))
            .make ()
            .load (SetterInterceptor.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded ()
            .newInstance();

    List<String> l = new ArrayList<String> ();
    t.getClass().getField("dirtyFields").set(t, l);
    l.add("foobar");

    return t;
  @RuntimeType
  public static Object intercept(@SuperCall Callable<?> superMethod, @Origin Method method, @Super(proxyType = TargetType.class) Object delegate) throws Exception {
    System.out.println ("---- Intercept");
    markDirty (delegate, Character.toLowerCase (method.getName ().charAt(3)) + method.getName().substring(4));
    return superMethod.call();
  }
T=newbytebuddy()
.子类(类型类)
.defineField(“dirtyFields”、List.class、Visibility.PUBLIC)
.method(ElementMatchers.isSetter())
.intercept(MethodDelegation.to(SetterInterceptor.class))
.make()
.load(SetterInterceptor.class.getClassLoader(),ClassLoadingStrategy.Default.WRAPPER)
.getLoaded()
.newInstance();
列表l=新的ArrayList();
t、 getClass().getField(“dirtyFields”).set(t,l);
l、 添加(“foobar”);
返回t;
我的拦截器是这样的:

        T t = new ByteBuddy()
            .subclass (typeClass)
            .defineField ("dirtyFields", List.class, Visibility.PUBLIC)
    .method(ElementMatchers.isSetter())
        .intercept(MethodDelegation.to(SetterInterceptor.class))
            .make ()
            .load (SetterInterceptor.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded ()
            .newInstance();

    List<String> l = new ArrayList<String> ();
    t.getClass().getField("dirtyFields").set(t, l);
    l.add("foobar");

    return t;
  @RuntimeType
  public static Object intercept(@SuperCall Callable<?> superMethod, @Origin Method method, @Super(proxyType = TargetType.class) Object delegate) throws Exception {
    System.out.println ("---- Intercept");
    markDirty (delegate, Character.toLowerCase (method.getName ().charAt(3)) + method.getName().substring(4));
    return superMethod.call();
  }
@RuntimeType
公共静态对象拦截(@SuperCall Callable superMethod、@Origin Method、@Super(proxyType=TargetType.class)对象委托)引发异常{
System.out.println(“----截取”);
markDirty(委托,Character.toLowerCase(method.getName().charAt(3))+method.getName().substring(4));
返回superMethod.call();
}
但是,我无法获取setter正在调用的“对象”,并且委托似乎没有引用该对象。我想知道我是否需要做一些事情,或者我可以注入一些东西来提供这个功能


谢谢

我通过将@This注入拦截器来回答自己的问题:

  public static Object intercept(@This Object thiz, @SuperCall Callable<?> superMethod, @Origin Method method, @Super(proxyType = TargetType.class) Object delegate) throws Exception {
公共静态对象拦截(@This Object thiz、@SuperCall Callable superMethod、@Origin Method、@Super(proxyType=TargetType.class)对象委托)引发异常{

请注意,您应该避免在目标类上使用非常昂贵的
@Super
代理。而应该有一个
CanBeDirty
接口,您的所有实例都实现该接口,并基于该接口创建代理。