1 | package tudelft.utilities.j2p.t;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 |
|
---|
5 | import com.github.javaparser.ast.expr.FieldAccessExpr;
|
---|
6 | import com.github.javaparser.ast.expr.MethodCallExpr;
|
---|
7 |
|
---|
8 | import tudelft.utilities.j2p.formatting.ExpressionLine;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * a class that implements J2P translator given an arbitrary java class. The
|
---|
12 | * translator assumes all functions in the given class can be accessed and will
|
---|
13 | * be translated properly to the same package name in python. System classes are
|
---|
14 | * translated using the t.java translateors.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | public class Stub extends tudelft.utilities.j2p.t.java.lang.Object {
|
---|
18 | private Class<?> clazz;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | *
|
---|
22 | * @param clazz the class to be stubbed.
|
---|
23 | */
|
---|
24 | public Stub(Class<?> clazz) {
|
---|
25 | this.clazz = clazz;
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public ExpressionLine call(MethodCallExpr call) {
|
---|
30 |
|
---|
31 | // forward the translation of standard calls to Object
|
---|
32 | switch (call.getName().asString()) {
|
---|
33 | case "toString":
|
---|
34 | case "hashCode":
|
---|
35 | case "equals":
|
---|
36 | return super.call(call);
|
---|
37 | }
|
---|
38 |
|
---|
39 | return Translator.callClassFunction(call);
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public ExpressionLine getName() {
|
---|
45 | return new ExpressionLine(clazz.getSimpleName(),
|
---|
46 | Collections.emptyList(),
|
---|
47 | // HACK do not import the main class that is being defined.
|
---|
48 | // Hack because there are probably other Stubbed objects that DO need an import.
|
---|
49 | // Arrays.asList("from " + clazz.getCanonicalName() + " import "
|
---|
50 | // + clazz.getSimpleName()),
|
---|
51 | false, Collections.emptyList());
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public ExpressionLine field(FieldAccessExpr node) {
|
---|
56 | return getName().withAppend("." + node.getNameAsString());
|
---|
57 | }
|
---|
58 |
|
---|
59 | }
|
---|