1 | package tudelft.utilities.j2p;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.nio.file.Path;
|
---|
6 |
|
---|
7 | import com.github.javaparser.JavaParser;
|
---|
8 | import com.github.javaparser.ParseResult;
|
---|
9 | import com.github.javaparser.ParserConfiguration;
|
---|
10 | import com.github.javaparser.ast.CompilationUnit;
|
---|
11 | import com.github.javaparser.symbolsolver.JavaSymbolSolver;
|
---|
12 | import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
|
---|
13 | import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
|
---|
14 |
|
---|
15 | import tudelft.utilities.j2p.formatting.Block;
|
---|
16 | import tudelft.utilities.j2p.t.Translator;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * A PyModule contains a python "file". This is the python equivalent to a java
|
---|
20 | * class "file".
|
---|
21 | */
|
---|
22 | public class PyModule {
|
---|
23 | private final Block code;
|
---|
24 | private final File source;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | *
|
---|
28 | * @param pycode the python code to be zipped as pip-installable zip. It
|
---|
29 | * is assumed that the code is just 1 file.
|
---|
30 | * @param sourcefile the java source file of the program, relative to the
|
---|
31 | * root of the source (typically src/main/java or so).
|
---|
32 | * This relativeness allows us to derive the correct
|
---|
33 | * filename, class path etc, without having to provide an
|
---|
34 | * actual class object.
|
---|
35 | */
|
---|
36 | public PyModule(Block code, File sourcefile) {
|
---|
37 | this.code = code;
|
---|
38 | this.source = sourcefile;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public Block getCode() {
|
---|
42 | return code;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public File getSourceFile() {
|
---|
46 | return source;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | *
|
---|
51 | * @param javaBaseDir the java base dir from where packages are found,
|
---|
52 | * typically src/main/java
|
---|
53 | * @param sourcefile the java source file of the program, relative to the
|
---|
54 | * root of the source (typically src/main/java or so).
|
---|
55 | * This relativeness allows us to derive the correct
|
---|
56 | * filename, class path etc, without having to provide an
|
---|
57 | * actual class object.
|
---|
58 | * @return PyModule containing the translated java file
|
---|
59 | * @throws FileNotFoundException
|
---|
60 | */
|
---|
61 | public static PyModule fromJavaFile(Path javaBaseDir, File source)
|
---|
62 | throws FileNotFoundException {
|
---|
63 | File javaFile = javaBaseDir.resolve(source.getPath()).toFile();
|
---|
64 | if (!javaFile.exists())
|
---|
65 | throw new FileNotFoundException("There is no file " + source
|
---|
66 | + " in the directory " + javaBaseDir);
|
---|
67 |
|
---|
68 | ParserConfiguration conf = new ParserConfiguration();
|
---|
69 | CombinedTypeSolver typeSolver = new CombinedTypeSolver();
|
---|
70 | typeSolver.add(new ReflectionTypeSolver(false));
|
---|
71 | JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
|
---|
72 | conf.setSymbolResolver(symbolSolver);
|
---|
73 | JavaParser parser = new JavaParser(conf);
|
---|
74 |
|
---|
75 | ParseResult<CompilationUnit> res = parser.parse(javaFile);
|
---|
76 | try {
|
---|
77 | Block translation = Translator.translate(res.getResult().get());
|
---|
78 | return new PyModule(translation, source);
|
---|
79 | } catch (Exception e) {
|
---|
80 | throw new IllegalArgumentException("Failed to translate " + source,
|
---|
81 | e);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|