View Javadoc
1   package net.sourceforge.jenesis4java.impl;
2   
3   /*
4    * #%L
5    * Jenesis 4 Java Code Generator
6    * %%
7    * Copyright (C) 2000 - 2015 jenesis4java
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  /**
26   * Copyright (C) 2008, 2010 Richard van Nieuwenhoven - ritchie [at] gmx [dot] at
27   * Copyright (C) 2000, 2001 Paul Cody Johnston - pcj@inxar.org <br>
28   * This file is part of Jenesis4java. Jenesis4java is free software: you can
29   * redistribute it and/or modify it under the terms of the GNU Lesser General
30   * Public License as published by the Free Software Foundation, either version 3
31   * of the License, or (at your option) any later version.<br>
32   * Jenesis4java is distributed in the hope that it will be useful, but WITHOUT
33   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
35   * details.<br>
36   * You should have received a copy of the GNU Lesser General Public License
37   * along with Jenesis4java. If not, see <http://www.gnu.org/licenses/>.
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   * Standard {@code Type} implementations.
53   */
54  public abstract class MType extends MVM.MCodeable {
55  
56      /**
57       * ARRAY TYPE
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      * CLASS TYPE
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      * PRIMITIVE TYPE
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 }