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

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

#115 changed J2P getName() to getName(fullname) and changed all code accordingly. Also cleaned up much of the code.

File size: 2.0 KB
Line 
1package tudelft.utilities.j2p.t;
2
3import com.github.javaparser.ast.Node;
4import com.github.javaparser.ast.expr.AnnotationExpr;
5import com.github.javaparser.ast.expr.FieldAccessExpr;
6import com.github.javaparser.ast.expr.MethodCallExpr;
7import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
8
9import tudelft.utilities.j2p.formatting.Block;
10import tudelft.utilities.j2p.formatting.ExpressionLine;
11
12/**
13 * a class that implements J2P translator given an arbitrary java class. The
14 * translator assumes all functions in the given class can be accessed and will
15 * be translated properly to the same package name in python. System classes are
16 * translated using the t.java translateors.
17 *
18 */
19public class Stub extends tudelft.utilities.j2p.t.java.lang.Object {
20 private Class<?> clazz;
21
22 /**
23 *
24 * @param clazz the class to be stubbed.
25 */
26 public Stub(Class<?> clazz) {
27 this.clazz = clazz;
28 }
29
30 @Override
31 public ExpressionLine call(MethodCallExpr call)
32 throws TranslationException {
33
34 // forward the translation of standard calls to Object
35 switch (call.getName().asString()) {
36 case "toString":
37 case "hashCode":
38 case "equals":
39 return super.call(call);
40 }
41
42 return Translator.callClassFunction(call);
43
44 }
45
46 @Override
47 public ExpressionLine getName(boolean fullname) {
48 ExpressionLine base = ExpressionLine.EMPTY
49 .withImport("from " + clazz.getCanonicalName() + " import "
50 + clazz.getSimpleName());
51 return makeName(base, clazz.getName(), fullname);
52 }
53
54 @Override
55 public ExpressionLine field(FieldAccessExpr node) {
56 return getName(false).withAppend("." + node.getNameAsString());
57 }
58
59 @Override
60 public java.lang.String toString() {
61 return "Stub/generated translator class for " + clazz;
62 }
63
64 @Override
65 public <T extends Node & NodeWithAnnotations<?>> Block annotate(T code,
66 AnnotationExpr annotation) throws TranslationException {
67 throw new TranslationException("There is no translator for " + this
68 + " and the default translator does not support annotations",
69 code);
70 }
71}
Note: See TracBrowser for help on using the repository browser.