@ SuppressWarnings ( "unchecked" ) // safe since the proxy implements type private < T > T createFakeResource ( Class < T > type , final String name ) { return ( T ) Proxy . newProxyInstance ( FakeClientBundleProvider . class . getClassLoader ( ) , new Class < ? > [ ] { type } , new InvocationHandler ( ) { @ Override public Object invoke ( Object proxy , Method method , Object [ ] args ) throws Exception { Class < ? > returnType = method . getReturnType ( ) ; if ( returnType == String . class ) { return name ; } else if ( returnType == SafeHtml . class ) { return SafeHtmlUtils . fromTrustedString ( name ) ; } else if ( returnType == SafeUri . class ) { return UriUtils . fromTrustedString ( name ) ; } else if ( returnType == boolean . class ) { return false ; } else if ( returnType == int . class ) { return 0 ; } else if ( method . getParameterTypes ( ) [ 0 ] == ResourceCallback . class ) { // Read the underlying resource type out of the generic parameter // in the method's argument Class < ? > resourceType = ( Class < ? > ) ( ( ParameterizedType ) args [ 0 ] . getClass ( ) . getGenericInterfaces ( ) [ 0 ] ) . getActualTypeArguments ( ) [ 0 ] ; ( ( ResourceCallback < ResourcePrototype > ) args [ 0 ] ) . onSuccess ( ( ResourcePrototype ) createFakeResource ( resourceType , name ) ) ; return null ; } else { throw new IllegalArgumentException ( "Unexpected return type for method " + method . getName ( ) ) ; } } } ) ; }
Creates a fake resource class that returns its own name where possible .