1 29 30 package com.caucho.java.gen; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.bytecode.JMethod; 34 import com.caucho.java.JavaWriter; 35 import com.caucho.util.L10N; 36 37 import java.io.IOException ; 38 39 42 public class BaseMethod extends ClassComponent { 43 private static final L10N L = new L10N(BaseMethod.class); 44 45 private JMethod _method; 46 private String _methodName; 47 48 private boolean _isStatic; 49 private String _visibility = "public"; 50 51 private JClass _returnType; 52 private JClass []_parameterTypes; 53 54 private JClass []_exceptionTypes; 55 56 private CallChain _call; 57 58 61 public BaseMethod(String methodName, CallChain call) 62 { 63 _methodName = methodName; 64 65 setCall(call); 66 } 67 68 71 public BaseMethod(JMethod method, CallChain call) 72 { 73 _method = method; 74 _exceptionTypes = method.getExceptionTypes(); 75 76 setCall(call); 77 } 78 79 82 public BaseMethod(JMethod method) 83 { 84 _method = method; 85 _exceptionTypes = method.getExceptionTypes(); 86 } 87 88 91 public BaseMethod(JMethod apiMethod, JMethod implMethod) 92 { 93 this(apiMethod, new MethodCallChain(implMethod)); 94 } 95 96 99 public CallChain getCall() 100 { 101 return _call; 102 } 103 104 107 public void setCall(CallChain call) 108 { 109 if (call == null) 110 throw new NullPointerException (); 111 112 _call = call; 113 } 114 115 118 public JMethod getMethod() 119 { 120 return _method; 121 } 122 123 126 public String getMethodName() 127 { 128 if (_methodName != null) 129 return _methodName; 130 else 131 return _method.getName(); 132 } 133 134 137 public JClass []getParameterTypes() 138 { 139 if (_parameterTypes != null) 140 return _parameterTypes; 141 else if (_method != null) 142 return _method.getParameterTypes(); 143 else 144 return _call.getParameterTypes(); 145 } 146 147 150 public JClass getReturnType() 151 { 152 if (_method != null) 153 return _method.getReturnType(); 154 else 155 return _call.getReturnType(); 156 } 157 158 161 public JClass []getExceptionTypes() 162 { 163 return _exceptionTypes; 164 } 165 166 171 public void generate(JavaWriter out) 172 throws IOException 173 { 174 String []args = generateMethodHeader(out); 175 176 JClass []exceptionTypes = getExceptionTypes(); 177 if (exceptionTypes != null && exceptionTypes.length > 0) { 178 out.print(" throws "); 179 180 for (int i = 0; i < exceptionTypes.length; i++) { 181 if (i != 0) 182 out.print(", "); 183 184 out.print(exceptionTypes[i].getPrintName()); 185 } 186 out.println(); 187 } 188 189 out.println("{"); 191 out.pushDepth(); 192 193 generateCall(out, args); 194 195 out.popDepth(); 196 out.println("}"); 197 } 198 199 206 public String []generateMethodHeader(JavaWriter out) 207 throws IOException 208 { 209 if (_visibility != null && ! _visibility.equals("")) 210 out.print(_visibility + " "); 211 212 if (_isStatic) 213 out.print("static "); 214 215 out.print(getReturnType().getPrintName()); 216 out.print(" " + getMethodName() + "("); 217 218 JClass []parameterTypes = getParameterTypes(); 219 String []args = new String [parameterTypes.length]; 220 221 for (int i = 0; i < args.length; i++) { 222 if (i != 0) 223 out.print(", "); 224 225 out.print(parameterTypes[i].getPrintName()); 226 227 args[i] = "a" + i; 228 out.print(" " + args[i]); 229 230 } 231 out.println(")"); 232 233 return args; 234 } 235 236 242 protected void generateCall(JavaWriter out, String []args) 243 throws IOException 244 { 245 _call.generateCall(out, null, null, args); 246 } 247 } 248 | Popular Tags |