javaclassdef.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,630 行 · 第 1/3 页
JAVA
1,630 行
AbstractJavaMethod method = _functionMap.get(hash, name, nameLen); if (method == null) { env.warning(L.l("{0}::{1} is an unknown method.", _name, toMethod(name, nameLen))); return NullValue.NULL; } return method.callMethod(env, qThis, args); } /** * Eval a method */ public Value callMethod(Env env, Value qThis, int hash, char []name, int nameLen, Value []args) { AbstractJavaMethod method = _functionMap.get(hash, name, nameLen); if (method != null) return method.callMethod(env, qThis, args); else if (__call != null) { Value []extArgs = new Value[args.length + 1]; extArgs[0] = env.createString(name, nameLen); System.arraycopy(args, 0, extArgs, 1, args.length); return __call.callMethod(env, qThis, extArgs); } else { env.error(L.l("'{0}::{1}' is an unknown method", _name, toMethod(name, nameLen))); return NullValue.NULL; } } /** * Eval a method */ public Value callMethod(Env env, Value qThis, int hash, char []name, int nameLen) { AbstractJavaMethod method = _functionMap.get(hash, name, nameLen); if (method != null) return method.callMethod(env, qThis); else if (__call != null) return __call.callMethod(env, qThis, env.createString(name, nameLen)); else { env.error(L.l("'{0}::{1}()' is an unknown method", _name, toMethod(name, nameLen))); return NullValue.NULL; } } /** * Eval a method */ public Value callMethod(Env env, Value qThis, int hash, char []name, int nameLen, Value a1) { AbstractJavaMethod method = _functionMap.get(hash, name, nameLen); if (method != null) return method.callMethod(env, qThis, a1); else if (__call != null) return __call.callMethod(env, qThis, env.createString(name, nameLen), a1); else { env.error(L.l("'{0}::{1}(a1)' is an unknown method", _name, toMethod(name, nameLen))); return NullValue.NULL; } } /** * Eval a method */ public Value callMethod(Env env, Value qThis, int hash, char []name, int nameLen, Value a1, Value a2) { AbstractJavaMethod method = _functionMap.get(hash, name, nameLen); if (method != null) return method.callMethod(env, qThis, a1, a2); else if (__call != null) return __call.callMethod(env, qThis, env.createString(name, nameLen), a1, a2); else { env.error(L.l("'{0}::{1}(a1,a2)' is an unknown method", _name, toMethod(name, nameLen))); return NullValue.NULL; } } /** * Eval a method */ public Value callMethod(Env env, Value qThis, int hash, char []name, int nameLen, Value a1, Value a2, Value a3) { AbstractJavaMethod method = _functionMap.get(hash, name, nameLen); if (method != null) return method.callMethod(env, qThis, a1, a2, a3); else if (__call != null) return __call.callMethod(env, qThis, env.createString(name, nameLen), a1, a2, a3); else { env.error(L.l("'{0}::{1}(a1,a2,a3)' is an unknown method", _name, toMethod(name, nameLen))); return NullValue.NULL; } } /** * Eval a method */ public Value callMethod(Env env, Value qThis, int hash, char []name, int nameLen, Value a1, Value a2, Value a3, Value a4) { AbstractJavaMethod method = _functionMap.get(hash, name, nameLen); if (method != null) return method.callMethod(env, qThis, a1, a2, a3, a4); else if (__call != null) return __call.callMethod(env, qThis, env.createString(name, nameLen), a1, a2, a3, a4); else { env.error(L.l("'{0}::{1}(a1,a2,a3,a4)' is an unknown method", _name, toMethod(name, nameLen))); return NullValue.NULL; } } /** * Eval a method */ public Value callMethod(Env env, Value qThis, int hash, char []name, int nameLen, Value a1, Value a2, Value a3, Value a4, Value a5) { AbstractJavaMethod method = _functionMap.get(hash, name, nameLen); if (method != null) return method.callMethod(env, qThis, a1, a2, a3, a4, a5); else if (__call != null) return __call.callMethod(env, qThis, new Value[] { env.createString(name, nameLen), a1, a2, a3, a4, a5 }); else { env.error(L.l("'{0}::{1}(a1,a2,a3,a4,a5)' is an unknown method", _name, toMethod(name, nameLen))); return NullValue.NULL; } } private String toMethod(char []name, int nameLen) { return new String(name, 0, nameLen); } public Set<? extends Map.Entry<Value,Value>> entrySet(Object obj) { try { if (_entrySet == null) { return null; } return (Set) _entrySet.invoke(obj); } catch (Exception e) { throw new QuercusException(e); } } /** * Initialize the quercus class. */ @Override public void initClass(QuercusClass cl) { init(); if (_cons != null) { cl.setConstructor(_cons); cl.addMethod("__construct", _cons); } for (AbstractJavaMethod value : _functionMap.values()) { cl.addMethod(value.getName(), value); } if (__fieldGet != null) cl.setFieldGet(__fieldGet); if (__fieldSet != null) cl.setFieldSet(__fieldSet); if (__call != null) cl.setCall(__call); if (__toString != null) { cl.addMethod("__toString", __toString); } if (_arrayDelegate != null) cl.setArrayDelegate(_arrayDelegate); else if (_funArrayDelegate != null) cl.setArrayDelegate(_funArrayDelegate); if (_traversableDelegate != null) cl.setTraversableDelegate(_traversableDelegate); else if (cl.getTraversableDelegate() == null && _iteratorMethod != null) { // adds support for Java classes implementing iterator() // php/ cl.setTraversableDelegate(new JavaTraversableDelegate(_iteratorMethod)); } if (_countDelegate != null) cl.setCountDelegate(_countDelegate); for (Map.Entry<String,Value> entry : _constMap.entrySet()) { cl.addConstant(entry.getKey(), new LiteralExpr(entry.getValue())); } } /** * Finds the matching constant */ public Value findConstant(Env env, String name) { return _constMap.get(name); } /** * Creates a new instance. */ public void initInstance(Env env, Value value) { } /** * Returns the constructor */ @Override public AbstractFunction findConstructor() { return null; } @Override public final void init() { if (_isInit) return; synchronized (this) { if (_isInit) return; super.init(); try { initInterfaceList(_type); introspect(); } finally { _isInit = true; } } } private void initInterfaceList(Class type) { Class[] ifaces = type.getInterfaces(); if (ifaces == null) return; for (Class iface : ifaces) { JavaClassDef javaClassDef = _moduleContext.getJavaClassDefinition(iface); if (javaClassDef != null) addInterface(javaClassDef.getName()); // recurse for parent interfaces initInterfaceList(iface); } } /** * Introspects the Java class. */ private void introspect() { introspectConstants(_type); introspectMethods(_moduleContext, _type); introspectFields(_moduleContext, _type); _marshal = new JavaMarshal(this, false); Method consMethod = getConsMethod(_type); if (consMethod != null) _cons = new JavaMethod(_moduleContext, consMethod); else { Constructor []cons = _type.getConstructors(); if (cons.length > 0) { int i; for (i = 0; i < cons.length; i++) { if (cons[i].isAnnotationPresent(Construct.class)) break; } if (i < cons.length) { _cons = new JavaConstructor(_moduleContext, cons[i]); } else { _cons = new JavaConstructor(_moduleContext, cons[0]); for (i = 1; i < cons.length; i++) { _cons = _cons.overload(new JavaConstructor(_moduleContext, cons[i])); } } } else _cons = null; } introspectAnnotations(_type); try { Method method = _type.getMethod("iterator", new Class[0]); if (method != null && Iterator.class.isAssignableFrom(method.getReturnType())) _iteratorMethod = method; } catch (Exception e) { } } private void introspectAnnotations(Class type) { try { if (type == null || type == Object.class) return; // interfaces for (Class<?> iface : type.getInterfaces()) introspectAnnotations(iface); // super-class introspectAnnotations(type.getSuperclass()); // this for (Annotation annotation : type.getAnnotations()) { if (annotation.annotationType() == Delegates.class) { Class[] delegateClasses = ((Delegates) annotation).value(); for (Class cl : delegateClasses) { boolean isDelegate = addDelegate(cl); if (! isDelegate) throw new IllegalArgumentException(L.l("unknown @Delegate class '{0}'", cl)); } } else if (annotation.annotationType() == ResourceType.class) { _resourceType = ((ResourceType) annotation).value(); } } } catch (RuntimeException e) { throw e; } catch (InstantiationException e) { throw new QuercusModuleException(e.getCause()); } catch (Exception e) { throw new QuercusModuleException(e); } } private boolean addDelegate(Class cl) throws InstantiationException, IllegalAccessException { boolean isDelegate = false; if (TraversableDelegate.class.isAssignableFrom(cl)) { _traversableDelegate = (TraversableDelegate) cl.newInstance(); isDelegate = true; } if (ArrayDelegate.class.isAssignableFrom(cl)) { _arrayDelegate = (ArrayDelegate) cl.newInstance(); isDelegate = true; } if (CountDelegate.class.isAssignableFrom(cl)) { _countDelegate = (CountDelegate) cl.newInstance(); isDelegate = true; } return isDelegate; } private <T> boolean addDelegate(Class<T> cl, ArrayList<T> delegates, Class<? extends Object> delegateClass) { if (!cl.isAssignableFrom(delegateClass)) return false; for (T delegate : delegates) { if (delegate.getClass() == delegateClass) { return true; } } try { delegates.add((T) delegateClass.newInstance()); } catch (InstantiationException e) { throw new QuercusModuleException(e); } catch (IllegalAccessException e) { throw new QuercusModuleException(e); } return true; } private Method getConsMethod(Class type) { Method []methods = type.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (! method.getName().equals("__construct")) continue; if (! Modifier.isStatic(method.getModifiers())) continue; if (! Modifier.isPublic(method.getModifiers())) continue; return method; } return null; } protected void setCons(Method method) { _cons = new JavaMethod(_moduleContext, method); } /** * Introspects the Java class. */ private void introspectFields(ModuleContext moduleContext, Class type) { if (type == null || type.equals(Object.class)) return; if (! Modifier.isPublic(type.getModifiers())) return; // Introspect getXXX and setXXX // also register whether __get, __getField, __set, __setField exists Method[] methods = type.getMethods(); for (Method method : methods) { if (Modifier.isStatic(method.getModifiers())) continue; String methodName = method.getName(); int length = methodName.length(); if (length > 3) { if (methodName.startsWith("get")) { StringValue quercusName = javaToQuercusConvert(methodName.substring(3, length)); AbstractJavaMethod existingGetter = _getMap.get(quercusName); AbstractJavaMethod newGetter = new JavaMethod(moduleContext, method); if (existingGetter != null) { newGetter = existingGetter.overload(newGetter); } _getMap.put(quercusName, newGetter); } else if (methodName.startsWith("is")) { StringValue quercusName = javaToQuercusConvert(methodName.substring(2, length)); AbstractJavaMethod existingGetter = _getMap.get(quercusName); AbstractJavaMethod newGetter = new JavaMethod(moduleContext, method); if (existingGetter != null) { newGetter = existingGetter.overload(newGetter); } _getMap.put(quercusName, newGetter); } else if (methodName.startsWith("set")) { StringValue quercusName = javaToQuercusConvert(methodName.substring(3, length)); AbstractJavaMethod existingSetter = _setMap.get(quercusName); AbstractJavaMethod newSetter = new JavaMethod(moduleContext, method); if (existingSetter != null) newSetter = existingSetter.overload(newSetter); _setMap.put(quercusName, newSetter); } else if ("__get".equals(methodName)) { if (_funArrayDelegate == null) _funArrayDelegate = new FunctionArrayDelegate(); _funArrayDelegate.setArrayGet(new JavaMethod(moduleContext, method)); } else if ("__set".equals(methodName)) { if (_funArrayDelegate == null) _funArrayDelegate = new FunctionArrayDelegate(); _funArrayDelegate.setArrayPut(new JavaMethod(moduleContext, method)); } else if ("__getField".equals(methodName)) { __fieldGet = new JavaMethod(moduleContext, method); } else if ("__setField".equals(methodName)) { __fieldSet = new JavaMethod(moduleContext, method); } else if ("__fieldGet".equals(methodName)) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?