jbfaden / javajythonconverter Goto Github PK
View Code? Open in Web Editor NEWConverter from Java to Jython (and close to Python)
License: GNU General Public License v3.0
Converter from Java to Jython (and close to Python)
License: GNU General Public License v3.0
int parseInt( String s )
and
int parseInt( String s, int deft )
must be merged in Python. This is probably best for the human reviewer to do, but it's worth considering.
This conditional expression still causes problems with Jython 2.2:
int daysInMonth = time[1] == 0 ? 31 : TimeUtil.DAYS_IN_MONTH[isLeapYear(time[0]) ? 1 : 0][time[1]];
Python 3.10 has a nice match statement which is roughly a superset of a case statement's functionality. The match statement should be used when this is a common target.
I think JavaScript Math.floor can be used in floorDiv.
s.charAt(2)==65 is going to need attention, because Python will interpret the string as a number, rather than using the ASCII value.
I was able to convert a Java code to an Autoplot Jython code almost trivially, but there were two small things it missed: import java.awt.* didn't translate and this then didn't call self.getBounds.
import java.awt.*; // Java abstract window toolkit
public class Snowperson extends Frame {
public static void main(String[] args) {
Snowperson c = new Snowperson (); // create a window
c.setSize (400, 400); // set its size
c.setBackground (Color.white); // set the background color
c.show (); // display the window
}
public void paint (Graphics g) {
Rectangle bb = getBounds ();
bb.x = 0;
bb.y = 0;
paintBox (g, bb);
}
public static void paintBox (Graphics g, Rectangle bb) {
// Mr. Snowperson
int personWidth = 30 * bb.width / 100;
int personHeight = bb.height;
int personx = bb.x + 20 * bb.width / 100;
int persony = bb.y;
Rectangle mrbox =
new Rectangle (personx, persony, personWidth, personHeight);
paintSnowperson (g, mrbox);
// Mrs. Snowperson
personWidth = 90 * personWidth / 100;
personHeight = 90 * bb.height / 100;
personx = 50 * bb.width / 100;
persony = bb.height - personHeight;
Rectangle mrsbox =
new Rectangle (personx, persony, personWidth, personHeight);
paintSnowperson (g, mrsbox);
}
public static void paintSnowperson (Graphics g, Rectangle bb) {
if (bb.height < 10) return; // refuse to draw very small snowpeople
int headheight = 20 * bb.height / 100;
int midheight = 30 * bb.height / 100;
int botheight = bb.height - midheight - headheight;
int headwidth = 30 * bb.width / 100;
int midwidth = 60 * bb.width / 100;
int headgap = (bb.width - headwidth) / 2;
int midgap = (bb.width - midwidth) / 2;
// top oval and face
g.drawOval (bb.x + headgap, bb.y, headwidth, headheight);
Rectangle headBox =
new Rectangle (bb.x + headgap, bb.y, headwidth, headheight);
paintFace (g, headBox);
// middle oval and rings
g.drawOval (bb.x + midgap, bb.y + headheight, midwidth, midheight);
Rectangle thoraxBox =
new Rectangle (bb.x + midgap, bb.y + headheight, midwidth, midheight);
paintRings (g, thoraxBox);
// bottom oval and baby snowpeople
int abdomeny = bb.y + headheight + midheight;
g.drawOval (bb.x, abdomeny, bb.width, botheight);
Rectangle abdomenBox =
new Rectangle (bb.x + bb.width/3, abdomeny, bb.width/3, botheight);
paintSnowperson (g, abdomenBox);
}
public static void paintFace (Graphics g, Rectangle bb) {
int eyeheight = 20 * bb.height / 100;
int eyewidth = 20 * bb.width / 100;
int gap = (bb.width - 2 * eyewidth) / 3;
int brow = 25 * bb.height / 100;
// just draw two eyes
g.fillOval (bb.x + gap, bb.y + brow, eyewidth, eyeheight);
g.fillOval (bb.x + 2 * gap + eyewidth, bb.y + brow, eyewidth, eyeheight);
}
public static void paintRings (Graphics g, Rectangle bb) {
// Polite methods avoid doing things like changing the current color,
// because the calling method might not like it. Before we change the
// color, we stow the old value.
Color oldColor = g.getColor ();
int ringWidth = 30 * bb.width/100;
int ring_height = 60 * bb.height/100;
int gap = (bb.width - 3 * ringWidth) / 2;
int xx = bb.x;
int yy = bb.y;
g.setColor (Color.blue);
g.drawOval (xx, yy, ringWidth, ring_height);
xx += ringWidth + gap;
g.setColor (Color.black);
g.drawOval (xx, yy, ringWidth, ring_height);
xx += ringWidth + gap;
g.setColor (Color.red);
g.drawOval (xx, yy, ringWidth, ring_height);
yy = bb.y + bb.height - ring_height;
xx = bb.x + (bb.width - 2 * ringWidth - gap) / 2;
g.setColor (Color.yellow);
g.drawOval (xx, yy, ringWidth, ring_height);
xx += ringWidth + gap;
g.setColor (Color.green);
g.drawOval (xx, yy, ringWidth, ring_height);
// before returning we restore the original color
g.setColor (oldColor);
}
}
System.err.println( a/b );
converts to
sys.err.write( a/b + '\n' );
which is not valid.
I made the early mistake of not worrying about static fields vs class fields, and I realize now I need to address this. Presently it replaces field access with ClassName.field, when sometimes it should be self.field.
I should create a nice demo of this with a Java class which contains a static field constant and a class field.
URITemplateTest has a little bit of JSON handling, which could be automatically converted for Python 3.6. I did this by hand, but for other applications this would be helpful.
complex javascript code is used for addall, when concat does the trick.
I've been working on Java to JavaScript now, and this morning on String.format, which is used all over the place in the TimeUtil and URI_Templates codes.
I just discovered padstart: '123'.padstart(9,'0') will return '000000123' and should work nicely.
It would be nice if one Java static method could be pasted into the converter, but I can't figure out how to compile this. I started to handle this by wrapping the code in a "class Foo {" code, but the result has a reference to the Foo class. I need to either remove the method or provide a way to conveniently wrap the class.
A for loop is converted to a Python while loop, but if a continue statement is within the loop the loop variable is not incremented.
for ( int i=0; i<10; i++ ) {
if ( i<3 ) continue;
System.err.println("i="+i);
}
Java allows a class field and a method to have the same name. I had the variable "format" and a method "format", and in Python this resulted in my field definition to be clobbered by the field definition without a compile error. This was a very confusing bug to figure out.
The converter needs to check for name conflicts like this, and at least throw an error.
The following Java code has two methods so that a default value of offset equal to 0 is
available:
public class DefaultArg {
public static String method( String arg ) {
return method( arg, 0 );
}
public static String method( String arg, int offset ) {
return arg.substring(offset);
}
}
This should be translated to Jython/Python with:
def method( arg, offset=0 ):
return arg[offset:]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.