/
_jexception.py
113 lines (94 loc) · 3.77 KB
/
_jexception.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# *****************************************************************************
import _jpype
import sys as _sys
from . import _jcustomizer
from . import _jclass
from . import _jobject
__all__ = ['JException']
if _sys.version > '3':
_unicode = str
else:
_unicode = unicode
class _JException(object):
""" Base class for all ``java.lang.Throwable`` objects.
When called as an object ``JException`` will produce a new exception class.
The arguments may either be a string or an existing Java throwable.
This functionality is deprecated as exception classes can be created with
``JClass``.
Use ``issubclass(cls, JException)`` to test if a class is derived
from ``java.lang.Throwable.``
Use ``isinstance(obj, JException)`` to test if an object is a
``java.lang.Throwable``.
"""
def __new__(cls, *args, **kwargs):
if cls == JException:
import warnings
if not hasattr(JException, '_warned'):
warnings.warn("Using JException to construct an exception type is deprecated.",
category=DeprecationWarning, stacklevel=2)
JException._warned = True
return _JExceptionClassFactory(*args, **kwargs)
return super(JException, cls).__new__(cls)
def __init__(self, *args, **kwargs):
if hasattr(self, '__javavalue__'):
pass
elif len(args) == 1 and isinstance(args[0], _jpype.PyJPValue):
self.__javavalue__ = args[0]
else:
self.__javavalue__ = self.__class__.__javaclass__.newInstance(
*args)
super(Exception, self.__class__).__init__(self)
def __str__(self):
return str(self.getMessage())
# Included for compatibility with JPype 0.6.3
def message(self):
return str(self.getMessage())
# Included for compatibility with JPype 0.6.3
def stacktrace(self):
""" Get a string listing the stack frame.
Returns:
A string with the classic Java ``printStackTrace`` result.
"""
StringWriter = _jclass.JClass("java.io.StringWriter")
PrintWriter = _jclass.JClass("java.io.PrintWriter")
sw = StringWriter()
pw = PrintWriter(sw)
self.printStackTrace(pw)
pw.flush()
r = sw.toString()
sw.close()
return r
# For compatiblity with python exceptions
@property
def args(self):
return self._jargs()
# args = property(lambda self: self._jargs(), None)
# """ Test doc string on property"""
def _jargs(self):
cause = self.getCause()
if cause is None:
return (str(self.getMessage()),)
return (str(self.getMessage()), cause,)
JException = _jobject.defineJObjectFactory("JException", "java.lang.Throwable",
_JException, bases=(Exception, _jobject.JObject))
_jcustomizer.registerClassBase('java.lang.Throwable', JException)
def _JExceptionClassFactory(tp):
if isinstance(tp, (str, _unicode)):
return _jclass.JClass(tp)
if isinstance(tp, _jclass.JClass):
return _jclass.JClass(tp.__javaclass__)
raise TypeError(
"JException requires a string or java throwable type, got %s." % tp)