proxy.concrete

Proxy classes using standard java.lang.reflect.InvocationHandler

io.earcam.instrumental.proxy.concrete.Proxy:

  • Uses CGLib to allow proxying of classes
  • Uses Objenesis to circumvent constructor issues
  • Accepts standard java.lang.reflect.InvocationHandler

A comprehensive (though not super-performant) proxy capable of proxying just about anything when couple with instrumental.agent.defy.

See also invocation handlers and proxying for interfaces in instrumental.proxy.


Dependency Graph

Module Dependency

Examples

Proxy an abstract class

	Number proxy = Proxy.createProxy(Number.class, new NoopInvocationHandler() {
		@Override
		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
		{
			return "doubleValue".equals(method.getName()) ? 42D : super.invoke(proxy, method, args);
		}
	});

	assertThat(proxy.doubleValue(), is(42D));

Proxy an awkward class

A bit awkward:

public static class Foo {

	public Foo()
	{
		throw new IllegalStateException();
	}

	public Foo(int unused)
	{}

	public String hello()
	{
		return "hello";
	}

	public String goodbye()
	{
		return "goodbye";
	}
}

A simple partial handler:

public static class Handler extends PartialInvocationHandler<Foo> {

	public Handler(Foo delegate)
	{
		super(delegate);
	}

	public String goodbye()
	{
		return "not " + delegate.goodbye() + ", but farewell";
	}
}

Then invocation:

	Foo proxy = Proxy.createProxy(Foo.class, new Handler(new Foo(42)));

	assertThat(proxy.hello(), is(equalTo("hello")));
	assertThat(proxy.goodbye(), is(equalTo("not goodbye, but farewell")));


Back to top

Version: 0.1.0. Last Published: 2018-10-08.

Earcam Maven Skin.