com.sun.tools.javac.code.Types.isConvertible()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(112)

本文整理了Java中com.sun.tools.javac.code.Types.isConvertible()方法的一些代码示例,展示了Types.isConvertible()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Types.isConvertible()方法的具体详情如下:
包路径:com.sun.tools.javac.code.Types
类名称:Types
方法名:isConvertible

Types.isConvertible介绍

[英]Is t a subtype of or convertible via boxing/unboxing conversions to s?
[中]t是s的子类型,还是可以通过装箱/拆箱转换为s来转换?

代码示例

代码示例来源:origin: google/error-prone

/** Classifies bad casts. */
private static String identifyBadCast(Type lhs, Type rhs, Types types) {
 if (!lhs.isPrimitive()) {
  return null;
 }
 if (types.isConvertible(rhs, lhs)) {
  // Exemption if the rhs is convertible to the lhs.
  // This allows, e.g.: <byte> &= <byte> since the narrowing conversion can never be
  // detected.
  // This also allows, for example, char += char, which could overflow, but this is no
  // different than any other integral addition.
  return null;
 }
 return String.format(
   "Compound assignments from %s to %s hide lossy casts", prettyType(rhs), prettyType(lhs));
}

代码示例来源:origin: org.jvnet.sorcerer/sorcerer-javac

/**
 * Is t a subtype of or convertiable via boxing/unboxing
 * convertions to s?
 */
public boolean isConvertible(Type t, Type s) {
  return isConvertible(t, s, Warner.noWarnings);
}
// </editor-fold>

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

/**
 * Is t a subtype of or convertiable via boxing/unboxing
 * convertions to s?
 */
public boolean isConvertible(Type t, Type s) {
  return isConvertible(t, s, noWarnings);
}
// </editor-fold>

代码示例来源:origin: sc.fiji/javac

/**
 * Is t a subtype of or convertiable via boxing/unboxing
 * convertions to s?
 */
public boolean isConvertible(Type t, Type s) {
  return isConvertible(t, s, Warner.noWarnings);
}
// </editor-fold>

代码示例来源:origin: konsoletyper/teavm-javac

/**
 * Is t a subtype of or convertible via boxing/unboxing
 * conversions to s?
 */
public boolean isConvertible(Type t, Type s) {
  return isConvertible(t, s, noWarnings);
}
// </editor-fold>

代码示例来源:origin: com.google.errorprone/error_prone_core

/** Classifies bad casts. */
private static String identifyBadCast(Type lhs, Type rhs, Types types) {
 if (!lhs.isPrimitive()) {
  return null;
 }
 if (types.isConvertible(rhs, lhs)) {
  // Exemption if the rhs is convertible to the lhs.
  // This allows, e.g.: <byte> &= <byte> since the narrowing conversion can never be
  // detected.
  // This also allows, for example, char += char, which could overflow, but this is no
  // different than any other integral addition.
  return null;
 }
 return String.format(
   "Compound assignments from %s to %s hide lossy casts", prettyType(rhs), prettyType(lhs));
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

public boolean compatible(Type found, Type req, Warner warn) {
  return strict ?
      types.isSubtypeUnchecked(found, deferredAttrContext.inferenceContext.asFree(req), warn) :
      types.isConvertible(found, deferredAttrContext.inferenceContext.asFree(req), warn);
}

代码示例来源:origin: org.jvnet.sorcerer/sorcerer-javac

/** Check if a parameter list accepts a list of args.
 */
boolean argumentsAcceptable(List<Type> argtypes,
              List<Type> formals,
              boolean allowBoxing,
              boolean useVarargs,
              Warner warn) {
  Type varargsFormal = useVarargs ? formals.last() : null;
  while (argtypes.nonEmpty() && formals.head != varargsFormal) {
    boolean works = allowBoxing
      ? types.isConvertible(argtypes.head, formals.head, warn)
      : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
    if (!works) return false;
    argtypes = argtypes.tail;
    formals = formals.tail;
  }
  if (formals.head != varargsFormal) return false; // not enough args
  if (!useVarargs)
    return argtypes.isEmpty();
  Type elt = types.elemtype(varargsFormal);
  while (argtypes.nonEmpty()) {
    if (!types.isConvertible(argtypes.head, elt, warn))
      return false;
    argtypes = argtypes.tail;
  }
  return true;
}

代码示例来源:origin: sc.fiji/javac

/** Check if a parameter list accepts a list of args.
 */
boolean argumentsAcceptable(List<Type> argtypes,
              List<Type> formals,
              boolean allowBoxing,
              boolean useVarargs,
              Warner warn) {
  Type varargsFormal = useVarargs ? formals.last() : null;
  while (argtypes.nonEmpty() && formals.head != varargsFormal) {
    boolean works = allowBoxing
      ? types.isConvertible(argtypes.head, formals.head, warn)
      : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
    if (!works) return false;
    argtypes = argtypes.tail;
    formals = formals.tail;
  }
  if (formals.head != varargsFormal) return false; // not enough args
  if (!useVarargs)
    return argtypes.isEmpty();
  Type elt = types.elemtype(varargsFormal);
  while (argtypes.nonEmpty()) {
    if (!types.isConvertible(argtypes.head, elt, warn))
      return false;
    argtypes = argtypes.tail;
  }
  return true;
}

代码示例来源:origin: konsoletyper/teavm-javac

private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) {
  if (types.isConvertible(actual, formal, warn))
    return;
  if (formal.isCompound()
    && types.isSubtype(actual, types.supertype(formal))
    && types.isSubtypeUnchecked(actual, types.interfaces(formal), warn))
    return;
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) {
  if (types.isConvertible(actual, formal, warn))
    return;
  if (formal.isCompound()
    && types.isSubtype(actual, types.supertype(formal))
    && types.isSubtypeUnchecked(actual, types.interfaces(formal), warn))
    return;
}

代码示例来源:origin: konsoletyper/teavm-javac

public boolean compatible(Type found, Type req, Warner warn) {
  InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
  return strict ?
      types.isSubtypeUnchecked(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn) :
      types.isConvertible(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn);
}

代码示例来源:origin: org.jvnet.sorcerer/sorcerer-javac

private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) {
  if (types.isConvertible(actual, formal, warn))
    return;
  if (formal.isCompound()
    && types.isSubtype(actual, types.supertype(formal))
    && types.isSubtypeUnchecked(actual, types.interfaces(formal), warn))
    return;
  if (false) {
    // TODO: make assertConvertible work
    chk.typeError(tree.pos(), JCDiagnostic.fragment("incompatible.types"), actual, formal);
    throw new AssertionError("Tree: " + tree
                 + " actual:" + actual
                 + " formal: " + formal);
  }
}

代码示例来源:origin: sc.fiji/javac

private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) {
  if (types.isConvertible(actual, formal, warn))
    return;
  if (formal.isCompound()
    && types.isSubtype(actual, types.supertype(formal))
    && types.isSubtypeUnchecked(actual, types.interfaces(formal), warn))
    return;
  if (false) {
    // TODO: make assertConvertible work
    chk.typeError(tree.pos(), JCDiagnostic.fragment("incompatible.types"), actual, formal);
    throw new AssertionError("Tree: " + tree
                 + " actual:" + actual
                 + " formal: " + formal);
  }
}

代码示例来源:origin: org.jvnet.sorcerer/sorcerer-javac

/**
 * Is t is castable to s?<br>
 * s is assumed to be an erased type.<br>
 * (not defined for Method and ForAll types).
 */
public boolean isCastable(Type t, Type s, Warner warn) {
  if (t == s)
    return true;
  if (t.isPrimitive() != s.isPrimitive())
    return allowBoxing && isConvertible(t, s, warn);
  if (warn != warnStack.head) {
    try {
      warnStack = warnStack.prepend(warn);
      return isCastable.visit(t, s);
    } finally {
      warnStack = warnStack.tail;
    }
  } else {
    return isCastable.visit(t, s);
  }
}
// where

代码示例来源:origin: sc.fiji/javac

/**
 * Is t is castable to s?<br>
 * s is assumed to be an erased type.<br>
 * (not defined for Method and ForAll types).
 */
public boolean isCastable(Type t, Type s, Warner warn) {
  if (t == s)
    return true;
  if (t.isPrimitive() != s.isPrimitive())
    return allowBoxing && isConvertible(t, s, warn);
  if (warn != warnStack.head) {
    try {
      warnStack = warnStack.prepend(warn);
      return isCastable.visit(t, s);
    } finally {
      warnStack = warnStack.tail;
    }
  } else {
    return isCastable.visit(t, s);
  }
}
// where

代码示例来源:origin: konsoletyper/teavm-javac

private Type generateReferenceToTargetConstraint(JCTree tree, UndetVar from,
    Type to, Attr.ResultInfo resultInfo,
    InferenceContext inferenceContext) {
  inferenceContext.solve(List.of(from.qtype), new Warner());
  inferenceContext.notifyChange();
  Type capturedType = resultInfo.checkContext.inferenceContext()
      .cachedCapture(tree, from.inst, false);
  if (types.isConvertible(capturedType,
      resultInfo.checkContext.inferenceContext().asUndetVar(to))) {
    //effectively skip additional return-type constraint generation (compatibility)
    return syms.objectType;
  }
  return to;
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

/**
 * Is t is castable to s?<br>
 * s is assumed to be an erased type.<br>
 * (not defined for Method and ForAll types).
 */
public boolean isCastable(Type t, Type s, Warner warn) {
  if (t == s)
    return true;
  if (t.isPrimitive() != s.isPrimitive())
    return allowBoxing && (
        isConvertible(t, s, warn)
        || (allowObjectToPrimitiveCast &&
          s.isPrimitive() &&
          isSubtype(boxedClass(s).type, t)));
  if (warn != warnStack.head) {
    try {
      warnStack = warnStack.prepend(warn);
      checkUnsafeVarargsConversion(t, s, warn);
      return isCastable.visit(t,s);
    } finally {
      warnStack = warnStack.tail;
    }
  } else {
    return isCastable.visit(t,s);
  }
}
// where

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

Type returnConstraintTarget(Type from, Type to) {
  if (from.hasTag(VOID)) {
    return syms.voidType;
  } else if (to.hasTag(NONE)) {
    return from.isPrimitive() ? from : syms.objectType;
  } else if (from.hasTag(UNDETVAR) && to.isPrimitive()) {
    if (!allowGraphInference) {
      //if legacy, just return boxed type
      return types.boxedClass(to).type;
    }
    //if graph inference we need to skip conflicting boxed bounds...
    UndetVar uv = (UndetVar)from;
    for (Type t : uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
      Type boundAsPrimitive = types.unboxedType(t);
      if (boundAsPrimitive == null) continue;
      if (types.isConvertible(boundAsPrimitive, to)) {
        //effectively skip return-type constraint generation (compatibility)
        return syms.objectType;
      }
    }
    return types.boxedClass(to).type;
  } else {
    return to;
  }
}

代码示例来源:origin: konsoletyper/teavm-javac

/**
 * Is t is castable to s?<br>
 * s is assumed to be an erased type.<br>
 * (not defined for Method and ForAll types).
 */
public boolean isCastable(Type t, Type s, Warner warn) {
  if (t == s)
    return true;
  if (t.isPrimitive() != s.isPrimitive())
    return allowBoxing && (
        isConvertible(t, s, warn)
        || (allowObjectToPrimitiveCast &&
          s.isPrimitive() &&
          isSubtype(boxedClass(s).type, t)));
  if (warn != warnStack.head) {
    try {
      warnStack = warnStack.prepend(warn);
      checkUnsafeVarargsConversion(t, s, warn);
      return isCastable.visit(t,s);
    } finally {
      warnStack = warnStack.tail;
    }
  } else {
    return isCastable.visit(t,s);
  }
}
// where

相关文章

Types类方法