private Object collectJsValue ( Object jsValue ) { if ( jsValue == null ) { return null ; } else if ( jsValue instanceof NativeFunction ) { return ( ( NativeFunction ) jsValue ) . getEncodedSource ( ) ; } else if ( jsValue instanceof NativeArray ) { NativeArray jsArr = ( NativeArray ) jsValue ; List < Object > retVal = new ArrayList <> ( ( int ) jsArr . getLength ( ) ) ; for ( int idx = 0 ; idx < jsArr . getLength ( ) ; idx ++ ) { retVal . add ( collectJsValue ( jsArr . get ( idx ) ) ) ; } return retVal ; } else if ( jsValue instanceof ScriptableObject ) { ScriptableObject jsObj = ( ScriptableObject ) jsValue ; Map < Object , Object > retVal = new HashMap <> ( ) ; for ( Object key : jsObj . getIds ( ) ) { retVal . put ( key , collectJsValue ( jsObj . get ( key ) ) ) ; } return retVal ; } else if ( jsValue instanceof ConsString ) { return ( ( ConsString ) jsValue ) . toString ( ) ; } else if ( jsValue instanceof NativeJavaObject ) { NativeJavaObject jsJavaObj = ( NativeJavaObject ) jsValue ; Object obj = jsJavaObj . unwrap ( ) ; return obj ; } else { String cn = jsValue . getClass ( ) . getCanonicalName ( ) ; if ( ! cn . startsWith ( "java." ) && ( ! cn . startsWith ( "il.ac.bgu" ) ) ) { System . out . println ( "collectJsValue: blind translation to java: " + jsValue + " (" + jsValue . getClass ( ) + ")" ) ; } return jsValue ; } }
Take a Javascript value from Rhino build a Java value for it .