本文整理汇总了Java中com.sun.tools.javac.code.Types.isSubtype方法的典型用法代码示例。如果您正苦于以下问题:Java Types.isSubtype方法的具体用法?Java Types.isSubtype怎么用?Java Types.isSubtype使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.code.Types
的用法示例。
在下文中一共展示了Types.isSubtype方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: matches
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public boolean matches(ExpressionTree tree, VisitorState state) {
Types types = state.getTypes();
Type classType = state.getSymtab().classType;
Type runtimeExceptionType = state.getSymtab().runtimeExceptionType;
Type argType = getType(tree);
// Make sure that the argument is a Class<Something> (and not null/bottom).
if (!isSubtype(argType, classType, state) || argType.getTag() == BOT) {
return false;
}
List<Type> typeArguments = ((ClassType) argType).getTypeArguments();
Type exceptionType = typeArguments.isEmpty() ? null : typeArguments.get(0);
return types.isSubtype(exceptionType, runtimeExceptionType);
}
示例2: isSubtype
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
/** Returns true if {@code erasure(s) <: erasure(t)}. */
public static boolean isSubtype(Type s, Type t, VisitorState state) {
if (s == null || t == null) {
return false;
}
Types types = state.getTypes();
return types.isSubtype(types.erasure(s), types.erasure(t));
}
示例3: isKnownCheckedException
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
boolean isKnownCheckedException(VisitorState state, Type type) {
Types types = state.getTypes();
Symtab symtab = state.getSymtab();
// Check erasure for generics.
type = types.erasure(type);
return
// Has to be some Exception: A variable of type Throwable might be an Error.
types.isSubtype(type, symtab.exceptionType)
// Has to be some subtype: A variable of type Exception might be a RuntimeException.
&& !types.isSameType(type, symtab.exceptionType)
// Can't be of type RuntimeException.
&& !types.isSubtype(type, symtab.runtimeExceptionType);
}
示例4: matches
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public boolean matches(T tree, VisitorState state) {
Types types = state.getTypes();
Type typeToCompare = typeToCompareSupplier.get(state);
return (typeToCompare != null &&
types.isSubtype(((JCTree) tree).type, types.erasure(typeToCompare)));
}
示例5: tryAsStaticMember
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
/** Returns a {@code StaticImportInfo} for a static field or method import. */
private static StaticImportInfo tryAsStaticMember(
JCTree.JCFieldAccess access, VisitorState state) {
Name identifier = access.getIdentifier();
if (identifier.contentEquals("*")) {
// Java doesn't allow non-canonical types inside wildcard imports,
// so there's nothing to do here.
return null;
}
String importedTypeName = access.getExpression().toString();
Type importedType = state.getTypeFromString(importedTypeName);
if (importedType == null) {
return null;
}
Types types = state.getTypes();
Type canonicalType = types.erasure(importedType);
if (canonicalType == null) {
return null;
}
Symbol.TypeSymbol baseType;
{
Symbol sym = ASTHelpers.getSymbol(access.getExpression());
if (!(sym instanceof Symbol.TypeSymbol)) {
return null;
}
baseType = (Symbol.TypeSymbol) sym;
}
Symbol.PackageSymbol pkgSym =
((JCTree.JCCompilationUnit) state.getPath().getCompilationUnit()).packge;
ImmutableSet<Symbol> members = lookup(baseType, baseType, identifier, types, pkgSym);
if (members.isEmpty()) {
return null;
}
/* Find the most specific subtype that defines one of the members that is imported.
* TODO(gak): we should instead find the most specific subtype with a member that is _used_ */
Type canonicalOwner = null;
for (Symbol member : members) {
Type owner = types.erasure(member.owner.type);
if (canonicalOwner == null || types.isSubtype(owner, canonicalOwner)) {
canonicalOwner = owner;
}
}
if (canonicalOwner == null) {
return null;
}
return StaticImportInfo.create(
importedTypeName, canonicalOwner.toString(), identifier.toString(), members);
}
示例6: matchTypeCast
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public Description matchTypeCast(TypeCastTree tree, VisitorState state) {
if (!BUNDLE_DESERIALIZATION_CAST_EXPRESSION.matches(tree, state)) {
return NO_MATCH;
}
Tree targetType = tree.getType();
// Casting to primitive types shouldn't cause issues since they extend no type and are final.
if (isPrimitiveType().matches(targetType, state)) {
return NO_MATCH;
}
/*
* Ordering of these checks determines precedence of types (which type *should* be cast to).
* Deduced by inspecting serialization code, see
* https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/Parcel.java#1377
* Simply allow specially handled final types, and check that other specially handled types
* are cast to their base types.
*
* There is no documentation of what types are safe to cast to, so this code is paralleling the
* code cited above to emulate the same logic in order to produce the correct behavior.
*/
if (isSameType("java.lang.String").matches(targetType, state)) {
return NO_MATCH;
}
if (isSubtypeOf("java.util.Map").matches(targetType, state)) {
// Make an exception for HashMap.
return anyOf(isSameType("java.util.Map"), isSameType("java.util.HashMap"))
.matches(targetType, state)
? NO_MATCH
: getDescriptionForType(tree, "Map");
}
// All Parcelables handled after this point have their types preserved.
if (isSubtypeOf("android.os.Parcelable").matches(targetType, state)) {
return NO_MATCH;
}
if (isSubtypeOf("java.lang.CharSequence").matches(targetType, state)) {
return isSameType("java.lang.CharSequence").matches(targetType, state)
? NO_MATCH
: getDescriptionForType(tree, "CharSequence");
}
if (isSubtypeOf("java.util.List").matches(targetType, state)) {
// Make an exception for ArrayList.
return anyOf(isSameType("java.util.List"), isSameType("java.util.ArrayList"))
.matches(targetType, state)
? NO_MATCH
: getDescriptionForType(tree, "List");
}
if (isSubtypeOf("android.util.SparseArray").matches(targetType, state)) {
return isSameType("android.util.SparseArray").matches(targetType, state)
? NO_MATCH
: getDescriptionForType(tree, "SparseArray");
}
// Check component types of arrays. The only type that may cause problems is CharSequence[].
if (isArrayType().matches(targetType, state)) {
Type componentType = ((ArrayType) getType(targetType)).getComponentType();
Types types = state.getTypes();
Type charSequenceType = typeFromString("java.lang.CharSequence").get(state);
Type stringType = typeFromString("java.lang.String").get(state);
// Okay to cast to String[] because String[] is written before CharSequence[]
// in the serialization code.
if (types.isSubtype(componentType, charSequenceType)
&& !types.isSameType(componentType, charSequenceType)
&& !types.isSameType(componentType, stringType)) {
return getDescriptionForType(tree, "CharSequence[]");
}
}
return NO_MATCH;
}
示例7: getInstance
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
/**
* Return an instance of the given type if it receives special handling by {@code String.format}.
* For example, an intance of {@link Integer} will be returned for an input of type {@code int} or
* {@link Integer}.
*/
private static Object getInstance(Tree tree, VisitorState state) {
Object value = ASTHelpers.constValue(tree);
if (value != null) {
return value;
}
Type type = ASTHelpers.getType(tree);
Types types = state.getTypes();
if (type.getKind() == TypeKind.NULL) {
return null;
}
// normalize boxed primitives
type = types.unboxedTypeOrType(types.erasure(type));
if (type.isPrimitive()) {
switch (type.getKind()) {
case BOOLEAN:
return false;
case BYTE:
return Byte.valueOf((byte) 1);
case SHORT:
return Short.valueOf((short) 2);
case INT:
return Integer.valueOf(3);
case LONG:
return Long.valueOf(4);
case CHAR:
return Character.valueOf('c');
case FLOAT:
return Float.valueOf(5.0f);
case DOUBLE:
return Double.valueOf(6.0d);
case VOID:
case NONE:
case NULL:
case ERROR:
return null;
case ARRAY:
return new Object[0];
default:
break;
}
}
if (types.isSubtype(type, state.getSymtab().stringType)) {
return String.valueOf("string");
}
if (types.isSubtype(type, state.getTypeFromString(BigDecimal.class.getName()))) {
return BigDecimal.valueOf(42.0d);
}
if (types.isSubtype(type, state.getTypeFromString(BigInteger.class.getName()))) {
return BigInteger.valueOf(43L);
}
if (types.isSubtype(type, state.getTypeFromString(Date.class.getName()))) {
return new Date();
}
if (types.isSubtype(type, state.getTypeFromString(Calendar.class.getName()))) {
return new GregorianCalendar();
}
if (types.isSubtype(type, state.getTypeFromString(TemporalAccessor.class.getName()))) {
return LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
}
return new Object();
}