KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > compiler > ClassSpec


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.compiler;
13
14 import java.util.List JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17
18 /**
19  * Source code based specification for a class to be generated. This
20  * essentially just keeps the interfaces implemented, fields and methods
21  * in separate lists of String's so it is easy for different parts of the
22  * system to add code to the same class.
23  */

24 public class ClassSpec {
25
26     private String JavaDoc pkg;
27     private String JavaDoc name;
28     private String JavaDoc baseClass;
29     private boolean finalClass;
30     private List JavaDoc imports = new ArrayList JavaDoc();
31     private List JavaDoc interfaces = new ArrayList JavaDoc();
32     private List JavaDoc fields = new ArrayList JavaDoc();
33     private List JavaDoc methods = new ArrayList JavaDoc();
34
35     public ClassSpec(String JavaDoc pkg, String JavaDoc name, String JavaDoc baseClass) {
36         this.pkg = pkg;
37         this.name = name;
38         this.baseClass = baseClass;
39         finalClass = true;
40     }
41
42     public String JavaDoc getPkg() {
43         return pkg;
44     }
45
46     public String JavaDoc getName() {
47         return name;
48     }
49
50     public String JavaDoc getQName() {
51         return pkg == null ? name : pkg + "." + name;
52     }
53
54     public String JavaDoc getBaseClass() {
55         return baseClass;
56     }
57
58     public boolean isFinalClass() {
59         return finalClass;
60     }
61
62     public void setFinalClass(boolean finalClass) {
63         this.finalClass = finalClass;
64     }
65
66     public void addImport(String JavaDoc name) {
67         imports.add(name);
68     }
69
70     public void addInterface(String JavaDoc name) {
71         interfaces.add(name);
72     }
73
74     public void addField(String JavaDoc name) {
75         fields.add(name);
76     }
77
78     public void addMethod(String JavaDoc name) {
79         methods.add(name);
80     }
81
82     public String JavaDoc toString() {
83         return getQName();
84     }
85
86     /**
87      * Return the complete source code for this class.
88      */

89     public String JavaDoc toSrcCode() {
90         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
91         if (pkg != null) {
92             s.append("package ");
93             s.append(pkg);
94             s.append(";\n\n");
95         }
96         int n = imports.size();
97         if (n > 0) {
98             for (int i = 0; i < n; i++) {
99                 s.append("import ");
100                 s.append((String JavaDoc)imports.get(i));
101                 s.append(";\n");
102             }
103             s.append('\n');
104         }
105         s.append("public ");
106         if (finalClass) {
107             s.append("final ");
108         }
109         s.append("class ");
110         s.append(name);
111         if (baseClass != null) {
112             s.append(" extends ");
113             s.append(baseClass);
114         }
115         n = interfaces.size();
116         if (n > 0) {
117             s.append("\n implements ");
118             s.append((String JavaDoc)interfaces.get(0));
119             for (int i = 1; i < n; i++) {
120                 s.append(", ");
121                 s.append((String JavaDoc)interfaces.get(i));
122             }
123         }
124         s.append(" {\n\n");
125         n = fields.size();
126         if (n > 0) {
127             for (int i = 0; i < n; i++) {
128                 s.append("\t");
129                 s.append((String JavaDoc)fields.get(i));
130                 s.append(";\n");
131             }
132             s.append('\n');
133         }
134         n = methods.size();
135         if (n > 0) {
136             for (int i = 0; i < n; i++) {
137                 s.append((String JavaDoc)methods.get(i));
138                 s.append('\n');
139             }
140             s.append('\n');
141         }
142         s.append("}\n");
143         return s.toString();
144     }
145
146     /**
147      * Add imports for popular things in java.lang and java.math.
148      */

149     public void addImportsForJavaLang() {
150         imports.addAll(Arrays.asList(new String JavaDoc[]{
151             "java.lang.Integer",
152             "java.lang.Byte",
153             "java.lang.Character",
154             "java.lang.Short",
155             "java.lang.Float",
156             "java.lang.Double",
157             "java.lang.Long",
158             "java.lang.Boolean",
159             "java.lang.String",
160             "java.lang.StringBuffer",
161             "java.lang.Object",
162             "java.lang.Comparable",
163             "java.lang.ClassNotFoundException",
164             "java.lang.RuntimeException",
165             "java.lang.NullPointerException",
166             "java.lang.System",
167             "java.lang.Runtime",
168             "java.lang.NumberFormatException",
169             "java.lang.Number",
170             "java.math.BigInteger",
171             "java.math.BigDecimal",
172             }));
173     }
174
175 }
176
177
Popular Tags