_jwrapper.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #*****************************************************************************
  2. # Copyright 2004-2008 Steve Menard
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. #*****************************************************************************
  17. import sys
  18. import _jpype
  19. from . import _jclass
  20. from ._jpackage import JPackage
  21. if sys.version > '3':
  22. unicode = str
  23. long = int
  24. def _initialize():
  25. _jpype.setWrapperClass(_JWrapper)
  26. _jpype.setStringWrapperClass(JString)
  27. class _JWrapper(object):
  28. def __init__(self, v):
  29. if v is not None:
  30. self._value = _jpype.convertToJValue(self.typeName, v)
  31. else:
  32. self._value = None
  33. class JByte(_JWrapper):
  34. typeName = "byte"
  35. class JShort(_JWrapper):
  36. typeName = "short"
  37. class JInt(_JWrapper):
  38. typeName = "int"
  39. class JLong(_JWrapper):
  40. typeName = "long"
  41. class JFloat(_JWrapper):
  42. typeName = "float"
  43. class JDouble(_JWrapper):
  44. typeName = "double"
  45. class JChar(_JWrapper):
  46. typeName = "char"
  47. class JBoolean(_JWrapper):
  48. typeName = "boolean"
  49. class JString(_JWrapper):
  50. typeName = "java.lang.String"
  51. def _getDefaultTypeName(obj):
  52. if obj is True or obj is False:
  53. return 'java.lang.Boolean'
  54. if isinstance(obj, str) or isinstance(obj, unicode):
  55. return "java.lang.String"
  56. if isinstance(obj, int):
  57. return "java.lang.Integer"
  58. if isinstance(obj, long):
  59. return "java.lang.Long"
  60. if isinstance(obj, float):
  61. return "java.lang.Double"
  62. if isinstance(obj, _jclass._JavaClass):
  63. return obj.__javaclassname__
  64. if isinstance(obj, JPackage("java").lang.Class):
  65. return obj.__class__.__javaclass__.getName()
  66. if isinstance(obj, _JWrapper):
  67. return obj.typeName
  68. raise TypeError(
  69. "Unable to determine the default type of {0}".format(obj.__class__))
  70. class JObject(_JWrapper):
  71. def __init__(self, v, tp=None):
  72. if tp is None:
  73. tp = _getDefaultTypeName(v)
  74. if isinstance(tp, _jclass._JavaClass):
  75. tp = tp.__javaclass__.getName()
  76. self.typeName = tp
  77. self._value = _jpype.convertToJValue(tp, v)