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

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

#115 numerous fixes. ActionUse is now starting in python (not yet running ok)

File size: 3.4 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 /**
46 * @return the java source file of the program, relative to the root of the
47 * source (typically src/main/java or so). This relativeness allows
48 * us to derive the correct filename, class path etc, without having
49 * to provide an actual class object.
50 */
51 public File getSourceFile() {
52 return source;
53 }
54
55 /**
56 *
57 * @param javaBaseDir the java base dir from where packages are found,
58 * typically src/main/java
59 * @param sourcefile the java source file of the program, relative to the
60 * root of the source (typically src/main/java or so).
61 * This relativeness allows us to derive the correct
62 * filename, class path etc, without having to provide an
63 * actual class object.
64 * @return PyModule containing the translated java file
65 * @throws FileNotFoundException
66 */
67 public static PyModule fromJavaFile(Path javaBaseDir, File source)
68 throws FileNotFoundException {
69 File javaFile = javaBaseDir.resolve(source.getPath()).toFile();
70 if (!javaFile.exists())
71 throw new FileNotFoundException("There is no file " + source
72 + " in the directory " + javaBaseDir);
73
74 ParserConfiguration conf = new ParserConfiguration();
75 CombinedTypeSolver typeSolver = new CombinedTypeSolver();
76 typeSolver.add(new ReflectionTypeSolver(false));
77 JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
78 conf.setSymbolResolver(symbolSolver);
79 JavaParser parser = new JavaParser(conf);
80
81 ParseResult<CompilationUnit> res = parser.parse(javaFile);
82 try {
83 Block translation = Translator.translate(res.getResult().get());
84 return new PyModule(translation, source);
85 } catch (Exception e) {
86 throw new IllegalArgumentException("Failed to translate " + source,
87 e);
88 }
89 }
90
91 @Override
92 public String toString() {
93 return "PyModule from " + source + ":\n" + code.toCode(
94 "from " + source.getPath().replace("/", ".") + " import .*");
95 }
96
97}
Note: See TracBrowser for help on using the repository browser.