InterfaceImplementor.kt 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package com.script.rhino
  26. import com.script.Invocable
  27. import com.script.ScriptException
  28. import java.lang.reflect.InvocationHandler
  29. import java.lang.reflect.Method
  30. import java.lang.reflect.Proxy
  31. import java.security.AccessControlContext
  32. import java.security.AccessController
  33. import java.security.PrivilegedExceptionAction
  34. /**
  35. * java.lang.reflect.Proxy based interface implementor. This is meant
  36. * to be used to implement Invocable.getInterface.
  37. *
  38. * @author Mike Grogan
  39. * @since 1.6
  40. */
  41. @Suppress("UNUSED_PARAMETER")
  42. open class InterfaceImplementor(private val engine: Invocable) {
  43. @Throws(ScriptException::class)
  44. fun <T> getInterface(obj: Any?, clazz: Class<T>?): T? {
  45. return if (clazz != null && clazz.isInterface) {
  46. if (!isImplemented(obj, clazz)) {
  47. null
  48. } else {
  49. val accContext = AccessController.getContext()
  50. clazz.cast(
  51. Proxy.newProxyInstance(
  52. clazz.classLoader,
  53. arrayOf<Class<*>>(clazz),
  54. InterfaceImplementorInvocationHandler(obj, accContext)
  55. )
  56. )
  57. }
  58. } else {
  59. throw IllegalArgumentException("interface Class expected")
  60. }
  61. }
  62. protected open fun isImplemented(obj: Any?, clazz: Class<*>): Boolean {
  63. return true
  64. }
  65. @Throws(ScriptException::class)
  66. protected open fun convertResult(method: Method?, res: Any?): Any? {
  67. return res
  68. }
  69. @Throws(ScriptException::class)
  70. protected fun convertArguments(method: Method?, args: Array<Any>): Array<Any> {
  71. return args
  72. }
  73. private inner class InterfaceImplementorInvocationHandler(
  74. private val obj: Any?,
  75. private val accContext: AccessControlContext
  76. ) : InvocationHandler {
  77. @Throws(Throwable::class)
  78. override fun invoke(proxy: Any, method: Method, args: Array<Any>): Any? {
  79. val finalArgs = convertArguments(method, args)
  80. val result = AccessController.doPrivileged(PrivilegedExceptionAction {
  81. if (obj == null) engine.invokeFunction(method.name, *finalArgs)
  82. else engine.invokeMethod(obj, method.name, *finalArgs)
  83. }, accContext)
  84. return convertResult(method, result)
  85. }
  86. }
  87. }