java2python
java2python copied to clipboard
j2py fails to convert
Hi I tried to use java2python on this library and it fails to do the convertion for some of the files.
For example, <src/model/Dof.java>
and <src/model/FeLoadData.java>
are converted without problems, but <src/model/FeModelData.java>
fails with the following traceback.
I'm on Linux 64bit. and I have Installed j2py 0.5.1.
any ideas?
P.
Traceback (most recent call last):
File "/usr/bin/j2py", line 259, in <module>
sys.exit(runMain(configScript(sys.argv[1:])))
File "/usr/bin/j2py", line 57, in runMain
return runOneOrMany(options)
File "/usr/bin/j2py", line 83, in runOneOrMany
return runTransform(options)
File "/usr/bin/j2py", line 135, in runTransform
module.walk(tree)
File "/usr/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/usr/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/usr/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/usr/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/usr/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/usr/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 83, in walk
visitor = self.accept(tree, memo)
File "/usr/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 43, in accept
return call(node, memo)
File "/usr/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 443, in acceptFor
if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
AttributeError: 'NoneType' object has no attribute 'children'
I reduced the example to the following:
public class FeModelData {
public void setNodeCoords() {
for (int i = 0; i < 3; i++) i = 0;
}
}
From here results that a for
statement without brackets can not be handled.
The Java AST looks like this:
FOR for [line=3, start=32, stop=60]
FOR_INIT [start=35, stop=41]
VAR_DECLARATION [start=35, stop=41]
LOCAL_MODIFIER_LIST [start=35, stop=34]
TYPE [start=35]
INT int [line=3, start=35]
VAR_DECLARATOR_LIST [start=37, stop=41]
VAR_DECLARATOR [start=37, stop=41]
IDENT i [line=3, start=37]
EXPR [start=41]
DECIMAL_LITERAL 0 [line=3, start=41]
FOR_CONDITION [start=44, stop=48]
EXPR [start=44, stop=48]
LESS_THAN < [line=3, start=44, stop=48]
IDENT i [line=3, start=44]
DECIMAL_LITERAL 3 [line=3, start=48]
FOR_UPDATE [start=51, stop=52]
EXPR [start=51, stop=52]
POST_INC [line=3, start=51, stop=52]
IDENT i [line=3, start=51]
EXPR [start=55, stop=60]
ASSIGN = [line=3, start=55, stop=59]
IDENT i [line=3, start=55]
DECIMAL_LITERAL 0 [line=3, start=59]
There is no BLOCK_SCOPE
and one is expected.
I was having a similar issue when attempting to utilize j2py.
Ensuring all conditionals and loops are defined with curly braces resolves the issue.
Thanks for the tip @iuliux
It looks like for(..) and if ( ..) blocks without brackets need to generate a BLOCKSCOPE..
@natural have any idea where someone would need to start to
i add failing tests for this ii implement ?
@gul2u @iuliux - I guess using a java formatter/beautifier on the code base as a preprocessing step might work while the bug exists.
Have worked out how to make the simple example -
for(int i = 0; i < 3; i++) System.out.println("Hello world");
Not crash ... though not how to output the SOPL yet ..
Tried this - no crash - but not sure how to output the actual statement in the for loop:
diff --git a/home/stuaxo/.virtualenvs/tmp/lib/python2.7/site-packages/java2python/compiler/visitor.py b/java2python/compiler/visitor.py
index cdac7d0..4109883 100644
--- a/home/stuaxo/.virtualenvs/tmp/lib/python2.7/site-packages/java2python/compiler/visitor.py
+++ b/java2python/compiler/visitor.py
@@ -440,13 +440,10 @@ class MethodContent(Base):
else:
whileStat.expr.walk(cond, memo)
whileBlock = self.factory.methodContent(parent=self)
- if node.children[3].type == tokens.EXPR:
- whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE), memo)
+ if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
+ self.factory.expr(left='pass', parent=whileBlock)
else:
- if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
- self.factory.expr(left='pass', parent=whileBlock)
- else:
- whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE), memo)
+ whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE), memo)
updateStat = self.factory.expr(parent=whileBlock)
updateStat.walk(node.firstChildOfType(tokens.FOR_UPDATE), memo)
I ran into this also - as a workaround as suggested I used a code beautifier. I used this one in particular: http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html
You can run as follows for example, this worked for me, note it will overwrite the existing files so keep a copy of them (it also saves them with a new extension in that same directory):
jacobe -cfg=sun.cng -overwrite ../dirtofiles/*.java
Hi did you solve the problem? I have used java formatter and having all {} for loops still don't work. HELP!