1 29 30 package com.caucho.quercus.program; 31 32 import com.caucho.quercus.Quercus; 33 import com.caucho.quercus.env.Env; 34 import com.caucho.quercus.env.JavaMethod; 35 import com.caucho.quercus.env.NullValue; 36 import com.caucho.quercus.env.ObjectExtValue; 37 import com.caucho.quercus.env.QuercusClass; 38 import com.caucho.quercus.env.Value; 39 import com.caucho.quercus.expr.Expr; 40 import com.caucho.quercus.expr.LiteralExpr; 41 import com.caucho.quercus.function.Marshal; 42 import com.caucho.quercus.function.MarshalFactory; 43 import com.caucho.quercus.module.ModuleContext; 44 import com.caucho.util.L10N; 45 46 import java.lang.reflect.Field ; 47 import java.lang.reflect.Method ; 48 import java.lang.reflect.Modifier ; 49 import java.util.HashMap ; 50 import java.util.Map ; 51 import java.util.logging.Level ; 52 import java.util.logging.Logger ; 53 54 57 public class JavaImplClassDef extends ClassDef { 58 private final static Logger log 59 = Logger.getLogger(JavaImplClassDef.class.getName()); 60 private final static L10N L = new L10N(JavaImplClassDef.class); 61 62 private final ModuleContext _moduleContext; 63 64 private final String _name; 65 private final Class _type; 66 67 private final HashMap <String , Expr> _constMap 68 = new HashMap <String , Expr>(); 69 70 private final HashMap <String , JavaMethod> _functionMap 71 = new HashMap <String , JavaMethod>(); 72 73 private JavaMethod _cons; 74 75 public JavaImplClassDef(ModuleContext moduleContext, 76 String name, 77 Class type) 78 { 79 super(name, null, new String [0]); 80 81 _moduleContext = moduleContext; 82 83 _name = name; 84 85 _type = type; 86 } 87 88 91 public String getName() 92 { 93 return _name; 94 } 95 96 public Class getType() 97 { 98 return _type; 99 } 100 101 104 public Value newInstance(Env env, QuercusClass qClass) 105 { 106 return new ObjectExtValue(qClass); 107 } 108 109 112 public Value callMethod(Env env, Value obj, String name, Expr []args) 113 { 114 JavaMethod method = _functionMap.get(name); 115 116 if (method == null) { 117 env.warning(env.getLocation().getMessagePrefix() + L.l("{0}::{1} is an unknown method.", 118 _name, name)); 119 120 return NullValue.NULL; 121 } 122 123 return method.call(env, obj, args); 124 } 125 126 129 public Value callMethod(Env env, Value obj, String name, Value []args) 130 { 131 return getMethod(env, name).call(env, obj, args); 132 } 133 134 137 public Value callMethod(Env env, Object obj, String name) 138 { 139 return getMethod(env, name).call(env, obj); 140 } 141 142 145 public Value callMethod(Env env, Object obj, String name, Value a1) 146 { 147 return getMethod(env, name).call(env, obj, a1); 148 } 149 150 153 public Value callMethod(Env env, Object obj, String name, 154 Value a1, Value a2) 155 { 156 return getMethod(env, name).call(env, obj, a1, a2); 157 } 158 159 162 public Value callMethod(Env env, Object obj, String name, 163 Value a1, Value a2, Value a3) 164 { 165 return getMethod(env, name).call(env, obj, a1, a2, a3); 166 } 167 168 171 public Value callMethod(Env env, Object obj, String name, 172 Value a1, Value a2, Value a3, Value a4) 173 { 174 return getMethod(env, name).call(env, obj, a1, a2, a3, a4); 175 } 176 177 180 public Value callMethod(Env env, Object obj, String name, 181 Value a1, Value a2, Value a3, Value a4, Value a5) 182 { 183 return getMethod(env, name).call(env, obj, a1, a2, a3, a4, a5); 184 } 185 186 private JavaMethod getMethod(Env env, String name) 187 { 188 JavaMethod method = _functionMap.get(name); 189 190 if (method == null) { 191 env.error("'" + name + "' is an unknown method."); 192 } 193 194 return method; 195 } 196 197 200 public void initClass(QuercusClass cl) 201 { 202 if (_cons != null) 203 cl.setConstructor(_cons); 204 205 for (Map.Entry <String ,JavaMethod> entry : _functionMap.entrySet()) { 206 cl.addMethod(entry.getKey(), entry.getValue()); 207 } 208 209 for (Map.Entry <String ,Expr> entry : _constMap.entrySet()) { 210 cl.addConstant(entry.getKey(), entry.getValue()); 211 } 212 } 213 214 217 public void initInstance(Env env, Value value) 218 { 219 } 220 221 224 @Override  225 public Value callNew(Env env, Expr []args) 226 { 227 return null; 228 } 229 230 233 @Override  234 public Value callNew(Env env, Value []args) 235 { 236 return null; 237 } 238 239 242 public AbstractFunction findConstructor() 243 { 244 return null; 245 } 246 247 250 public void introspect(ModuleContext moduleContext) 251 { 252 introspectConstants(_type); 253 introspectMethods(moduleContext, _type); 254 introspectFields(moduleContext, _type); 255 } 256 257 private Method getConsMethod(Class type) 258 { 259 Method []methods = type.getMethods(); 260 261 for (int i = 0; i < methods.length; i++) { 262 Method method = methods[i]; 263 264 if (! method.getName().equals("__construct")) 265 continue; 266 if (! Modifier.isStatic(method.getModifiers())) 267 continue; 268 if (! Modifier.isPublic(method.getModifiers())) 269 continue; 270 271 return method; 272 } 273 274 return null; 275 } 276 277 280 private void introspectFields(ModuleContext moduleContext, Class type) 281 { 282 MarshalFactory marshalFactory = moduleContext.getMarshalFactory(); 283 284 Field [] fields = type.getFields(); 286 287 for (Field field : fields) { 288 if (Modifier.isStatic(field.getModifiers())) 289 continue; 290 291 Marshal marshal = marshalFactory.create(field.getType(), false); 292 293 } 295 296 297 } 299 300 303 private void introspectConstants(Class type) 304 { 305 if (type == null || type.equals(Object .class)) 306 return; 307 308 if (! Modifier.isPublic(type.getModifiers())) 309 return; 310 311 Class []ifcs = type.getInterfaces(); 312 313 for (Class ifc : ifcs) { 314 introspectConstants(ifc); 315 } 316 317 Field []fields = type.getDeclaredFields(); 318 319 for (Field field : fields) { 320 if (_constMap.get(field.getName()) != null) 321 continue; 322 else if (! Modifier.isPublic(field.getModifiers())) 323 continue; 324 else if (! Modifier.isStatic(field.getModifiers())) 325 continue; 326 else if (! Modifier.isFinal(field.getModifiers())) 327 continue; 328 329 try { 330 Value value = Quercus.objectToValue(field.get(null)); 331 332 if (value != null) 333 _constMap.put(field.getName().intern(), new LiteralExpr(value)); 334 } catch (Throwable e) { 335 log.log(Level.FINER, e.toString(), e); 336 } 337 } 338 339 introspectConstants(type.getSuperclass()); 340 } 341 342 345 private void introspectMethods(ModuleContext moduleContext, Class type) 346 { 347 if (type == null || type.equals(Object .class)) 348 return; 349 350 Class []ifcs = type.getInterfaces(); 351 352 for (Class ifc : ifcs) { 353 introspectMethods(moduleContext, ifc); 354 } 355 356 Method []methods = type.getDeclaredMethods(); 357 358 for (Method method : methods) { 359 if (_functionMap.get(method.getName()) != null) 360 continue; 361 else if (! Modifier.isPublic(method.getModifiers())) 362 continue; 363 else if (! Modifier.isStatic(method.getModifiers())) 364 continue; 365 366 JavaMethod javaMethod = new JavaMethod(moduleContext, method); 367 368 if (method.getName().equals("__construct")) 369 _cons = javaMethod; 370 371 _functionMap.put(method.getName(), javaMethod); 372 } 373 374 introspectMethods(moduleContext, type.getSuperclass()); 375 } 376 377 private class FieldMarshalPair { 378 public final Field _field; 379 public final Marshal _marshal; 380 381 public FieldMarshalPair(Field field, 382 Marshal marshal) 383 { 384 _field = field; 385 _marshal = marshal; 386 } 387 } 388 } 389 390 | Popular Tags |