proxy

Smoother API than java.lang.reflect and various InvocationHandlers

  • A cleaner API io.earcam.instrumental.proxy.Proxies over java.lang.reflect.Proxy
  • A couple of java.lang.reflect.InvocationHandler implementations:
    • AOP advice like; around invocation
    • NOOP; provides safe default return values and noopthing else.
    • Easy stub; any matching methods on a PartialInvocationHandler instance will be invoked, otherwise the delegate instance is called (can combine with the NOOP handler)

Please note; PartialInvocationHandler contains some disgusting code for handling interface default methods and also suffers from inner/anon class issues - so don’t try extending this as an anonymous class..

To proxy abstract and concrete classes, see instrumental.proxy.concrete.


Dependency Graph

Module Dependency

Examples

Given an interface with default methods:

public interface WithDefaultMethod {

	default int returnOne()
	{
		return 1;
	}

	default int returnTwo()
	{
		return returnOne() + returnOne();
	}
}

Selectively override (without inheritance, override by method signature matching):

	WithDefaultMethod noop = Proxies.proxy(NOOP_INVOCATION_HANDLER, WithDefaultMethod.class);

	PartialInvocationHandler<WithDefaultMethod> handler;
	handler = new PartialInvocationHandler<WithDefaultMethod>(noop) {
		public int returnTwo()
		{
			return -2;
		}
	};

And invoke:

	WithDefaultMethod proxy = Proxies.proxy(handler, WithDefaultMethod.class);

	assertThat(proxy.returnOne(), is(1));
	assertThat(proxy.returnTwo(), is(-2));


Back to top

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

Earcam Maven Skin.