def registerClassImplementation(classname, proto): """ (internal) Add an implementation for a class Use @JImplementationFor(cls) to access this. """ if classname in _JP_IMPLEMENTATIONS: _JP_IMPLEMENTATIONS[classname].append(proto) else: _JP_IMPLEMENTATIONS[classname] = [proto] # If we have already created a class, apply it retroactively. if _jpype._hasClass(classname): _applyCustomizerPost(_jpype._getClass(classname), proto)
def __new__(cls, jc, loader=None, initialize=True): if loader and isinstance(jc, str): jc = _jpype._java_lang_Class.forName(jc, initialize, loader) # Handle generics if isinstance(jc, str) and jc.endswith(">"): i = jc.find("<") params = jc[i + 1:-1] ret = _jpype._getClass(jc[:i]) acceptParams = len(ret.class_.getTypeParameters()) if acceptParams == 0: raise TypeError("Java class '%s' does not take parameters" % (ret.__name__)) if len(params) > 0: params = params.split(',') if len(params) > 0 and len(params) != len( ret.class_.getTypeParameters()): raise TypeError("Java generic class '%s' length mismatch" % (ret.__name__)) return ret # Pass to class factory to create the type return _jpype._getClass(jc)
def testGetClass(self): with self.assertRaisesRegex(TypeError, "not found"): _jpype._getClass("not.a.class")
def testGetClass(self): self.assertIsInstance(_jpype._getClass("java.lang.String"), _jpype._JClass) with self.assertRaises(TypeError): _jpype._getClass(object())