1 2 12 package com.versant.core.compiler; 13 14 import java.util.List ; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 18 24 public class ClassSpec { 25 26 private String pkg; 27 private String name; 28 private String baseClass; 29 private boolean finalClass; 30 private List imports = new ArrayList (); 31 private List interfaces = new ArrayList (); 32 private List fields = new ArrayList (); 33 private List methods = new ArrayList (); 34 35 public ClassSpec(String pkg, String name, String baseClass) { 36 this.pkg = pkg; 37 this.name = name; 38 this.baseClass = baseClass; 39 finalClass = true; 40 } 41 42 public String getPkg() { 43 return pkg; 44 } 45 46 public String getName() { 47 return name; 48 } 49 50 public String getQName() { 51 return pkg == null ? name : pkg + "." + name; 52 } 53 54 public String 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 name) { 67 imports.add(name); 68 } 69 70 public void addInterface(String name) { 71 interfaces.add(name); 72 } 73 74 public void addField(String name) { 75 fields.add(name); 76 } 77 78 public void addMethod(String name) { 79 methods.add(name); 80 } 81 82 public String toString() { 83 return getQName(); 84 } 85 86 89 public String toSrcCode() { 90 StringBuffer s = new StringBuffer (); 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 )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 )interfaces.get(0)); 119 for (int i = 1; i < n; i++) { 120 s.append(", "); 121 s.append((String )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 )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 )methods.get(i)); 138 s.append('\n'); 139 } 140 s.append('\n'); 141 } 142 s.append("}\n"); 143 return s.toString(); 144 } 145 146 149 public void addImportsForJavaLang() { 150 imports.addAll(Arrays.asList(new String []{ 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 |