Byte Buddy Interceptor Class Visibility

Byte Buddy seems to evaluate public classes as implementations of interceptors, even if I provide the actual instance; often I find that I want to do something like this:

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

This results in the following exception:

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

Is there any reason for maintaining visibility?

EDITOR: I still have problems with this; I get another exception, this is my code:

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

And now the exception:

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

EDIT2:

It works fine if I change the class loading strategy to INJECTION

+4
source share
1 answer

In this case, Byte Buddy informs you of a visibility restriction that will result IllegalAccessExceptionin runtime.

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));
+1

Source: https://habr.com/ru/post/1624026/


All Articles