1 33 34 package net.percederberg.grammatica.code.java; 35 36 import java.io.PrintWriter ; 37 38 import net.percederberg.grammatica.code.CodeElement; 39 import net.percederberg.grammatica.code.CodeElementContainer; 40 import net.percederberg.grammatica.code.CodeStyle; 41 42 49 abstract class JavaType extends CodeElementContainer { 50 51 54 private int modifiers; 55 56 59 private String name; 60 61 64 private String extendType; 65 66 69 private String [] implementTypes = null; 70 71 74 private JavaComment comment = null; 75 76 84 protected JavaType(int modifiers, 85 String name, 86 String extendsType, 87 String implementType) { 88 89 this.modifiers = modifiers; 90 this.name = name; 91 this.extendType = extendsType; 92 if (implementType == null || implementType.equals("")) { 93 this.implementTypes = new String [0]; 94 } else { 95 this.implementTypes = new String [1]; 96 this.implementTypes[0] = implementType; 97 } 98 } 99 100 108 protected JavaType(int modifiers, 109 String name, 110 String extendsType, 111 String [] implementTypes) { 112 113 this.modifiers = modifiers; 114 this.name = name; 115 this.extendType = extendsType; 116 this.implementTypes = implementTypes; 117 } 118 119 124 public String toString() { 125 return name; 126 } 127 128 133 public void addComment(JavaComment comment) { 134 this.comment = comment; 135 } 136 137 145 protected void print(PrintWriter out, 146 CodeStyle style, 147 int indent, 148 String type) { 149 150 String indentStr = style.getIndent(indent); 151 String codeIndentStr = style.getIndent(indent + 1); 152 StringBuffer buf = new StringBuffer (); 153 String str; 154 155 if (this.comment != null) { 157 this.comment.print(out, style, indent); 158 } 159 160 buf.append(indentStr); 162 buf.append(JavaModifier.createModifierDecl(modifiers)); 163 buf.append(type); 164 buf.append(" "); 165 buf.append(name); 166 if (extendType != null && !extendType.equals("")) { 167 buf.append(" extends "); 168 buf.append(extendType); 169 } 170 str = createImplDecl(); 171 if (str.length() > 0) { 172 if (buf.length() + str.length() > style.getMargin()) { 173 buf.append("\n"); 174 buf.append(codeIndentStr); 175 } else { 176 buf.append(" "); 177 } 178 buf.append(str); 179 } 180 buf.append(" {"); 181 out.println(buf.toString()); 182 out.println(); 183 184 printContents(out, style, indent + 1); 186 187 out.println(indentStr + "}"); 189 } 190 191 199 protected void printSeparator(PrintWriter out, 200 CodeStyle style, 201 CodeElement prev, 202 CodeElement next) { 203 204 if (prev == null || next == null) { 205 } else { 207 out.println(); 208 } 209 } 210 211 217 private String createImplDecl() { 218 StringBuffer res = new StringBuffer ("implements "); 219 220 if (implementTypes == null || implementTypes.length <= 0) { 221 return ""; 222 } 223 for (int i = 0; i < implementTypes.length; i++) { 224 res.append(implementTypes[i]); 225 if (i + 1 < implementTypes.length) { 226 res.append(", "); 227 } 228 } 229 230 return res.toString(); 231 } 232 } 233 | Popular Tags |