示例#1
0
  /**
   * Checks the type compatibility<br>
   * Two types are compatible when and only when:
   *
   * <ol>
   *   <li>They are exactly the same
   *   <li>The left parameter is {@linkplain Object} or {@linkplain AnyType} and the right parameter
   *       is either {@linkplain Object} or an array
   *   <li>The left parameter is assignable from the right one (is a superclass of the right one)
   * </ol>
   *
   * @param left The left type parameter
   * @param right The right type parameter
   * @return Returns true if a value of the left {@linkplain Type} can hold the value of the right
   *     {@linkplain Type}
   */
  public static boolean isCompatible(Type left, Type right) {
    if (left.equals(right)) {
      return true;
    } else if (isArray(left)) {
      return false;
    } else if (isObjectOrAnyType(left)) {
      int sort2 = right.getSort();
      return (sort2 == Type.OBJECT || sort2 == Type.ARRAY);
    } else if (isPrimitive(left)) {
      // a primitive type requires strict equality
      return left.equals(right);
    } else {
      ClassLoader cl = Thread.currentThread().getContextClassLoader();
      if (cl == null) {
        cl = ClassLoader.getSystemClassLoader();
      }
      // those classes should already have been loaded at this point
      Class clzLeft, clzRight;
      try {
        clzLeft = cl.loadClass(left.getClassName());
      } catch (Throwable e) {
        clzLeft = Object.class;
      }

      // anything is assignable to Object
      if (clzLeft == Object.class) {
        return true;
      }

      try {
        clzRight = cl.loadClass(right.getClassName());
      } catch (Throwable e) {
        clzRight = Object.class;
      }
      return (clzLeft.isAssignableFrom(clzRight));
    }
  }