| 29 | For usage you have the choice between translating a single file or an entire package. We generally recommend translating the entire package. |
| 30 | |
| 31 | === Single File |
| 32 | If you want to translate a single java file or even just a string containing your program, that does not need any specialized libraries, you can do the approach as many tests in core do. Check the examples. The heart of the code will look like this, where most boilerplate is about setting up the javaparser: |
| 33 | |
| 34 | {{{ |
| 35 | ParserConfiguration conf = new ParserConfiguration(); |
| 36 | CombinedTypeSolver typeSolver = new CombinedTypeSolver(); |
| 37 | typeSolver.add(new ReflectionTypeSolver(false)); |
| 38 | JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver); |
| 39 | conf.setSymbolResolver(symbolSolver); |
| 40 | JavaParser parser = new JavaParser(conf); |
| 41 | ParseResult<CompilationUnit> res = parser.parse(new File(javaFile)); |
| 42 | |
| 43 | Block translation = Translator.translate(res.getResult().get()); |
| 44 | |
| 45 | }}} |
| 46 | |
| 47 | After you get the translation, you can print it or put it in a {{{.py}}} file. |
| 48 | |
| 49 | === Entire Package |
| 50 | If you want to translate an entire package, first create a separate source directory containing all the java code that you want to translate. Also make sure that your code is compiled (eg, using the build-helper-maven-plugin , check the pom files). Then you just do |
| 51 | {{{ |
| 52 | PyProgram program = PyProgram.fromDirectory(Paths.get("src/test/myprogram")); |
| 53 | }}} |
| 54 | |
| 55 | This gives you a fully translated program that you can print or get a zip file from. To get a zip file, call |
| 56 | {{{program.getZip() }}} |
| 57 | |
| 58 | The zip file is ready for a pip install (eg in your own virtual environment) or running from java through our PythonVenv. |
| 59 | |