1 package net.sourceforge.jenesis4java.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 import java.util.Calendar;
40
41 import net.sourceforge.jenesis4java.ArrayType;
42 import net.sourceforge.jenesis4java.CalendarClassType;
43 import net.sourceforge.jenesis4java.ClassType;
44 import net.sourceforge.jenesis4java.CodeWriter;
45 import net.sourceforge.jenesis4java.ReplacingVisitor;
46 import net.sourceforge.jenesis4java.Import;
47 import net.sourceforge.jenesis4java.PrimitiveType;
48 import net.sourceforge.jenesis4java.Type;
49 import net.sourceforge.jenesis4java.impl.util.VisitorUtils;
50
51
52
53
54 public abstract class MType extends MVM.MCodeable {
55
56
57
58
59 static class MArrayType extends MType implements ArrayType {
60
61 Type type;
62
63 int dims;
64
65 public MArrayType(MVM vm, Type type, int dims) {
66 super(vm, Type.ARRAY, type.toString());
67 this.type = type;
68 this.dims = dims;
69 }
70
71 @Override
72 public Type getComponentType() {
73 return type;
74 }
75
76 @Override
77 public int getDims() {
78 return dims;
79 }
80
81 @Override
82 public String getName() {
83 return name;
84 }
85
86 @Override
87 public CodeWriter toCode(CodeWriter out) {
88 out.queue(comment);
89 type.toCode(out);
90 for (int i = 0; i < dims; i++) {
91 out.write("[]");
92 }
93 return out;
94 }
95
96 @Override
97 public void visit(ReplacingVisitor visitor) {
98 super.visit(visitor);
99 type = VisitorUtils.visit(type, this, visitor);
100 }
101 }
102
103 static class MCalendarClassType extends MType implements ClassType, CalendarClassType {
104
105 private final String pattern;
106
107 public MCalendarClassType(MVM vm, String pattern) {
108 super(vm, Type.CLASS, Calendar.class.getName());
109 this.pattern = pattern;
110 }
111
112 @Override
113 public String getName() {
114 return Calendar.class.getName();
115 }
116
117 @Override
118 public String getPattern() {
119 return pattern;
120 }
121
122 @Override
123 public void visit(ReplacingVisitor visitor) {
124 super.visit(visitor);
125 }
126 }
127
128
129
130
131 static class MClassType extends MType implements ClassType {
132
133 MClassType(MVM vm, String name) {
134 super(vm, Type.CLASS, name);
135 }
136
137 @Override
138 public String getName() {
139 return name;
140 }
141
142 @Override
143 public void visit(ReplacingVisitor visitor) {
144 super.visit(visitor);
145 }
146 }
147
148
149
150
151 static class MPrimitiveType extends MType implements PrimitiveType {
152
153 MPrimitiveType(MVM vm, int type, String name) {
154 super(vm, type, name);
155 }
156
157 @Override
158 public void visit(ReplacingVisitor visitor) {
159 super.visit(visitor);
160 }
161 }
162
163 private static final String JAVA_LANG_PACKAGE = "java.lang.";
164
165 private int type;
166
167 String name;
168
169 MType(MVM vm, int type, String name) {
170 super(vm);
171 this.type = type;
172 this.name = name;
173 }
174
175 public boolean isArray() {
176 return type == Type.ARRAY;
177 }
178
179 public boolean isPrimitive() {
180 return type != Type.CLASS && type != Type.ARRAY;
181 }
182
183 @Override
184 public CodeWriter toCode(CodeWriter out) {
185 out.queue(comment);
186 boolean foundInImportedTypes = isImportedType(out);
187 if (foundInImportedTypes) {
188 out.write(name.substring(name.lastIndexOf('.') + 1));
189 } else {
190 out.write(name);
191 }
192 return out;
193 }
194
195 @Override
196 public String toString() {
197 return name;
198 }
199
200 public int type() {
201 return type;
202 }
203
204 @Override
205 public void visit(ReplacingVisitor visitor) {
206 super.visit(visitor);
207 }
208
209 private boolean isImportedType(CodeWriter out) {
210 if (name.indexOf('.') > 0) {
211 if (name.startsWith(MType.JAVA_LANG_PACKAGE)) {
212 return true;
213 }
214 if (out.getCompilationUnit() != null) {
215 for (Import importedType : out.getCompilationUnit().getImports()) {
216 if (importedType.getName().equals(name)) {
217 return true;
218 }
219 if (importedType.getName().endsWith(".*")) {
220 String packageName = name.substring(0, name.lastIndexOf('.') + 1);
221 if ((packageName + "*").equals(importedType.getName())) {
222 return true;
223 }
224 }
225 }
226 }
227 }
228 return false;
229 }
230 }