示例#1
0
  @SuppressWarnings("unchecked")
  public static Map<String, Field> getAllFieldMap(Class<?> resultClass) {
    List<Class<?>> allSuperclasses = ClassUtils.getAllSuperclasses(resultClass);

    Map<String, Field> m = new HashMap<String, Field>();
    for (Class<?> one : allSuperclasses) {
      Field[] fields = one.getDeclaredFields();
      for (Field o : fields) {
        m.put(o.getName(), o);
      }
    }
    Field[] fields = resultClass.getDeclaredFields();
    for (Field o : fields) {
      m.put(o.getName(), o);
    }

    return m;
  }
  /**
   * 从{@link #handleres handleres}的<code>key</code>的迭代器中找出参数<code>clazz</code> 最近的父类
   *
   * @param clazz 异常类型
   * @return 参数<code>clazz</code>最近的父类, 如果参数没有直接的父类返回<code>null</code>
   */
  private Class findSuperClass(Iterator iterator, Class clazz) {
    Class ret = null;
    List classes = ClassUtils.getAllSuperclasses(clazz);
    int minIndex = -1;
    while (iterator.hasNext()) {
      Class candidate = (Class) iterator.next();
      int candidateIndex = classes.indexOf(candidate);
      if (minIndex == -1 || (candidateIndex != -1 && candidateIndex < minIndex)) {
        minIndex = candidateIndex;
      }
    }

    if (minIndex > -1) {
      ret = (Class) classes.get(minIndex);
    }

    return ret;
  }
  /** {@inheritDoc} */
  @SuppressWarnings({"unchecked", "rawtypes"})
  public ControlAccessor getControlAccessor(Object control) {
    Class<? extends ControlAccessor> accessorClass = null;
    ControlAccessor accessor = null;
    Class<? extends Object> clazz = control.getClass();

    accessorClass = accessors.get(clazz);

    if (accessorClass == null) { // try with superclasses

      List superclasses = ClassUtils.getAllSuperclasses(clazz);
      superclasses.addAll(ClassUtils.getAllInterfaces(clazz));
      Iterator iter = superclasses.iterator();

      while (iter.hasNext() && accessorClass == null) {
        accessorClass = accessors.get(iter.next());
      }
    }

    if (accessorClass != null) {
      try {
        Constructor<? extends ControlAccessor> ctor = accessorClass.getConstructor(Object.class);
        accessor = ctor.newInstance(control);
      } catch (InstantiationException e) {
        log.error(e);
      } catch (IllegalAccessException e) {
        log.error(e);
      } catch (SecurityException e) {
        log.error(e);
      } catch (NoSuchMethodException e) {
        log.error(e);
      } catch (IllegalArgumentException e) {
        log.error(e);
      } catch (InvocationTargetException e) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(baos));
        log.error(baos);
      }
    } else {
      log.warn("Can't find a accessor for class: " + clazz.getName());
    }

    return accessor;
  }