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

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

#115 working at translators for annotations

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