private <T> T getInterfaceInner(final Object thiz, final Class<T> clazz) { if (clazz == null || !clazz.isInterface()) { throw new IllegalArgumentException(getMessage("interface.class.expected")); } // perform security access check as early as possible final SecurityManager sm = System.getSecurityManager(); if (sm != null) { if (! Modifier.isPublic(clazz.getModifiers())) { throw new SecurityException(getMessage("implementing.non.public.interface", clazz.getName())); } Context.checkPackageAccess(clazz); } ScriptObject realSelf = null; ScriptObject realGlobal = null; if(thiz == null) { // making interface out of global functions realSelf = realGlobal = getNashornGlobalFrom(context); } else if (thiz instanceof ScriptObjectMirror) { final ScriptObjectMirror mirror = (ScriptObjectMirror)thiz; realSelf = mirror.getScriptObject(); realGlobal = mirror.getHomeGlobal(); if (! isOfContext(realGlobal, nashornContext)) { throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); } } else if (thiz instanceof ScriptObject) { // called from script code. realSelf = (ScriptObject)thiz; realGlobal = Context.getGlobal(); if (realGlobal == null) { throw new IllegalArgumentException(getMessage("no.current.nashorn.global")); } if (! isOfContext(realGlobal, nashornContext)) { throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); } } if (realSelf == null) { throw new IllegalArgumentException(getMessage("interface.on.non.script.object")); } try { final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != realGlobal); try { if (globalChanged) { Context.setGlobal(realGlobal); } if (! isInterfaceImplemented(clazz, realSelf)) { return null; } return clazz.cast(JavaAdapterFactory.getConstructor(realSelf.getClass(), clazz, MethodHandles.publicLookup()).invoke(realSelf)); } finally { if (globalChanged) { Context.setGlobal(oldGlobal); } } } catch(final RuntimeException|Error e) { throw e; } catch(final Throwable t) { throw new RuntimeException(t); } }
private <T> T getInterfaceInner(final Object thiz, final Class<T> clazz) { if (clazz == null || !clazz.isInterface()) { throw new IllegalArgumentException(getMessage("interface.class.expected")); } // perform security access check as early as possible final SecurityManager sm = System.getSecurityManager(); if (sm != null) { if (! Modifier.isPublic(clazz.getModifiers())) { throw new SecurityException(getMessage("implementing.non.public.interface", clazz.getName())); } Context.checkPackageAccess(clazz.getName()); } ScriptObject realSelf = null; ScriptObject realGlobal = null; if(thiz == null) { // making interface out of global functions realSelf = realGlobal = getNashornGlobalFrom(context); } else if (thiz instanceof ScriptObjectMirror) { final ScriptObjectMirror mirror = (ScriptObjectMirror)thiz; realSelf = mirror.getScriptObject(); realGlobal = mirror.getHomeGlobal(); if (! isOfContext(realGlobal, nashornContext)) { throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); } } else if (thiz instanceof ScriptObject) { // called from script code. realSelf = (ScriptObject)thiz; realGlobal = Context.getGlobal(); if (realGlobal == null) { throw new IllegalArgumentException(getMessage("no.current.nashorn.global")); } if (! isOfContext(realGlobal, nashornContext)) { throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); } } if (realSelf == null) { throw new IllegalArgumentException(getMessage("interface.on.non.script.object")); } try { final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != realGlobal); try { if (globalChanged) { Context.setGlobal(realGlobal); } if (! isInterfaceImplemented(clazz, realSelf)) { return null; } return clazz.cast(JavaAdapterFactory.getConstructor(realSelf.getClass(), clazz).invoke(realSelf)); } finally { if (globalChanged) { Context.setGlobal(oldGlobal); } } } catch(final RuntimeException|Error e) { throw e; } catch(final Throwable t) { throw new RuntimeException(t); } }