6

即使我提供了实际实例,Byte Buddy 似乎也只欣赏公共类作为拦截器实现;我经常发现自己想做这样的事情:

import static MethodDelegation.to;

new ByteBuddy().subclass(Object.class).method(any()).intercept(to(new Object() {
  @RuntimeType
  public Object intercept(@Origin Method m, @AllArguments Object[] a) {
    return null;
  }
});

然而,这会导致如下异常:

Exception in thread "main" java.lang.IllegalStateException: class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$pUmdGhyP cannot access class us.levk.guice.vs.Scopes$1Builder$1

可见性强制执行背后有什么原因吗?

编辑:我仍然遇到麻烦;我得到了一个不同的例外,这是我的代码:

package us.levk.guice.vs;

import static net.bytebuddy.implementation.MethodDelegation.to;
import static net.bytebuddy.matcher.ElementMatchers.any;

import java.lang.reflect.Method;
import java.util.function.Function;
import java.util.function.Supplier;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;

public class Bar {

  Function<Supplier<?>, Class<?>> wrapper () {
    return s -> {
      return new ByteBuddy ().subclass (Object.class)
                             .name (Bar.class.getPackage ().getName () + ".Foo")
                             .method (any ())
                             .intercept (to (new Object () {
                               @RuntimeType
                               public Object intercept (@Origin Method method,
                                                        @AllArguments Object[] args) {
                                 System.out.println (method);
                                 return null;
                               }
                             }))
                             .make ()
                             .load (getClass ().getClassLoader (),
                                    ClassLoadingStrategy.Default.WRAPPER)
                             .getLoaded ();
    };
  }

  public static void main (String[] args) throws InstantiationException, IllegalAccessException {
    new Bar ().wrapper ().apply ( () -> new Integer (1)).newInstance ().toString ();
  }
}

现在的例外是:

Exception in thread "main" java.lang.IllegalAccessError: tried to access class us.levk.guice.vs.Bar$1 from class us.levk.guice.vs.Foo

编辑2:

如果我将类加载策略更改为 INJECTION 它工作正常

4

1 回答 1

2

在这种情况下,Byte Buddy 会通知您会导致IllegalAccessException运行时出现的可见性约束。

即使您声明了您的方法public,匿名类也会被javac自动定义为包私有。因此,该方法对生成的类不可见,因为net.bytebuddy.renamed.java.lang.Object不等于us.levk.guice.vs.Scopes

如果你这样做了:

new ByteBuddy().subclass(Object.class).name("us.levk.guice.vs.Foo")
               .method(any()).intercept(to(new Object() {
  @RuntimeType
  public Object intercept(@Origin Method m, @AllArguments Object[] a) {
    return null;
  }
}));

一切都会奏效。或者,您可以定义 - 例如 - 一个公共接口,例如:

public interface Foo {
  @RuntimeType
  Object intercept(@Origin Method m, @AllArguments Object[] a);
}

new ByteBuddy().subclass(Object.class).name("us.levk.guice.vs.Foo")
               .method(any()).intercept(to(new Foo() {
  @RuntimeType
  public Object intercept(@Origin Method m, @AllArguments Object[] a) {
    return null;
  }
}, Foo.class));
于 2016-01-13T11:12:52.523 回答