### Question:
InvocationHandlerAdapter implements Implementation { public static InvocationHandlerAdapter toField(String name) { return toField(name, FieldLocator.ForClassHierarchy.Factory.INSTANCE); } protected  InvocationHandlerAdapter(String fieldName, boolean cached, boolean privileged, Assigner assigner); static InvocationHandlerAdapter of(InvocationHandler invocationHandler); static InvocationHandlerAdapter of(InvocationHandler invocationHandler, String fieldName); static InvocationHandlerAdapter toField(String name); static InvocationHandlerAdapter toField(String name, FieldLocator.Factory fieldLocatorFactory); abstract WithoutPrivilegeConfiguration withoutMethodCache(); abstract Implementation withAssigner(Assigner assigner); abstract AssignerConfigurable withPrivilegedLookup();  }### Answer:
@Test public void testInstanceAdapterWithMethodCache() throws Exception { DynamicType.Loaded<Bar> loaded = new ByteBuddy() .subclass(Bar.class) .defineField(QUX, InvocationHandler.class, Visibility.PUBLIC) .method(isDeclaredBy(Bar.class)) .intercept(InvocationHandlerAdapter.toField(QUX)) .make() .load(Bar.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0)); assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1)); assertThat(loaded.getLoaded().getDeclaredFields().length, is(2)); Field field = loaded.getLoaded().getDeclaredField(QUX); assertThat(field.getModifiers(), is(Modifier.PUBLIC)); field.setAccessible(true); Bar instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); Foo foo = new Foo(); field.set(instance, foo); assertThat(instance.bar(FOO), is((Object) instance)); assertThat(foo.methods.size(), is(1)); assertThat(instance.bar(FOO), is((Object) instance)); assertThat(foo.methods.size(), is(2)); assertThat(foo.methods.get(0), sameInstance(foo.methods.get(1))); instance.assertZeroCalls(); }
@Test(expected = IllegalStateException.class) public void testStaticMethod() throws Exception { new ByteBuddy() .subclass(Object.class) .defineField(QUX, InvocationHandler.class) .defineMethod(FOO, void.class, Ownership.STATIC) .intercept(InvocationHandlerAdapter.toField(QUX)) .make(); }
@Test(expected = IllegalStateException.class) public void testNonExistentField() throws Exception { new ByteBuddy() .subclass(Object.class) .defineMethod(FOO, void.class) .intercept(InvocationHandlerAdapter.toField(QUX)) .make(); }
@Test(expected = IllegalStateException.class) public void testIncompatibleFieldType() throws Exception { new ByteBuddy() .subclass(Object.class) .defineField(QUX, Object.class) .defineMethod(FOO, void.class) .intercept(InvocationHandlerAdapter.toField(QUX)) .make(); }