示例#1
0
  public static int scoreMethods(
      final MetaClass[] arguments, final MetaParameter[] parmTypes, final boolean isVarArgs) {
    int score = 0;
    for (int i = 0; i != arguments.length; i++) {
      final MetaClass actualParamType;
      if (isVarArgs && !arguments[arguments.length - 1].isArray() && i >= parmTypes.length - 1)
        actualParamType = parmTypes[parmTypes.length - 1].getType().getComponentType();
      else actualParamType = parmTypes[i].getType();

      if (arguments[i] == null) {
        if (!actualParamType.isPrimitive()) {
          score += 6;
        } else {
          score = 0;
          break;
        }
      } else if (actualParamType.equals(arguments[i])) {
        score += 7;
      } else if (actualParamType.isPrimitive() && actualParamType.asBoxed().equals(arguments[i])) {
        score += 6;
      } else if (arguments[i].isPrimitive() && arguments[i].asUnboxed().equals(actualParamType)) {
        score += 6;
      } else if (actualParamType.isAssignableFrom(arguments[i])) {
        score += 5;
      } else if (isNumericallyCoercible(arguments[i], actualParamType)) {
        score += 4;
      } else if (actualParamType.asBoxed().isAssignableFrom(arguments[i].asBoxed())
          && !Object_MetaClass.equals(arguments[i])) {
        score += 3 + scoreInterface(actualParamType, arguments[i]);
      } else if (canConvert(actualParamType, arguments[i])) {
        if (actualParamType.isArray() && arguments[i].isArray()) {
          final MetaClass outerComponentTypeActual = actualParamType.getOuterComponentType();
          final MetaClass outerComponentTypeArg = arguments[i].getOuterComponentType();

          if (canConvert(outerComponentTypeActual, outerComponentTypeArg)) {
            score += 1;
          } else {
            continue;
          }
        } else if (actualParamType.equals(char_MetaClass)
            && arguments[i].equals(String_MetaClass)) {
          score += 1;
        }

        score += 1;
      } else if (actualParamType.equals(Object_MetaClass)
          || arguments[i].equals(NullType_MetaClass)) {
        score += 1;
      } else {
        score = 0;
        break;
      }
    }
    return score;
  }
示例#2
0
  public static void assertAssignableTypes(
      final Context context, final MetaClass from, final MetaClass to) {
    if (!to.asBoxed().isAssignableFrom(from.asBoxed())) {
      if (to.isArray()
          && from.isArray()
          && GenUtil.getArrayDimensions(to) == GenUtil.getArrayDimensions(from)
          && to.getOuterComponentType().isAssignableFrom(from.getOuterComponentType())) {
        return;
      }

      if (!context.isPermissiveMode()) {
        if (classAliases.contains(from.getFullyQualifiedName())
            && classAliases.contains(to.getFullyQualifiedName())) {
          // handle convertibility between MetaClass API and java Class reference.
          return;
        }

        throw new InvalidTypeException(
            to.getFullyQualifiedName() + " is not assignable from " + from.getFullyQualifiedName());
      }
    }
  }