source: java2python/src/main/java/tudelft/utilities/j2p/PyModule.java@ 383

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

#115 numerous fixes: UniaryExpression, String.matches, throws, etc. ActionTest now compiles

File size: 2.9 KB
Line 
1package tudelft.utilities.j2p;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.nio.file.Path;
6
7import com.github.javaparser.JavaParser;
8import com.github.javaparser.ParseResult;
9import com.github.javaparser.ParserConfiguration;
10import com.github.javaparser.ast.CompilationUnit;
11import com.github.javaparser.symbolsolver.JavaSymbolSolver;
12import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
13import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
14
15import tudelft.utilities.j2p.formatting.Block;
16import 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 */
22public 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}
Note: See TracBrowser for help on using the repository browser.