source: java2python/src/test/java/tudelft/utilities/j2p/PyTest.java@ 353

Last change on this file since 353 was 346, checked in by wouter, 2 years ago

version 0.1 of automatic java to python translator. Can translate some simple programs, lot of work remains to be done

File size: 3.3 KB
Line 
1package tudelft.utilities.j2p;
2
3import java.io.BufferedOutputStream;
4import java.io.BufferedReader;
5import java.io.File;
6import java.io.FileNotFoundException;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.util.LinkedList;
12import java.util.List;
13
14import com.github.javaparser.JavaParser;
15import com.github.javaparser.ParseResult;
16import com.github.javaparser.ParserConfiguration;
17import com.github.javaparser.ast.CompilationUnit;
18import com.github.javaparser.symbolsolver.JavaSymbolSolver;
19import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
20import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
21
22import tudelft.utilities.j2p.formatting.Block;
23import tudelft.utilities.j2p.t.Translator;
24
25public class PyTest {
26 private static final String TESTFILE = "test.py";
27 private static final File TESTDIR = new File("test/");
28
29 /**
30 *
31 * @param pycode the python code to run
32 * @param fullclaspath a "package/class/path/filename.py" name containing
33 * the full filename where the file should be placed.
34 * This must be a RELATIVE name, we will prefix it with
35 * {@link #TESTDIR} as needed
36 * @return the text lines that the code printed to stdout.
37 * @throws Exception
38 */
39 public List<String> runPython(String pycode, String fullclaspath)
40 throws Exception {
41 File f = TESTDIR.toPath().resolve(fullclaspath).toFile();
42 f.getParentFile().mkdirs();// create dir, massaging java...
43 FileOutputStream fos = new FileOutputStream(f);
44 BufferedOutputStream bos = new BufferedOutputStream(fos);
45 // convert string to byte array
46 byte[] bytes = pycode.getBytes();
47 // write byte array to file
48 bos.write(bytes);
49 bos.close();
50 fos.close();
51
52 Process process = Runtime.getRuntime().exec("python " + fullclaspath,
53 new String[0], TESTDIR);
54 List<String> errors = readAll(process.getErrorStream());
55 if (!errors.isEmpty())
56 throw new IllegalStateException("Python failed. " + errors);
57
58 return readAll(process.getInputStream());
59 }
60
61 private List<String> readAll(InputStream in) throws IOException {
62 BufferedReader Buffered_Reader = new BufferedReader(
63 new InputStreamReader(in));
64 List<String> lines = new LinkedList<>();
65
66 String line;
67 while ((line = Buffered_Reader.readLine()) != null) {
68 lines.add(line);
69 }
70 return lines;
71 }
72
73 /**
74 * @param javaFile a java file name to be translated
75 * @return a string containing python code translated from the java.
76 * @throws FileNotFoundException
77 */
78 public String translate(String javaFile) throws FileNotFoundException {
79 ParserConfiguration conf = new ParserConfiguration();
80 CombinedTypeSolver typeSolver = new CombinedTypeSolver();
81 typeSolver.add(new ReflectionTypeSolver(false));
82 // FIXME add sources of jars source code etc
83 // typeSolver.add(new JavaParserTypeSolver(new File(SOURCE_PATH)));
84 // typeSolver.add(new JarTypeSolver(jarSourceFile));
85 JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
86 conf.setSymbolResolver(symbolSolver);
87 JavaParser parser = new JavaParser(conf);
88
89 ParseResult<CompilationUnit> res = parser.parse(new File(javaFile));
90
91 Block translation = new Translator().translate(res.getResult().get());
92 return translation.toString();
93 }
94
95}
Note: See TracBrowser for help on using the repository browser.