Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

When subclassing a JavaClass, provide helpful exception #23

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion rubicon/java/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,11 @@ class JavaClass(type):
# This class returns known JavaClass instances where possible.
_class_cache = {}

def __new__(cls, descriptor):
def __new__(cls, descriptor, bases=None, attrs=None):
if bases or attrs:
# The user is trying to subclass a JavaClass. This will not work because
# rubicon-java relies on the Java reflection APIs, which do not support that.
raise NotImplementedError("Unable to subclass a JavaClass.")
# print ("Creating Java class", descriptor)
# Using str descriptor as cache key, to avoid .encode() when checking cache.
if descriptor in cls._class_cache:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_rubicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def test_simple_object(self):
# with self.assertRaises(Exception):
# stack.pop()

def test_subclass_impossible(self):
def make_subclass():
Stack = JavaClass('java/util/Stack')
class ImpossibleStackSubclass(Stack):
pass

self.assertRaises(NotImplementedError, make_subclass)

def test_field(self):
"A field on an instance can be accessed and mutated"
Example = JavaClass('org/beeware/rubicon/test/Example')
Expand Down