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

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

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

Types.asSuper介绍

[英]Return the (most specific) base type of t that starts with the given symbol. If none exists, return null.
[中]返回以给定符号开头的(最具体的)t基类型。如果不存在,则返回null。

代码示例

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

private static List<Type> typeArgsAsSuper(Type baseType, Type superType, VisitorState state) {
 Type projectedType = state.getTypes().asSuper(baseType, superType.tsym);
 if (projectedType != null) {
  return projectedType.getTypeArguments();
 }
 return new ArrayList<>();
}

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

private static Type predicateType(Type type, VisitorState state) {
 Symbol predicate = state.getSymbolFromString(java.util.function.Predicate.class.getName());
 if (predicate == null) {
  return null;
 }
 Type asPredicate = state.getTypes().asSuper(type, predicate);
 if (asPredicate == null) {
  return null;
 }
 return getOnlyElement(asPredicate.getTypeArguments(), null);
}

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

/**
  * Extracts the appropriate type argument from a specific supertype of the given {@code type}.
  * This handles the case when a subtype has different type arguments than the expected type. For
  * example, {@code ClassToInstanceMap<T>} implements {@code Map<Class<? extends T>, T>}.
  *
  * @param type the (sub)type from which to extract the type argument
  * @param superTypeSym the symbol of the supertype on which the type parameter is defined
  * @param typeArgIndex the index of the type argument to extract from the supertype
  * @param types the {@link Types} utility class from the {@link VisitorState}
  * @return the type argument, if defined, or null otherwise
  */
 @Nullable
 protected static final Type extractTypeArgAsMemberOfSupertype(
   Type type, Symbol superTypeSym, int typeArgIndex, Types types) {
  Type collectionType = types.asSuper(type, superTypeSym);
  if (collectionType == null) {
   return null;
  }
  com.sun.tools.javac.util.List<Type> tyargs = collectionType.getTypeArguments();
  if (tyargs.size() <= typeArgIndex) {
   // Collection is raw, nothing we can do.
   return null;
  }

  return tyargs.get(typeArgIndex);
 }
}

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

private static boolean isVariableClassCreator(
   VariableTree variableTree,
   VisitorState state,
   ClassType classType,
   Symbol parcelableCreatorSymbol) {
  Tree typeTree = variableTree.getType();
  Type type = ASTHelpers.getType(typeTree);
  Types types = state.getTypes();
  Type superType = types.asSuper(type, parcelableCreatorSymbol);
  if (superType == null) {
   return false;
  }
  List<Type> typeArguments = superType.getTypeArguments();
  if (typeArguments.isEmpty()) {
   // raw creator
   return true;
  }
  return ASTHelpers.isSubtype(classType, Iterables.getOnlyElement(typeArguments), state);
 }
}

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

private static Type getComparableTypeArgument(ClassTree tree, VisitorState state) {
  final Type comparable =
    state
      .getTypes()
      .asSuper(ASTHelpers.getType(tree), state.getSymtab().comparableType.asElement());

  if (comparable != null && !comparable.getTypeArguments().isEmpty()) {
   return Iterables.getOnlyElement(comparable.getTypeArguments());
  }

  return null;
 }
}

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

@Override
 public boolean matches(Tree tree, VisitorState state) {
  Symbol sym = state.getSymbolFromString(clazz);
  if (sym == null) {
   return false;
  }
  Type type = ASTHelpers.getType(tree);
  if (!ASTHelpers.isSubtype(type, sym.type, state)) {
   return false;
  }
  Types types = state.getTypes();
  Type superType = types.asSuper(type, sym);
  if (superType == null) {
   return false;
  }
  List<Type> typeArguments = superType.getTypeArguments();
  if (typeArguments.isEmpty()) {
   return false;
  }
  return ASTHelpers.isSameType(
    typeArguments.get(typeArgumentIndex), state.getTypeFromString(URL_CLASS), state);
 }
}

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

@Override
 public Description matchMethodInvocation(
   MethodInvocationTree methodInvocationTree, VisitorState visitorState) {
  if (!TO_ARRAY_MATCHER.matches(methodInvocationTree, visitorState)) {
   return NO_MATCH;
  }
  Types types = visitorState.getTypes();
  Type variableType =
    types.elemtype(getType(getOnlyElement(methodInvocationTree.getArguments())));
  if (variableType == null) {
   return NO_MATCH;
  }

  Type collectionType =
    types.asSuper(
      ASTHelpers.getReceiverType(methodInvocationTree),
      visitorState.getSymbolFromString("java.util.Collection"));
  List<Type> typeArguments = collectionType.getTypeArguments();

  if (!typeArguments.isEmpty()
    && !types.isCastable(
      types.erasure(variableType), types.erasure(getOnlyElement(typeArguments)))) {
   return describeMatch(methodInvocationTree);
  }
  return NO_MATCH;
 }
}

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

/** Ignore some common ThreadLocal type arguments that are fine to have per-instance copies of. */
 private boolean wellKnownTypeArgument(NewClassTree tree, VisitorState state) {
  Type type = getType(tree);
  if (type == null) {
   return false;
  }
  type = state.getTypes().asSuper(type, state.getSymbolFromString("java.lang.ThreadLocal"));
  if (type == null) {
   return false;
  }
  if (type.getTypeArguments().isEmpty()) {
   return false;
  }
  Type argType = getOnlyElement(type.getTypeArguments());
  if (WELL_KNOWN_TYPES.contains(argType.asElement().getQualifiedName().toString())) {
   return true;
  }
  if (isSubtype(argType, state.getTypeFromString("java.text.DateFormat"), state)) {
   return true;
  }
  return false;
 }
}

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

} else if (isSubtype(type, state.getTypeFromString("org.hamcrest.Matcher"), state)) {
 Type matcherType =
   state.getTypes().asSuper(type, state.getSymbolFromString("org.hamcrest.Matcher"));
 if (!matcherType.getTypeArguments().isEmpty()) {
  Type matchType = getOnlyElement(matcherType.getTypeArguments());

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

Type returnedFutureType = state.getTypes().asSuper(returnType, futureType.tsym);
if (returnedFutureType != null

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

return NO_MATCH;
Type classType = state.getTypes().asSuper(type, state.getSymtab().classType.asElement());
if (classType == null || classType.getTypeArguments().isEmpty()) {
 return NO_MATCH;

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

ASTHelpers.getType(Iterables.getOnlyElement(methodInvocation.getArguments()));
Symbol extension = state.getSymbolFromString("com.google.protobuf.ExtensionLite");
Type genericsArgument = state.getTypes().asSuper(argumentType, extension);

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

@Override
public Type visitTypeVar(TypeVar t, Symbol sym) {
  if (t.tsym == sym)
    return t;
  else
    return asSuper(t.bound, sym);
}

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

/** does this functional expression require serialization support? */
boolean isSerializable() {
  for (Type target : tree.targets) {
    if (types.asSuper(target, syms.serializableType.tsym) != null) {
      return true;
    }
  }
  return false;
}

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

private static List<Type> typeArgsAsSuper(Type baseType, Type superType, VisitorState state) {
 Type projectedType = state.getTypes().asSuper(baseType, superType.tsym);
 if (projectedType != null) {
  return projectedType.getTypeArguments();
 }
 return new ArrayList<>();
}

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

private static Type predicateType(Type type, VisitorState state) {
 Symbol predicate = state.getSymbolFromString(java.util.function.Predicate.class.getName());
 if (predicate == null) {
  return null;
 }
 Type asPredicate = state.getTypes().asSuper(type, predicate);
 if (asPredicate == null) {
  return null;
 }
 return getOnlyElement(asPredicate.getTypeArguments(), null);
}

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

private JCStatement makeResourceCloseInvocation(JCExpression resource) {
  // convert to AutoCloseable if needed
  if (types.asSuper(resource.type, syms.autoCloseableType.tsym) == null) {
    resource = (JCExpression) convert(resource, syms.autoCloseableType);
  }
  // create resource.close() method invocation
  JCExpression resourceClose = makeCall(resource,
                     names.close,
                     List.<JCExpression>nil());
  return make.Exec(resourceClose);
}

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

private static Type getComparableTypeArgument(ClassTree tree, VisitorState state) {
  final Type comparable =
    state
      .getTypes()
      .asSuper(ASTHelpers.getType(tree), state.getSymtab().comparableType.asElement());

  if (comparable != null && !comparable.getTypeArguments().isEmpty()) {
   return Iterables.getOnlyElement(comparable.getTypeArguments());
  }

  return null;
 }
}

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

public void visitTypeCast(JCTypeCast tree) {
result = genExpr(tree.expr, tree.clazz.type).load();
// Additional code is only needed if we cast to a reference type
// which is not statically a supertype of the expression's type.
// For basic types, the coerce(...) in genExpr(...) will do
// the conversion.
if (tree.clazz.type.tag > lastBaseTag &&
  types.asSuper(tree.expr.type, tree.clazz.type.tsym) == null) {
  code.emitop2(checkcast, makeRef(tree.pos(), tree.clazz.type));
}
}

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

public void visitTypeCast(JCTypeCast tree) {
  setTypeAnnotationPositions(tree.pos);
  result = genExpr(tree.expr, tree.clazz.type).load();
  // Additional code is only needed if we cast to a reference type
  // which is not statically a supertype of the expression's type.
  // For basic types, the coerce(...) in genExpr(...) will do
  // the conversion.
  if (!tree.clazz.type.isPrimitive() &&
    types.asSuper(tree.expr.type, tree.clazz.type.tsym) == null) {
    code.emitop2(checkcast, makeRef(tree.pos(), tree.clazz.type));
  }
}

相关文章

Types类方法