翻译 Java 注释 [英] Translating Java annotations

查看:23
本文介绍了翻译 Java 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意这类似于 Jython @property SyntaxError: mismatched input '' expectingCLASS 但我使用的是 Jython 2.7.0 并且该答案引用了 2.5.2 中的特定错误

Note this is similar to Jython @property SyntaxError: mismatched input '' expecting CLASS but I am using Jython 2.7.0 and that answer references a specfic bug in 2.5.2

我有一些带有注释的 Java 代码,我试图在 Jython 中重写:

I have some Java code that has annotations that I am trying to rewrite in Jython:

@ProcessInput
public void process(SomeEvent event) {
  ...
  }

我试图将这个方法转换成 Python 而不考虑注释:

I tried to convert this method into Python leaving the annotation alone:

@ProcessInput
def process(event):

但由于另一篇文章中的错误而失败,SyntaxError: mismatched input '' expected CLASS

But that fails with the error from the other post, SyntaxError: mismatched input '' expecting CLASS

我在网上阅读了有关 Jynx 的信息(https://code.google.com/p/jynx/) 并尝试过

I read online about Jynx (https://code.google.com/p/jynx/) and tried

import jynx
from java.lang import Object
from jynx.lib.junit import*
from jynx.jannotations import*
...
ProcessInput = annotation.extract(ProcessInput)

但这没有任何作用;同样的错误.我做错了什么,或者,是否有一种简单的方法可以找出 Java 注释正在做什么并重写 Java 代码,使其不使用这种糖?

but that didn't do anything; same error. What am I doing wrong, or alternatively, is there an easy way to figure out what the Java annotation is doing and rewrite the Java code so that it does not use this sugar?

推荐答案

我把这个写在我的脑海里(很可能有错别字),所以警告空头.

I'm writing this off the top of my head (with typos most likely), so caveat emptor.

除了目前 Jython 根本无法支持 Java 注释之外,我没有太多答案.请注意,Java 注释语法与 Python 装饰器语法冲突.因此,如果没有 Jython 提供一些开箱即用的支持,我看不出这是如何解决的.

I don't have much of an answer other that, at this time, Jython simply cannot support Java annotations. Notice that Java annotation syntax collides with Python decorator syntax. Thus, I don't see how this is even solvable without Jython providing some support out of the box.

不得不依赖像 Jynx 这样的编译器中介是 IMO 的绊脚石.如果没有扎实的Java注解支持,我感觉Jython会变成死胡同.

Having to rely on a compiler intermediary like Jynx is an stumbling block IMO. Without solid Java annotation support, I feel Jython will become a dead end.

鉴于我为框架和自动化开发做了大量的 Jython 工作,这令人难过.

回答您的问题:

我做错了什么,

如果不知道你的执行产生的具体错误就很难说

It is hard to say without knowing the specific error generated by your execution of

ProcessInput = annotation.extract(ProcessInput)

ProcessInput = annotation.extract(ProcessInput)

Java 注释就像 Python 装饰器.它们都提供影响、增强或以其他方式改变带注释代码的执行(或辅助所述带注释代码的幕后代码的生成)的元数据.

Java annotations are like Python decorators. They both provide metadata that affect, enhance or otherwise alter the execution of annotated code (or the generation of behind-the-scenes code that assist the said annotated code.)

这种行为并非微不足道,至少在 Java 中,注释可能会带来更多需要导入的代码.在您的情况下,当尝试加载注释所依赖的类时,对 annotation.extract(ProcessInput) 的特定调用可能会导致 ClassNotFoundError.

This behavior is not trivial, and at least in Java, an annotation might bring a lot more code with required imports. In your case, this specific call to annotation.extract(ProcessInput) might be causing a ClassNotFoundError when trying to load classes the annotation depends on.

或者,有没有一种简单的方法来弄清楚 Java注释正在做并重写 Java 代码,使其不使用这个糖?

or alternatively, is there an easy way to figure out what the Java annotation is doing and rewrite the Java code so that it does not use this sugar?

这是一项非常重要的任务,我什至根本不会考虑它.您必须为注释和带注释的类获取生成的字节码,将它们反编译回 Java.

This is such a non-trivial task, I wouldn't even contemplate it at all. You would have to take the generated bytecode for the annotation and the annotated class, de-compile them back into Java.

然后以某种方式创建一个与 Java 注释等效的 Python 装饰器(如果可能的话),然后创建一个 Jython 类来扩展带注释的 Java 类,并以某种方式将装饰器硬塞给它.

Then somehow create a Python decorator that is equivalent to the Java annotation (if that is even possible), then create a Jython class that extends the annotated Java class and somehow shoe-horn the decorator to it.

即便如此,这还不够.请看,在 Java 中,注释驱动由 EE 容器、包装器(即休眠)、服务提供者(即 JPA 提供者)或控制平台反转(例如 Spring)执行的逻辑.

And even then, this is not enough. See, in Java, annotations drive logic executed by a EE container, a wrapper (.ie. hibernate), a service provider (.ie. a JPA provider), or an inversion of control platform (say, Spring.)

我不认为这样的努力是明智的.

I don't think such an effort is wise.

此时我们最多能做的就是在 Jython 中扩展一个带注释的 Java 类(假设该类不是 final),在 Jython 中完成大部分开发工作,并让必备的 Java 容器(或中介)启动一个Java 到 Jython 的垫片(例如,Jython 的 PyServlet 垫片)

The most we can do, at this time, is to extend an annotated java class in Jython (assuming the class is not final), do most of your development in Jython and have the prerequisite Java container (or intermediaries) launch a Java-to-Jython shim (for example, Jython's PyServlet shim)

目前尝试直接在 Jython 上使用 Java 注释是失败者的游戏(直到新版本的 Jython 出现并提供开箱即用的支持.)

Trying to work with Java annotations directly on Jython is a loser's game at this time (and until a new version of Jython comes along with out-of-the-box support.)

幸运的是,大多数 Java 注释(至少对于主要框架而言)都可以用 XML 配置覆盖.

Fortunately, most Java annotations (at least for major frameworks) can be overwritten with XML configuration.

所以,重申一下,这就是我的方法:

So, to reiterate, this would be my approach:

  1. 确定并部署支持注释所需的容器
  2. 确定必要的 Java-to-Jython shim(即 PyServlet)或编写自己的(我们必须在工作中这样做,并非微不足道,但并非不可能.)
  3. 使用 Jython 类扩展带注释的 Java 类
  4. 用 xml 覆盖注解默认值
  1. identify and deploy the container(s) needed to support the annotations
  2. identify the necessary Java-to-Jython shim (.ie. PyServlet) or write your own (we had to do that at work, not trivial, but not impossible.)
  3. extend the annotated Java classes with Jython classes
  4. overwrite annotation default values with xml

如果上述任何步骤是不可能的,那么从技术上讲,整个努力从一开始就已经死了.

If any of the steps above are impossible, then the whole endeavor is technically dead from the get-go.

祝你好运.

这篇关于翻译 Java 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆