1 | package tudelft.utilities.j2p;
|
---|
2 |
|
---|
3 | import java.io.BufferedOutputStream;
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileNotFoundException;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.io.InputStreamReader;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import com.github.javaparser.JavaParser;
|
---|
15 | import com.github.javaparser.ParseResult;
|
---|
16 | import com.github.javaparser.ParserConfiguration;
|
---|
17 | import com.github.javaparser.ast.CompilationUnit;
|
---|
18 | import com.github.javaparser.symbolsolver.JavaSymbolSolver;
|
---|
19 | import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
|
---|
20 | import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
|
---|
21 |
|
---|
22 | import tudelft.utilities.j2p.formatting.Block;
|
---|
23 | import tudelft.utilities.j2p.t.Translator;
|
---|
24 |
|
---|
25 | public 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 | }
|
---|