1 23 24 package org.enhydra.xml.xmlc.codegen; 25 26 import org.enhydra.xml.xmlc.XMLCError; 27 28 32 public final class JavaField extends JavaVariable { 33 36 private JavaCode fInitializerCode; 37 38 41 private String fInitializer; 42 43 46 private static int adjustModifiers(int modifiers) { 47 if ((modifiers & (JavaModifiers.PROTECTED|JavaModifiers.PRIVATE)) != 0) { 49 modifiers |= JavaModifiers.OMIT_INTERFACE; 50 } 51 if ((modifiers & JavaModifiers.STATIC) == 0) { 53 modifiers |= JavaModifiers.OMIT_INTERFACE; 54 } 55 if ((modifiers & JavaModifiers.OMIT_INTERFACE) == 0) { 57 modifiers |= JavaModifiers.OMIT_IMPLEMENTATION; 58 } 59 return modifiers; 60 } 61 62 65 private void buildInitializer(Object initializer) { 66 if (initializer == null) { 67 } else if (initializer instanceof String ) { 69 fInitializer = (String )initializer; 70 } else if (initializer instanceof JavaCode) { 71 fInitializerCode = (JavaCode)initializer; 72 } else { 73 throw new XMLCError("Invalid type for field initializer: " + initializer.getClass()); 74 } 75 } 76 77 89 public JavaField(String name, 90 String type, 91 int modifiers, 92 String [] doc, 93 Object initializer) { 94 super(name, type, adjustModifiers(modifiers), doc); 95 buildInitializer(initializer); 96 } 97 98 109 public JavaField(String name, 110 String type, 111 int modifiers, 112 String doc, 113 Object initializer) { 114 super(name, type, adjustModifiers(modifiers), doc); 115 buildInitializer(initializer); 116 } 117 120 public void print(IndentWriter out) { 121 String [] doc = getDoc(); 122 if (doc != null) { 123 out.println("/**"); 124 for (int idx = 0; idx < doc.length; idx++) { 125 out.print(" * "); 126 out.println(doc[idx]); 127 } 128 out.println(" */"); 129 } 130 printDefinition(out); 131 if (fInitializer != null) { 132 out.print(" = "); 134 out.print(fInitializer); 135 out.println(';'); 136 } else if (fInitializerCode != null) { 137 out.println(" = {"); 139 out.enter(); 140 fInitializerCode.print(out); 141 out.leave(); 142 out.println("};"); 143 } else { 144 out.println(";"); 145 } 146 out.println(); 147 } 148 } 149 | Popular Tags |