@ SuppressWarnings ( "unchecked" ) public < P > P as ( final Class < P > proxyType ) { final boolean isMap = ( object instanceof Map ) ; final InvocationHandler handler = new InvocationHandler ( ) { @ Override @ SuppressWarnings ( "null" ) public Object invoke ( Object proxy , Method method , Object [ ] args ) throws Throwable { String name = method . getName ( ) ; // Actual method name matches always come first try { return on ( type , object ) . call ( name , args ) . get ( ) ; } // [#14] Emulate POJO behaviour on wrapped map objects catch ( ReflectException e ) { if ( isMap ) { Map < String , Object > map = ( Map < String , Object > ) object ; int length = ( args == null ? 0 : args . length ) ; if ( length == 0 && name . startsWith ( "get" ) ) { return map . get ( property ( name . substring ( 3 ) ) ) ; } else if ( length == 0 && name . startsWith ( "is" ) ) { return map . get ( property ( name . substring ( 2 ) ) ) ; } else if ( length == 1 && name . startsWith ( "set" ) ) { map . put ( property ( name . substring ( 3 ) ) , args [ 0 ] ) ; return null ; } } throw e ; } } } ; return ( P ) Proxy . newProxyInstance ( proxyType . getClassLoader ( ) , new Class [ ] { proxyType } , handler ) ; }
Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface