JavaAdapter.kt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2005, 2006, 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 org.mozilla.javascript.*
  28. import org.mozilla.javascript.Function
  29. /**
  30. * This class implements Rhino-like JavaAdapter to help implement a Java
  31. * interface in JavaScript. We support this using Invocable.getInterface.
  32. * Using this JavaAdapter, script author could write:
  33. *
  34. *
  35. * var r = new java.lang.Runnable() {
  36. * run: function() { script... }
  37. * };
  38. *
  39. *
  40. * r.run();
  41. * new java.lang.Thread(r).start();
  42. *
  43. *
  44. * Note that Rhino's JavaAdapter support allows extending a Java class and/or
  45. * implementing one or more interfaces. This JavaAdapter implementation does
  46. * not support these.
  47. *
  48. * @author A. Sundararajan
  49. * @since 1.6
  50. */
  51. @Suppress("UNUSED_PARAMETER")
  52. internal class JavaAdapter private constructor(private val engine: Invocable) : ScriptableObject(),
  53. Function {
  54. override fun getClassName(): String {
  55. return "JavaAdapter"
  56. }
  57. @Throws(RhinoException::class)
  58. override fun call(cx: Context, scope: Scriptable, thisObj: Scriptable, args: Array<Any>): Any {
  59. return construct(cx, scope, args)
  60. }
  61. @Throws(RhinoException::class)
  62. override fun construct(cx: Context, scope: Scriptable, args: Array<Any>): Scriptable {
  63. return if (args.size == 2) {
  64. var clazz: Class<*>? = null
  65. val obj = args[0]
  66. if (obj is Wrapper) {
  67. val o = obj.unwrap()
  68. if (o is Class<*> && o.isInterface) {
  69. clazz = o
  70. }
  71. } else if (obj is Class<*> && obj.isInterface) {
  72. clazz = obj
  73. }
  74. if (clazz == null) {
  75. throw Context.reportRuntimeError("JavaAdapter: first arg should be interface Class")
  76. } else {
  77. val topLevel = getTopLevelScope(scope)
  78. Context.toObject(
  79. engine.getInterface(args[1], clazz),
  80. topLevel
  81. )
  82. }
  83. } else {
  84. throw Context.reportRuntimeError("JavaAdapter requires two arguments")
  85. }
  86. }
  87. companion object {
  88. @JvmStatic
  89. @Throws(RhinoException::class)
  90. fun init(cx: Context, scope: Scriptable, sealed: Boolean) {
  91. val topLevel = scope as RhinoTopLevel
  92. val engine: Invocable = topLevel.scriptEngine
  93. val obj = JavaAdapter(engine)
  94. obj.parentScope = scope
  95. obj.prototype = getFunctionPrototype(scope)
  96. putProperty(topLevel, "JavaAdapter", obj)
  97. }
  98. }
  99. }