5

Whats the best way to generate java from python?

I want to write a decorator that generates java code to call a json version of a function (I can use existing decorators to export the json api).

Whats the best way to generate the java, should I consider stuff like FSMs here?

Ideally I can write my code once, for the server and generate code to interface with it for various languages (java first).

Edit (pulled from a comment on a deleted answer):
The java code will be running on android, while the python code will be in a django server... Also, I want to be able to statically generate the java code, and have this as part of an API that people can use.

MichaelMilom
  • 3,024
  • 1
  • 16
  • 25
Stuart Axon
  • 1,844
  • 1
  • 26
  • 44
  • 1
    The best way is not generating something this complex if you can avoid it. One hint though: AS far as I kno,w it's relatively "simple" (as opposed to all other possibilities) to generate correct (not too readable, but correct) code from something like an AST (abstract syntax tree) - at least if you know that tree format (and what it represents) well. That basically means implementing half of a java compiler though. –  Dec 01 '10 at 21:03
  • So your server generates some Java (byte?)code that the Android user will execute just to get some JSON data? – Nick T Dec 01 '10 at 21:08
  • Obviously, the bit that can generate java code would only be run when we decide to change the API, not all the time :) – Stuart Axon Dec 01 '10 at 21:13
  • @delnan AST definitely seems along the right lines, maybe I can use a java one from jython. – Stuart Axon Dec 01 '10 at 21:43

3 Answers3

2

You can always create java code the same way webapps create HTML: with a template. Then you can have the java source code compiled into bytecode using a regular Java compiler ( see: Package javax.tools )

Not necessarily the best option, but definitely an option ( and quite simple btw )

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1

Reuse is good. Complexity is a price.

It will make you pay twice or more later on. Measure your ROI (Return on Investment) before you make a leap.

[Edit:]

  1. You could use Jython which will make it easy to write glue code as calling Java functions.
  2. jythonc transforms Python source code into Java source code then invokes a Java compiler to turn it into .class files.
  3. As said, you could use templating. Some one did use cheetah for that purpose. Though the link is not available any more.
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • 1
    Network programming can be a nightmare to debug, to generate all this glue will save a lot of hassle. – Stuart Axon Dec 01 '10 at 21:14
  • @Stuart Axon: +1 Very true! I don't dispute it at all. We have to be a judge of that. How ever, I have seen cost folks have paid for making hasty decision specially with glue codes. It really depends on what is being achieved here. The complexity of json APIs vs the complexity of glue code. I just have a gut feeling that complexity of json APIs would be lesser as Python has excellent tools in it's arsenal. – pyfunc Dec 01 '10 at 21:18
  • It's more generating the code to call it from java/android that I'm thinking of than so much the python side. I'll investigate jythonc... for the moment I'm in an investigation phase, if this seems like a dead end then it's not too much of a loss. – Stuart Axon Dec 01 '10 at 21:42
-2

My advice? Have it generate JavaScript code instead of Java code. Android can execute JavaScript code using a WebView.

Powerlord
  • 87,612
  • 17
  • 125
  • 175