source: java2python/src/main/java/tudelft/utilities/j2p/t/Stub.java@ 381

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

#116 various fixes, to support multi-file zips

File size: 1.6 KB
Line 
1package tudelft.utilities.j2p.t;
2
3import java.util.Arrays;
4import java.util.Collections;
5
6import com.github.javaparser.ast.expr.FieldAccessExpr;
7import com.github.javaparser.ast.expr.MethodCallExpr;
8
9import tudelft.utilities.j2p.formatting.ExpressionLine;
10
11/**
12 * a class that implements J2P translator given an arbitrary java class. The
13 * translator assumes all functions in the given class can be accessed and will
14 * be translated properly to the same package name in python. System classes are
15 * translated using the t.java translateors.
16 *
17 */
18public class Stub extends tudelft.utilities.j2p.t.java.lang.Object {
19 private Class<?> clazz;
20
21 /**
22 *
23 * @param clazz the class to be stubbed.
24 */
25 public Stub(Class<?> clazz) {
26 this.clazz = clazz;
27 }
28
29 @Override
30 public ExpressionLine call(MethodCallExpr call) {
31
32 // forward the translation of standard calls to Object
33 switch (call.getName().asString()) {
34 case "toString":
35 case "hashCode":
36 case "equals":
37 return super.call(call);
38 }
39
40 return Translator.callClassFunction(call);
41
42 }
43
44 @Override
45 public ExpressionLine getName() {
46 // fixme this fails for hidden classes etc. Check getCanonicalName().
47 return new ExpressionLine(clazz.getSimpleName(),
48 Arrays.asList("from " + clazz.getCanonicalName() + " import "
49 + clazz.getSimpleName()),
50 false, Collections.emptyList());
51 }
52
53 @Override
54 public ExpressionLine field(FieldAccessExpr node) {
55 return getName().withAppend("." + node.getNameAsString());
56 }
57
58 @Override
59 public java.lang.String toString() {
60 return "Stub/generated translator class for " + clazz;
61 }
62
63}
Note: See TracBrowser for help on using the repository browser.