• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tcalmant / python-javaobj / 8591112367

07 Apr 2024 07:18PM UTC coverage: 78.701%. First build
8591112367

push

github

tcalmant
Added 3.11 & 3.12 to GitHub actions

1611 of 2047 relevant lines covered (78.7%)

4.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.95
/javaobj/v1/beans.py
1
#!/usr/bin/python
2
# -- Content-Encoding: utf-8 --
3
"""
6✔
4
Definition of the beans of the v1 parser
5

6
:authors: Volodymyr Buell, Thomas Calmant
7
:license: Apache License 2.0
8
:version: 0.4.4
9
:status: Alpha
10

11
..
12

13
    Copyright 2024 Thomas Calmant
14

15
    Licensed under the Apache License, Version 2.0 (the "License");
16
    you may not use this file except in compliance with the License.
17
    You may obtain a copy of the License at
18

19
        http://www.apache.org/licenses/LICENSE-2.0
20

21
    Unless required by applicable law or agreed to in writing, software
22
    distributed under the License is distributed on an "AS IS" BASIS,
23
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
    See the License for the specific language governing permissions and
25
    limitations under the License.
26
"""
27

28
from __future__ import absolute_import
6✔
29

30
from typing import List
6✔
31
import struct
6✔
32

33
from ..utils import UNICODE_TYPE
6✔
34

35
# ------------------------------------------------------------------------------
36

37
__all__ = (
6✔
38
    "JavaArray",
39
    "JavaByteArray",
40
    "JavaClass",
41
    "JavaEnum",
42
    "JavaObject",
43
    "JavaString",
44
)
45

46
# Module version
47
__version_info__ = (0, 4, 4)
6✔
48
__version__ = ".".join(str(x) for x in __version_info__)
6✔
49

50
# Documentation strings format
51
__docformat__ = "restructuredtext en"
6✔
52

53
# ------------------------------------------------------------------------------
54

55

56
class JavaClass(object):  # pylint:disable=R0205
6✔
57
    """
58
    Represents a class in the Java world
59
    """
60

61
    def __init__(self):
6✔
62
        """
63
        Sets up members
64
        """
65
        self.name = None  # type: str
6✔
66
        self.serialVersionUID = None  # type: int  # pylint:disable=C0103
6✔
67
        self.flags = None  # type: int
6✔
68
        self.fields_names = []  # type: List[str]
6✔
69
        self.fields_types = []  # type: List[JavaString]
6✔
70
        self.superclass = None  # type: JavaClass
6✔
71

72
    def __str__(self):
6✔
73
        """
74
        String representation of the Java class
75
        """
76
        return self.__repr__()
6✔
77

78
    def __repr__(self):
6✔
79
        """
80
        String representation of the Java class
81
        """
82
        return "[{0:s}:0x{1:X}]".format(self.name, self.serialVersionUID)
6✔
83

84
    def __eq__(self, other):
6✔
85
        """
86
        Equality test between two Java classes
87

88
        :param other: Other JavaClass to test
89
        :return: True if both classes share the same fields and name
90
        """
91
        if not isinstance(other, type(self)):
6✔
92
            return False
6✔
93

94
        return (
6✔
95
            self.name == other.name
96
            and self.serialVersionUID == other.serialVersionUID
97
            and self.flags == other.flags
98
            and self.fields_names == other.fields_names
99
            and self.fields_types == other.fields_types
100
            and self.superclass == other.superclass
101
        )
102

103

104
class JavaObject(object):  # pylint:disable=R0205
6✔
105
    """
106
    Represents a deserialized non-primitive Java object
107
    """
108

109
    def __init__(self):
6✔
110
        """
111
        Sets up members
112
        """
113
        self.classdesc = None  # type: JavaClass
6✔
114
        self.annotations = []
6✔
115

116
    def get_class(self):
6✔
117
        """
118
        Returns the JavaClass that defines the type of this object
119
        """
120
        return self.classdesc
6✔
121

122
    def __str__(self):
6✔
123
        """
124
        String representation
125
        """
126
        return self.__repr__()
6✔
127

128
    def __repr__(self):
6✔
129
        """
130
        String representation
131
        """
132
        name = "UNKNOWN"
6✔
133
        if self.classdesc:
6✔
134
            name = self.classdesc.name
6✔
135
        return "<javaobj:{0}>".format(name)
6✔
136

137
    def __hash__(self):
6✔
138
        """
139
        Each JavaObject we load must have a hash method to be accepted in sets
140
        and alike. The default hash is the memory address of the object.
141
        """
142
        return id(self)
×
143

144
    def __eq__(self, other):
6✔
145
        """
146
        Equality test between two Java classes
147

148
        :param other: Other JavaClass to test
149
        :return: True if both classes share the same fields and name
150
        """
151
        if not isinstance(other, type(self)):
×
152
            return False
×
153

154
        res = (
×
155
            self.classdesc == other.classdesc
156
            and self.annotations == other.annotations
157
        )
158
        if not res:
×
159
            return False
×
160

161
        for name in self.classdesc.fields_names:
×
162
            if not getattr(self, name) == getattr(other, name):
×
163
                return False
×
164
        return True
×
165

166

167
class JavaString(UNICODE_TYPE):
6✔
168
    """
169
    Represents a Java String
170
    """
171

172
    def __hash__(self):
6✔
173
        return UNICODE_TYPE.__hash__(self)
6✔
174

175
    def __eq__(self, other):
6✔
176
        if not isinstance(other, UNICODE_TYPE):
6✔
177
            return False
6✔
178
        return UNICODE_TYPE.__eq__(self, other)
6✔
179

180

181
class JavaEnum(JavaObject):
6✔
182
    """
183
    Represents a Java enumeration
184
    """
185

186
    def __init__(self, constant=None):
6✔
187
        super(JavaEnum, self).__init__()
6✔
188
        self.constant = constant
6✔
189

190

191
class JavaArray(list, JavaObject):
6✔
192
    """
193
    Represents a Java Array
194
    """
195

196
    def __init__(self, classdesc=None):
6✔
197
        list.__init__(self)
6✔
198
        JavaObject.__init__(self)
6✔
199
        self.classdesc = classdesc
6✔
200

201
    def __hash__(self):
6✔
202
        return list.__hash__(self)
×
203

204

205
class JavaByteArray(JavaObject):
6✔
206
    """
207
    Represents the special case of Java Array which contains bytes
208
    """
209

210
    def __init__(self, data, classdesc=None):
6✔
211
        JavaObject.__init__(self)
6✔
212
        self._data = struct.unpack("b" * len(data), data)
6✔
213
        self.classdesc = classdesc
6✔
214

215
    def __str__(self):
6✔
216
        return "JavaByteArray({0})".format(self._data)
×
217

218
    def __getitem__(self, item):
6✔
219
        return self._data[item]
×
220

221
    def __iter__(self):
6✔
222
        return iter(self._data)
6✔
223

224
    def __len__(self):
6✔
225
        return len(self._data)
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc