1 23 24 package org.enhydra.xml.xmlc.codegen; 25 26 27 29 32 public final class JavaMethod { 33 34 private String fName; 35 36 37 private String fReturnType; 38 39 40 protected int fModifiers; 41 42 43 private JavaParameter[] fParameters; 44 45 46 private String [] fDoc; 47 48 49 private JavaCode fCode = new JavaCode(); 50 51 54 private static int adjustModifiers(String returnType, 55 int modifiers) { 56 if ((modifiers & (JavaModifiers.PROTECTED|JavaModifiers.PRIVATE)) != 0) { 58 modifiers |= JavaModifiers.OMIT_INTERFACE; 59 } 60 if ((modifiers & JavaModifiers.STATIC) != 0) { 62 modifiers |= JavaModifiers.OMIT_INTERFACE; 63 } 64 if (returnType == null) { 66 modifiers |= JavaModifiers.OMIT_INTERFACE; 67 } 68 return modifiers; 69 } 70 71 85 public JavaMethod(String name, 87 String returnType, 88 int modifiers, 89 JavaParameter[] parameters, 90 String [] doc) { 91 fName = name; 92 fReturnType = returnType; 93 fModifiers = adjustModifiers(returnType, modifiers); 94 if (parameters != null) { 95 fParameters = (JavaParameter[])parameters.clone(); 96 } 97 fDoc = (String [])doc.clone(); 98 } 99 100 103 public JavaCode getCode() { 104 return fCode; 105 } 106 107 110 public String getName() { 111 return fName; 112 } 113 114 117 public String getReturnType() { 118 return fReturnType; 119 } 120 121 124 public int getModifiers() { 125 return fModifiers; 126 } 127 128 136 public void printMethodSignature(IndentWriter out, 137 boolean beginBody) { 138 int numParams = (fParameters == null) ? 0 : fParameters.length; 139 140 StringBuffer start = new StringBuffer (JavaModifiers.toDecl(fModifiers)); 142 if (fReturnType != null) { 143 start.append(fReturnType); 144 start.append(' '); 145 } 146 start.append(fName); 147 start.append('('); 148 out.print(start); 149 150 String indent = (numParams == 0) ? "" : JavaLang.makeIndent(start.length()); 152 153 for (int idx = 0; idx < numParams; idx++) { 155 if (idx > 0) { 156 out.print(indent); 157 } 158 fParameters[idx].printDefinition(out); 159 if (idx < (numParams - 1)) { 160 out.println(','); 161 } 162 } 163 if (beginBody) { 164 out.println(") {"); 165 } else { 166 out.println(");"); 167 } 168 } 169 170 173 public void print(IndentWriter out, 174 boolean printBody) { 175 if (fDoc != null) { 176 out.println("/**"); 177 for (int idx = 0; idx < fDoc.length; idx++) { 178 out.print(" * "); 179 out.println(fDoc[idx]); 180 } 181 out.println(" */"); 182 } 183 if (fName != null) { 184 printMethodSignature(out, printBody); 185 } else { 186 out.println("static {"); 187 } 188 if (printBody) { 189 out.enter(); 190 fCode.print(out); 191 out.leave(); 192 out.println("}"); 193 } 194 out.println(); 195 } 196 } 197 | Popular Tags |