如何在 Jython 解释器中将 Jython 类转换为 Java 类?

发布于 2024-09-26 13:23:40 字数 1074 浏览 1 评论 0原文

我正在尝试使用 Esper API 注册用户定义函数。它需要类或字符串类型争论

http://esper.codehaus.org/esper-4.0.0/doc/api/com/espertech/esper/client/ConfigurationOperations.html#addImport(java.lang.String)

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

http://esper.codehaus.org/esper-4.0.0/doc/api/com/espertech/esper/client/ConfigurationOperations.html#addImport(java.lang.String)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千紇 2024-10-03 13:23:40

我认为这是不可能的。 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文