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
I have some Java code that has annotations that I am trying to rewrite in Jython:
@ProcessInput
public void process(SomeEvent event) {
...
}
I tried to convert this method into Python leaving the annotation alone:
@ProcessInput
def process(event):
But that fails with the error from the other post, SyntaxError: mismatched input '' expecting CLASS
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)
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.
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.
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.
Which is sad given I do a substantial amount of Jython work for framework and automation development.
To your questions:
It is hard to say without knowing the specific error generated by your execution of
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.)
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.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.
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.
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.
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)
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.)
Fortunately, most Java annotations (at least for major frameworks) can be overwritten with XML configuration.
So, to reiterate, this would be my approach:
If any of the steps above are impossible, then the whole endeavor is technically dead from the get-go.
Good luck.