翻译Java批注 [英] Translating Java annotations

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

问题描述

请注意,这类似于 Jython @property SyntaxError:输入不匹配" CLASS ,但我使用的是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 '' 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?

推荐答案

我正在写这篇文章(很容易出现拼写错误),所以 caveat emptor .

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工作,这让我感到很难过.

Which is sad given I do a substantial amount of Jython work for framework and automation development.

对您的问题:

我在做什么错了,

What am I doing wrong,

在不知道执行

ProcessInput =注解.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类(假设该类不是最终的),在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到Jython填充程序(即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天全站免登陆