如何在 Jython 解释器中将 Jython 类转换为 Java 类?
我正在尝试使用 Esper API 注册用户定义函数。它需要类或字符串类型争论
class MyUdf():
@staticmethod
def udf():
return 50
conf.addImport(myudf.getClass().getName())
错误消息
AttributeError: class MyUdf has no attribute 'getClass'
I can import java class by
from java.lang import Math
conf.addImport(Math)
@larsmans: class 似乎只存在于 Java Class 类中
class MyUdf():
@staticmethod
def udf():
return 50
def main():
a = 'abc'
print a.__class__
u = MyUdf
print u.__class__
Traceback (most recent call last):
line 79, in main print u.__class__ AttributeError: class MyUdf has no attribute '__class__'
I am trying to register a User Defined Function with Esper API. It take a class or string type arguement
class MyUdf():
@staticmethod
def udf():
return 50
conf.addImport(myudf.getClass().getName())
The error message
AttributeError: class MyUdf has no attribute 'getClass'
I can import java class by
from java.lang import Math
conf.addImport(Math)
@larsmans: class seems only exists in Java Class class
class MyUdf():
@staticmethod
def udf():
return 50
def main():
a = 'abc'
print a.__class__
u = MyUdf
print u.__class__
Traceback (most recent call last):
line 79, in main print u.__class__ AttributeError: class MyUdf has no attribute '__class__'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是不可能的。 Jython 类不是 Java 类,据我所知,没有纯 jython 机制来验证它。
一般来说,我会说你应该采用对象工厂方法 中提到的Jython 书籍,并与代理类(您将作为参数传递的内容)结合起来。
然而,该方法涉及编写大量 Java,而且在您的情况下,用 Java 编写 MyUdf 类并使用它似乎会更简单。
或者,您也许可以通过动态字节码生成来做一些事情,但这是一个全新的兔子洞......
I don't think this is possible. Jython classes are not Java classes, and as far as I can tell there's no pure-jython mechanism to corce it.
Generally, I would say that you should take the object factory method mentioned in the Jython book, and combine with a proxy class, which is what you'd pass as the parameter.
However, that method involves writing lots of Java, and it seems that in your case it would simpler to just write the MyUdf class in Java and be done with it.
Alternatively, you might be able to do something with dynamic bytecode generation, but that's a whole new rabbit hole...