标签:section checked efi ora 子类 table sam javase 通过
/**
* Is t a subtype of or convertible via boxing/unboxing conversion to s?
*/
public boolean isConvertible(Type t, Type s, Warner warn) {
if (t.tag == ERROR)
return true;
boolean tPrimitive = t.isPrimitive();
boolean sPrimitive = s.isPrimitive();
if (tPrimitive == sPrimitive) {
return isSubtypeUnchecked(t, s, warn);
}
if (!allowBoxing)
return false;
return tPrimitive
? isSubtype(boxedClass(t).type, s)
: isSubtype(unboxedType(t), s);
}
当两个类型相同时(同时为原始类型或引用类型)通过调用isSubtypeUnchecked()方法来进一步判断。
/**
* Is t an unchecked subtype of s?
*/
public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
boolean result = isSubtypeUncheckedInternal(t, s, warn);
if (result) {
checkUnsafeVarargsConversion(t, s, warn);
}
return result;
}
在如上方法中涉及到两个重要的方法isSubtypeUncheckedInternal()与checkUnsafeVarargsConversion(),不过优先调用前一个方法,如果是Unchecked Subtype关系,则需要检查是否为Unsafe Varargs Conversion。
private boolean isSubtypeUncheckedInternal(Type t, Type s, Warner warn) {
if (t.tag == ARRAY && s.tag == ARRAY) {
if (((ArrayType)t).elemtype.tag <= lastBaseTag) {
return isSameType(elemtype(t), elemtype(s));
} else {
return isSubtypeUnchecked(elemtype(t), elemtype(s), warn);
}
} else if (isSubtype(t, s)) {
return true;
}
else if (t.tag == TYPEVAR) {
return isSubtypeUnchecked(t.getUpperBound(), s, warn);
}
else if (s.tag == UNDETVAR) {
UndetVar uv = (UndetVar)s;
if (uv.inst != null)
return isSubtypeUnchecked(t, uv.inst, warn);
}
else if (!s.isRaw()) {
Type t2 = asSuper(t, s.tsym);
if (t2 != null && t2.isRaw()) {
if (isReifiable(s))
warn.silentWarn(LintCategory.UNCHECKED);
else
warn.warn(LintCategory.UNCHECKED);
return true;
}
}
return false;
}
当两个类型是数组类型时,并且是原始数组类型时,则数组类型的元素类型必须相同,也就是byte[]与int[]没有父子类关系,如下代码会报错:
byte[] x = new byte[10];
int[] y = (int[])x;
/**
* Is t is castable to s?
* s is assumed to be an erased type.
* (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);
}
}
Types方法之isCastable-isConvertible
标签:section checked efi ora 子类 table sam javase 通过
原文地址:http://www.cnblogs.com/extjs4/p/7620053.html