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

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

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

Types.closure介绍

[英]Returns the closure of a class or interface type.
[中]返回类或接口类型的闭包。

代码示例

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

private static Stream<MethodSymbol> findSuperMethods(
  MethodSymbol methodSymbol, Types types, boolean skipInterfaces) {
 TypeSymbol owner = (TypeSymbol) methodSymbol.owner;
 return types.closure(owner.type).stream()
   .filter(closureTypes -> skipInterfaces ? !closureTypes.isInterface() : true)
   .map(type -> findSuperMethodInType(methodSymbol, type, types))
   .filter(Objects::nonNull);
}

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

/**
 * Finds all methods in any superclass of {@code startClass} with a certain {@code name} that
 * match the given {@code predicate}.
 *
 * @return The (possibly empty) set of methods in any superclass that match {@code predicate} and
 *     have the given {@code name}.
 */
public static Set<MethodSymbol> findMatchingMethods(
  Name name, final Predicate<MethodSymbol> predicate, Type startClass, Types types) {
 Filter<Symbol> matchesMethodPredicate =
   sym -> sym instanceof MethodSymbol && predicate.apply((MethodSymbol) sym);
 Set<MethodSymbol> matchingMethods = new HashSet<>();
 // Iterate over all classes and interfaces that startClass inherits from.
 for (Type superClass : types.closure(startClass)) {
  // Iterate over all the methods declared in superClass.
  TypeSymbol superClassSymbol = superClass.tsym;
  Scope superClassSymbols = superClassSymbol.members();
  if (superClassSymbols != null) { // Can be null if superClass is a type variable
   for (Symbol symbol :
     superClassSymbols.getSymbolsByName(name, matchesMethodPredicate, NON_RECURSIVE)) {
    // By definition of the filter, we know that the symbol is a MethodSymbol.
    matchingMethods.add((MethodSymbol) symbol);
   }
  }
 }
 return matchingMethods;
}

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

/** Finds all the visible fields declared or inherited in the target class */
public static List<VarSymbol> findAllFields(Type classType, VisitorState state) {
 // TODO(andrewrice): Switch collector to ImmutableList.toImmutableList() when released
 return state.getTypes().closure(classType).stream()
   .flatMap(
     type -> {
      TypeSymbol tsym = type.tsym;
      if (tsym == null) {
       return ImmutableList.<VarSymbol>of().stream();
      }
      WriteableScope scope = tsym.members();
      if (scope == null) {
       return ImmutableList.<VarSymbol>of().stream();
      }
      return ImmutableList.copyOf(scope.getSymbols(VarSymbol.class::isInstance)).reverse()
        .stream()
        .map(v -> (VarSymbol) v)
        .filter(v -> isVisible(v, state.getPath()));
     })
   .collect(Collectors.toCollection(ArrayList::new));
}

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

/**
 * Returns the type of the first superclass or superinterface in the hierarchy annotated with
 * {@code @Immutable}, or {@code null} if no such super type exists.
 */
private Type immutableSupertype(Symbol sym, VisitorState state) {
 for (Type superType : state.getTypes().closure(sym.type)) {
  if (superType.tsym.equals(sym.type.tsym)) {
   continue;
  }
  // Don't use getImmutableAnnotation here: subtypes of trusted types are
  // also trusted, only check for explicitly annotated supertypes.
  if (immutableAnnotations.stream()
    .anyMatch(annotation -> ASTHelpers.hasAnnotation(superType.tsym, annotation, state))) {
   return superType;
  }
  // We currently trust that @interface annotations are immutable, but don't enforce that
  // custom interface implementations are also immutable. That means the check can be
  // defeated by writing a custom mutable annotation implementation, and passing it around
  // using the superclass type.
  //
  // TODO(b/25630189): fix this
  //
  // if (superType.tsym.getKind() == ElementKind.ANNOTATION_TYPE) {
  //   return superType;
  // }
 }
 return null;
}

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

private static Symbol getFinalizer(VisitorState state, ClassSymbol enclosing) {
 Type finalizerType = state.getTypeFromString("com.google.common.labs.base.Finalizer");
 Optional<VarSymbol> finalizerField =
   state.getTypes().closure(enclosing.asType()).stream()
     .flatMap(s -> getFields(s.asElement()))
     .filter(s -> ASTHelpers.isSameType(finalizerType, s.asType(), state))
     .findFirst();
 if (finalizerField.isPresent()) {
  return finalizerField.get();
 }
 return ASTHelpers.resolveExistingMethod(
   state,
   enclosing.enclClass(),
   state.getName("finalize"),
   /* argTypes= */ ImmutableList.of(),
   /* tyargTypes= */ ImmutableList.of());
}

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

private Symbol getSuperType(Symbol symbol, String name) {
 for (Type t : types.closure(symbol.type)) {
  if (t.asElement().getSimpleName().contentEquals(name)) {
   return t.asElement();
  }
 }
 return null;
}

代码示例来源:origin: uber/NullAway

private static boolean isMapMethod(Symbol.MethodSymbol symbol, Types types, String methodName) {
 if (!symbol.getSimpleName().toString().equals(methodName)) {
  return false;
 }
 Symbol owner = symbol.owner;
 if (owner.getQualifiedName().toString().equals("java.util.Map")) {
  return true;
 }
 com.sun.tools.javac.util.List<Type> supertypes = types.closure(owner.type);
 for (Type t : supertypes) {
  if (t.asElement().getQualifiedName().toString().equals("java.util.Map")) {
   return true;
  }
 }
 return false;
}

代码示例来源:origin: uber/NullAway

for (Type s : types.closure(owner.type)) {
 if (s.equals(owner.type)) {
  continue;

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

/**
  * Returns the {@link MethodSymbol} of the first method that sym overrides in its supertype
  * closure, or {@code null} if no such method exists.
  */
 private MethodSymbol getFirstOverride(Symbol sym, Types types) {
  ClassSymbol owner = sym.enclClass();
  if (ignoreInterfaceOverrides && owner.isInterface()) {
   // pretend the method does not override anything
   return null;
  }
  for (Type s : types.closure(owner.type)) {
   if (s == owner.type) {
    continue;
   }
   for (Symbol m : s.tsym.members().getSymbolsByName(sym.name)) {
    if (!(m instanceof MethodSymbol)) {
     continue;
    }
    MethodSymbol msym = (MethodSymbol) m;
    if (msym.isStatic()) {
     continue;
    }
    if (sym.overrides(msym, owner, types, /* checkResult= */ false)) {
     return msym;
    }
   }
  }
  return null;
 }
}

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

List<Type> classTypeClosure = state.getTypes().closure(classType);
List<Type> superTypes =
  classTypeClosure.size() <= 1

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

private <T extends Symbol> T getMember(
  Class<T> type, ElementKind kind, Symbol classSymbol, String name) {
 if (classSymbol.type == null) {
  return null;
 }
 for (Type t : types.closure(classSymbol.type)) {
  Scope scope = t.tsym.members();
  for (Symbol sym : scope.getSymbolsByName(getName(name))) {
   if (sym.getKind().equals(kind)) {
    return type.cast(sym);
   }
  }
 }
 if (classSymbol.hasOuterInstance()) {
  T sym = getMember(type, kind, classSymbol.type.getEnclosingType().asElement(), name);
  if (sym != null) {
   return sym;
  }
 }
 if (classSymbol.owner != null
   && classSymbol != classSymbol.owner
   && classSymbol.owner instanceof Symbol.ClassSymbol) {
  T sym = getMember(type, kind, classSymbol.owner, name);
  if (sym != null && sym.isStatic()) {
   return sym;
  }
 }
 return null;
}

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

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
 if (!MATCHER.matches(tree, state)) {
  return NO_MATCH;
 }
 if (state.getTypes().closure(ASTHelpers.getSymbol(tree).enclClass().asType()).stream()
   .anyMatch(
     s ->
       s.asElement()
         .packge()
         .getQualifiedName()
         .toString()
         .startsWith("java.util.concurrent"))) {
  return NO_MATCH;
 }
 if (blockEndsInBreakOrReturn(state)) {
  return NO_MATCH;
 }
 ExpressionTree collection = getReceiver(tree);
 if (collection == null) {
  return NO_MATCH;
 }
 if (!enclosingLoop(state.getPath(), collection)) {
  return NO_MATCH;
 }
 return describeMatch(tree);
}

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

for (Type sup : types.closure(ASTHelpers.getSymbol(classTree).type)) {
 if (equals == null) {
  equals = getMatchingMethod(sup, equalsName, equalsPredicate);

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

/** Check if the method declares or inherits an implementation of .equals() */
 public static boolean implementsEquals(Type type, VisitorState state) {
  Name equalsName = state.getName("equals");
  Symbol objectEquals = getOnlyMember(state, state.getSymtab().objectType, "equals");
  for (Type sup : state.getTypes().closure(type)) {
   if (sup.tsym.isInterface()) {
    continue;
   }
   if (ASTHelpers.isSameType(sup, state.getSymtab().objectType, state)) {
    return false;
   }
   Scope scope = sup.tsym.members();
   if (scope == null) {
    continue;
   }
   for (Symbol sym : scope.getSymbolsByName(equalsName)) {
    if (sym.overrides(objectEquals, type.tsym, state.getTypes(), /* checkResult= */ false)) {
     return true;
    }
   }
  }
  return false;
 }
}

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

private static Stream<MethodSymbol> findSuperMethods(
  MethodSymbol methodSymbol, Types types, boolean skipInterfaces) {
 TypeSymbol owner = (TypeSymbol) methodSymbol.owner;
 return types.closure(owner.type).stream()
   .filter(closureTypes -> skipInterfaces ? !closureTypes.isInterface() : true)
   .map(type -> findSuperMethodInType(methodSymbol, type, types))
   .filter(Objects::nonNull);
}

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

private Symbol getSuperType(Symbol symbol, String name) {
 for (Type t : types.closure(symbol.type)) {
  if (t.asElement().getSimpleName().contentEquals(name)) {
   return t.asElement();
  }
 }
 return null;
}

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

/**
 * {@inheritDoc}
 */
public FilteredMemberList getAllMembers(TypeElement element) {
Symbol sym = cast(Symbol.class, element);
Scope scope = sym.members().dupUnshared();
List<Type> closure = types.closure(sym.asType());
for (Type t : closure)
  addMembers(scope, t);
return new FilteredMemberList(scope);
}
// where

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

List<Type> erasedSupertypes(Type t) {
  ListBuffer<Type> buf = new ListBuffer<>();
  for (Type sup : closure(t)) {
    if (sup.hasTag(TYPEVAR)) {
      buf.append(sup);
    } else {
      buf.append(erasure(sup));
    }
  }
  return buf.toList();
}

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

/**
 * {@inheritDoc}
 */
public FilteredMemberList getAllMembers(TypeElement element) {
  Symbol sym = cast(Symbol.class, element);
  Scope scope = sym.members().dupUnshared();
  List<Type> closure = types.closure(sym.asType());
  for (Type t : closure)
    addMembers(scope, t);
  return new FilteredMemberList(scope);
}
// where

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

/**
 * {@inheritDoc}
 */
public FilteredMemberList getAllMembers(TypeElement element) {
  Symbol sym = cast(Symbol.class, element);
  Scope scope = sym.members().dupUnshared();
  List<Type> closure = types.closure(sym.asType());
  for (Type t : closure)
    addMembers(scope, t);
  return new FilteredMemberList(scope);
}
// where

相关文章

Types类方法