001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.compiler;
012:
013: import java.util.List;
014: import java.util.ArrayList;
015: import java.util.Arrays;
016:
017: /**
018: * Source code based specification for a class to be generated. This
019: * essentially just keeps the interfaces implemented, fields and methods
020: * in separate lists of String's so it is easy for different parts of the
021: * system to add code to the same class.
022: */
023: public class ClassSpec {
024:
025: private String pkg;
026: private String name;
027: private String baseClass;
028: private boolean finalClass;
029: private List imports = new ArrayList();
030: private List interfaces = new ArrayList();
031: private List fields = new ArrayList();
032: private List methods = new ArrayList();
033:
034: public ClassSpec(String pkg, String name, String baseClass) {
035: this .pkg = pkg;
036: this .name = name;
037: this .baseClass = baseClass;
038: finalClass = true;
039: }
040:
041: public String getPkg() {
042: return pkg;
043: }
044:
045: public String getName() {
046: return name;
047: }
048:
049: public String getQName() {
050: return pkg == null ? name : pkg + "." + name;
051: }
052:
053: public String getBaseClass() {
054: return baseClass;
055: }
056:
057: public boolean isFinalClass() {
058: return finalClass;
059: }
060:
061: public void setFinalClass(boolean finalClass) {
062: this .finalClass = finalClass;
063: }
064:
065: public void addImport(String name) {
066: imports.add(name);
067: }
068:
069: public void addInterface(String name) {
070: interfaces.add(name);
071: }
072:
073: public void addField(String name) {
074: fields.add(name);
075: }
076:
077: public void addMethod(String name) {
078: methods.add(name);
079: }
080:
081: public String toString() {
082: return getQName();
083: }
084:
085: /**
086: * Return the complete source code for this class.
087: */
088: public String toSrcCode() {
089: StringBuffer s = new StringBuffer();
090: if (pkg != null) {
091: s.append("package ");
092: s.append(pkg);
093: s.append(";\n\n");
094: }
095: int n = imports.size();
096: if (n > 0) {
097: for (int i = 0; i < n; i++) {
098: s.append("import ");
099: s.append((String) imports.get(i));
100: s.append(";\n");
101: }
102: s.append('\n');
103: }
104: s.append("public ");
105: if (finalClass) {
106: s.append("final ");
107: }
108: s.append("class ");
109: s.append(name);
110: if (baseClass != null) {
111: s.append(" extends ");
112: s.append(baseClass);
113: }
114: n = interfaces.size();
115: if (n > 0) {
116: s.append("\n implements ");
117: s.append((String) interfaces.get(0));
118: for (int i = 1; i < n; i++) {
119: s.append(", ");
120: s.append((String) interfaces.get(i));
121: }
122: }
123: s.append(" {\n\n");
124: n = fields.size();
125: if (n > 0) {
126: for (int i = 0; i < n; i++) {
127: s.append("\t");
128: s.append((String) fields.get(i));
129: s.append(";\n");
130: }
131: s.append('\n');
132: }
133: n = methods.size();
134: if (n > 0) {
135: for (int i = 0; i < n; i++) {
136: s.append((String) methods.get(i));
137: s.append('\n');
138: }
139: s.append('\n');
140: }
141: s.append("}\n");
142: return s.toString();
143: }
144:
145: /**
146: * Add imports for popular things in java.lang and java.math.
147: */
148: public void addImportsForJavaLang() {
149: imports.addAll(Arrays.asList(new String[] {
150: "java.lang.Integer", "java.lang.Byte",
151: "java.lang.Character", "java.lang.Short",
152: "java.lang.Float", "java.lang.Double",
153: "java.lang.Long", "java.lang.Boolean",
154: "java.lang.String", "java.lang.StringBuffer",
155: "java.lang.Object", "java.lang.Comparable",
156: "java.lang.ClassNotFoundException",
157: "java.lang.RuntimeException",
158: "java.lang.NullPointerException", "java.lang.System",
159: "java.lang.Runtime", "java.lang.NumberFormatException",
160: "java.lang.Number", "java.math.BigInteger",
161: "java.math.BigDecimal", }));
162: }
163:
164: }
|