public <T> T as(Class<T> type) {
        return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getDeclaringClass() == Object.class)
                    return method.invoke(this, args);

                Configuration cn = method.getAnnotation(Configuration.class);

                Class<?> r = method.getReturnType();
                String key = getKey(method,cn);

                String v = source.apply(key);
                if (v==null && cn!=null && !cn.defaultValue().equals(UNSPECIFIED))
                    v = cn.defaultValue();

                if (v==null)    return null;    // TODO: check how the primitive types are handled here

                return ConvertUtils.convert(v,r);
            }

            private String getKey(Method method, Configuration c) {
                if (c!=null && !c.name().equals(UNSPECIFIED))
                    return c.name();        // name override

                String n = method.getName();
                for (String p : GETTER_PREFIX) {
                    if (n.startsWith(p))
                        return Introspector.decapitalize(n.substring(p.length()));
                }
                return n;
            }
        }));
    }
public < T > T as ( Class < T > type ) { return type . cast ( Proxy . newProxyInstance ( type . getClassLoader ( ) , new Class [ ] { type } , new InvocationHandler ( ) { public Object invoke ( Object proxy , Method method , Object [ ] args ) throws Throwable { if ( method . getDeclaringClass ( ) == Object . class ) return method . invoke ( this , args ) ; Configuration cn = method . getAnnotation ( Configuration . class ) ; Class < ? > r = method . getReturnType ( ) ; String key = getKey ( method , cn ) ; String v = source . apply ( key ) ; if ( v == null && cn != null && ! cn . defaultValue ( ) . equals ( UNSPECIFIED ) ) v = cn . defaultValue ( ) ; if ( v == null ) return null ; return ConvertUtils . convert ( v , r ) ; } private String getKey ( Method method , Configuration c ) { if ( c != null && ! c . name ( ) . equals ( UNSPECIFIED ) ) return c . name ( ) ; String n = method . getName ( ) ; for ( String p : GETTER_PREFIX ) { if ( n . startsWith ( p ) ) return Introspector . decapitalize ( n . substring ( p . length ( ) ) ) ; } return n ; } } ) ) ; }
Creates a type-safe proxy that reads from the source specified by one of the fromXyz methods.