javaclassdef.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,630 行 · 第 1/3 页

JAVA
1,630
字号
          __fieldGet = new JavaMethod(moduleContext, method);        } else if ("__fieldSet".equals(methodName)) {          __fieldSet = new JavaMethod(moduleContext, method);        }      }    }    // Introspect public non-static fields    Field[] fields = type.getFields();    for (Field field : fields) {      if (Modifier.isStatic(field.getModifiers()))        continue;      MarshalFactory factory = moduleContext.getMarshalFactory();      Marshal marshal = factory.create(field.getType(), false);            _fieldMap.put(new StringBuilderValue(field.getName()),		    new FieldMarshalPair(field, marshal));    }   // introspectFields(quercus, type.getSuperclass());  }  /**   * helper for introspectFields   *   * @param s (eg: Foo, URL)   * @return (foo, URL)   */  private StringValue javaToQuercusConvert(String s)  {    if (s.length() == 1) {      return new StringBuilderValue(new char[] {Character.toLowerCase(s.charAt(0))});    }    if (Character.isUpperCase(s.charAt(1)))      return new StringBuilderValue(s);    else {      StringBuilderValue sb = new StringBuilderValue();      sb.append(Character.toLowerCase(s.charAt(0)));      int length = s.length();      for (int i = 1; i < length; i++) {        sb.append(s.charAt(i));      }      return sb;    }  }  /**   * Introspects the Java class.   */  private void introspectConstants(Class type)  {    if (type == null || type.equals(Object.class))      return;    if (! Modifier.isPublic(type.getModifiers()))      return;    Class []ifcs = type.getInterfaces();    for (Class ifc : ifcs) {      introspectConstants(ifc);    }    Field []fields = type.getDeclaredFields();    for (Field field : fields) {      if (_constMap.get(field.getName()) != null)        continue;      else if (! Modifier.isPublic(field.getModifiers()))        continue;      else if (! Modifier.isStatic(field.getModifiers()))        continue;      else if (! Modifier.isFinal(field.getModifiers()))        continue;      try {        Value value = Quercus.objectToValue(field.get(null));        if (value != null)          _constMap.put(field.getName().intern(), value);      } catch (Throwable e) {        log.log(Level.FINER, e.toString(), e);      }    }    introspectConstants(type.getSuperclass());  }  /**   * Introspects the Java class.   */  private void introspectMethods(ModuleContext moduleContext, Class type)  {    if (type == null || type.equals(Object.class))      return;    Method []methods = type.getMethods();    for (Method method : methods) {      if (! Modifier.isPublic(method.getModifiers()))        continue;      if (method.getDeclaringClass() == Object.class)        continue;      if ("printRImpl".equals(method.getName())) {        _printRImpl = method;      } else if ("varDumpImpl".equals(method.getName())) {        _varDumpImpl = method;      } else if (method.isAnnotationPresent(EntrySet.class)) {        _entrySet = method;      } else if ("__call".equals(method.getName())) {        __call = new JavaMethod(moduleContext, method);      } else if ("__toString".equals(method.getName())) {        __toString = new JavaMethod(moduleContext, method);      } else {        if (method.getName().startsWith("quercus_"))          throw new UnsupportedOperationException(L.l("{0}: use @Name instead", method.getName()));        JavaMethod newFun = new JavaMethod(moduleContext, method);        AbstractJavaMethod fun = _functionMap.get(newFun.getName());        if (fun != null)          fun = fun.overload(newFun);        else          fun = newFun;        _functionMap.put(fun.getName(), fun);      }    }    introspectMethods(moduleContext, type.getSuperclass());    Class []ifcs = type.getInterfaces();    for (Class ifc : ifcs) {      introspectMethods(moduleContext, ifc);    }  }    public JavaMethod getToString()  {    return __toString;  }  public StringValue toString(Env env,                              JavaValue value)  {    if (__toString == null)      return null;    return __toString.callMethod(env, value, new Expr[0]).toStringValue();  }    /**   *   * @return false if printRImpl not implemented   * @throws IOException   */  public boolean printRImpl(Env env,                               Object obj,                               WriteStream out,                               int depth,                               IdentityHashMap<Value, String> valueSet)    throws IOException  {    try {      if (_printRImpl == null) {        return false;      }      _printRImpl.invoke(obj, env, out, depth, valueSet);      return true;    } catch (Exception e) {      throw new QuercusException(e);    }  }  public boolean varDumpImpl(Env env,                             Object obj,                             WriteStream out,                             int depth,                             IdentityHashMap<Value, String> valueSet)    throws IOException  {    try {      if (_varDumpImpl == null)        return false;      _varDumpImpl.invoke(obj, env, out, depth, valueSet);      return true;    } catch (Exception e) {      throw new QuercusException(e);    }  }    private class JavaTraversableDelegate    implements TraversableDelegate  {    private Method _iteratorMethod;        public JavaTraversableDelegate(Method iterator)    {      _iteratorMethod = iterator;    }        public Iterator<Map.Entry<Value, Value>>      getIterator(Env env, ObjectValue qThis)    {      try {        Iterator iterator	  = (Iterator) _iteratorMethod.invoke(qThis.toJavaObject());                return new JavaIterator(env, iterator);      }      catch (Exception e) {        throw new QuercusRuntimeException(e);      }    }    public Iterator<Value> getKeyIterator(Env env, ObjectValue qThis)    {      try {        Iterator iterator =          (Iterator) _iteratorMethod.invoke(qThis.toJavaObject());                return new JavaKeyIterator(iterator);      }      catch (Exception e) {        throw new QuercusRuntimeException(e);      }    }        public Iterator<Value> getValueIterator(Env env, ObjectValue qThis)    {      try {        Iterator iterator =          (Iterator) _iteratorMethod.invoke(qThis.toJavaObject());        return new JavaValueIterator(env, iterator);      }      catch (Exception e) {        throw new QuercusRuntimeException(e);      }    }  }  private class JavaKeyIterator    implements Iterator<Value>  {    private Iterator _iterator;    private int _index;        public JavaKeyIterator(Iterator iterator)    {      _iterator = iterator;    }        public Value next()    {      _iterator.next();      return LongValue.create(_index++);    }        public boolean hasNext()    {      return _iterator.hasNext();    }        public void remove()    {      throw new UnsupportedOperationException();    }  }  private class JavaValueIterator    implements Iterator<Value>  {    private Env _env;    private Iterator _iterator;        public JavaValueIterator(Env env, Iterator iterator)    {      _env = env;      _iterator = iterator;    }        public Value next()    {      return _env.wrapJava(_iterator.next());    }        public boolean hasNext()    {      if (_iterator != null)	return _iterator.hasNext();      else	return false;    }        public void remove()    {      throw new UnsupportedOperationException();    }  }  private class JavaIterator    implements Iterator<Map.Entry<Value, Value>>  {    private Env _env;    private Iterator _iterator;        private int _index;        public JavaIterator(Env env, Iterator iterator)    {      _env = env;      _iterator = iterator;    }        public Map.Entry<Value, Value> next()    {      Object next = _iterator.next();      int index = _index++;            if (next instanceof Map.Entry) {        Map.Entry entry = (Map.Entry) next;                if (entry.getKey() instanceof Value &&            entry.getValue() instanceof Value)        {          return (Map.Entry<Value, Value>) entry;        }        else {          Value key = _env.wrapJava(entry.getKey());          Value val = _env.wrapJava(entry.getValue());                    return new JavaEntry(key, val);        }      }      else {        return new JavaEntry(LongValue.create(index), _env.wrapJava(next));      }    }        public boolean hasNext()    {      if (_iterator != null)	return _iterator.hasNext();      else	return false;    }        public void remove()    {      throw new UnsupportedOperationException();    }  }  private class JavaEntry    implements Map.Entry<Value, Value>  {    private Value _key;    private Value _value;        public JavaEntry(Value key, Value value)    {      _key = key;      _value = value;    }        public Value getKey()    {      return _key;    }        public Value getValue()    {      return _value;    }        public Value setValue(Value value)    {      throw new UnsupportedOperationException();    }  }  private class MethodMarshalPair {    public Method _method;    public Marshal _marshal;    public MethodMarshalPair(Method method,                              Marshal marshal)    {      _method = method;      _marshal = marshal;    }  }  private class FieldMarshalPair {    public Field _field;    public Marshal _marshal;    public FieldMarshalPair(Field field,                             Marshal marshal)    {      _field = field;      _marshal = marshal;    }  }    private static class LongClassDef extends JavaClassDef {    LongClassDef(ModuleContext module)    {      super(module, "Long", Long.class);    }    @Override    public Value wrap(Env env, Object obj)    {      return LongValue.create(((Number) obj).longValue());    }  }    private static class DoubleClassDef extends JavaClassDef {    DoubleClassDef(ModuleContext module)    {      super(module, "Double", Double.class);    }    @Override    public Value wrap(Env env, Object obj)    {      return new DoubleValue(((Number) obj).doubleValue());    }  }    private static class BigIntegerClassDef extends JavaClassDef {    BigIntegerClassDef(ModuleContext module)    {      super(module, "BigInteger", BigInteger.class);    }    @Override    public Value wrap(Env env, Object obj)    {      return new BigIntegerValue(env, (BigInteger) obj, this);    }  }    private static class BigDecimalClassDef extends JavaClassDef {    BigDecimalClassDef(ModuleContext module)    {      super(module, "BigDecimal", BigDecimal.class);    }    @Override    public Value wrap(Env env, Object obj)    {      return new BigDecimalValue(env, (BigDecimal) obj, this);    }  }    private static class StringClassDef extends JavaClassDef {    StringClassDef(ModuleContext module)    {      super(module, "String", String.class);    }    @Override    public Value wrap(Env env, Object obj)    {      return env.createString((String) obj);    }  }    private static class BooleanClassDef extends JavaClassDef {    BooleanClassDef(ModuleContext module)    {      super(module, "Boolean", Boolean.class);    }    @Override    public Value wrap(Env env, Object obj)    {      if (Boolean.TRUE.equals(obj))	return BooleanValue.TRUE;      else	return BooleanValue.FALSE;    }  }    private static class CalendarClassDef extends JavaClassDef {    CalendarClassDef(ModuleContext module)    {      super(module, "Calendar", Calendar.class);    }    @Override    public Value wrap(Env env, Object obj)    {      return new JavaCalendarValue(env, (Calendar)obj, this);    }  }    private static class DateClassDef extends JavaClassDef {    DateClassDef(ModuleContext module)    {      super(module, "Date", Date.class);    }    @Override    public Value wrap(Env env, Object obj)    {      return new JavaDateValue(env, (Date)obj, this);    }  }    private static class URLClassDef extends JavaClassDef {    URLClassDef(ModuleContext module)    {      super(module, "URL", URL.class);    }    @Override    public Value wrap(Env env, Object obj)    {      return new JavaURLValue(env, (URL)obj, this);    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?