当前位置: 首页>>代码示例>>Java>>正文


Java MethodSymbol.getTypeParameters方法代码示例

本文整理汇总了Java中com.sun.tools.javac.code.Symbol.MethodSymbol.getTypeParameters方法的典型用法代码示例。如果您正苦于以下问题:Java MethodSymbol.getTypeParameters方法的具体用法?Java MethodSymbol.getTypeParameters怎么用?Java MethodSymbol.getTypeParameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.sun.tools.javac.code.Symbol.MethodSymbol的用法示例。


在下文中一共展示了MethodSymbol.getTypeParameters方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: attributeParamIdentifier

import com.sun.tools.javac.code.Symbol.MethodSymbol; //导入方法依赖的package包/类
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:24,代码来源:JavacTrees.java

示例2: checkLostType

import com.sun.tools.javac.code.Symbol.MethodSymbol; //导入方法依赖的package包/类
private Description checkLostType(MethodInvocationTree tree, VisitorState state) {
  Type futureType =
      Objects.requireNonNull(state.getTypeFromString("java.util.concurrent.Future"));

  MethodSymbol sym = ASTHelpers.getSymbol(tree);
  Type returnType = ASTHelpers.getResultType(tree);
  Type returnedFutureType = state.getTypes().asSuper(returnType, futureType.tsym);
  if (returnedFutureType != null && !returnedFutureType.isRaw()) {
    if (ASTHelpers.isSubtype(
        ASTHelpers.getUpperBound(returnedFutureType.getTypeArguments().get(0), state.getTypes()),
        futureType,
        state)) {
      return buildDescription(tree)
          .setMessage(String.format("Method returns a nested type, %s", returnType))
          .build();
    }

    // The type variable that determines the generic on the returned future was not a Future.
    // However, many methods (like guava's Futures.transform) have signatures like this:
    // Future<O> do(SomeObject<? extends O>). If O resolves to java.lang.Object or ?, then a
    // SomeObject<Future> is a valid parameter to pass, but results in a nested future.
    Type methodReturnType = sym.getReturnType();
    List<TypeVariableSymbol> typeParameters = sym.getTypeParameters();
    Set<TypeVariableSymbol> returnTypeChoosing = new HashSet<>();
    // For each type variable on the method, see if we can reach the type declared as the param
    // of the returned Future, by traversing its type bounds. If we can reach it, we know that if
    // an argument is passed to an invocation of this method where the type variable is a subtype
    // of Future, that means that a nested Future is being returned.
    for (TypeVariableSymbol tvs : typeParameters) {
      Queue<TypeVariableSymbol> queue = new ArrayDeque<>();
      queue.add(tvs);
      while (!queue.isEmpty()) {
        TypeVariableSymbol currentTypeParam = queue.poll();
        for (Type typeParam : methodReturnType.getTypeArguments()) {
          if (typeParam.tsym == currentTypeParam) {
            returnTypeChoosing.add(tvs);
          }
        }
        for (Type toAdd : currentTypeParam.getBounds()) {
          if (toAdd.tsym instanceof TypeVariableSymbol) {
            queue.add((TypeVariableSymbol) toAdd.tsym);
          }
        }
      }
    }
    // If at least one of the method's type parameters is involved in determining the returned
    // Future's type, check each passed parameter to ensure that it is never passed as a subtype
    // of Future.
    if (!returnTypeChoosing.isEmpty()) {
      Multimap<TypeVariableSymbol, TypeInfo> resolved = getResolvedGenerics(tree);
      for (TypeVariableSymbol returnTypeChoosingSymbol : returnTypeChoosing) {
        Collection<TypeInfo> types = resolved.get(returnTypeChoosingSymbol);
        for (TypeInfo type : types) {
          if (ASTHelpers.isSubtype(type.resolvedVariableType, futureType, state)) {
            return buildDescription(type.tree)
                .setMessage(
                    String.format(
                        "Invocation produces a nested type - Type variable %s, as part of return "
                            + "type %s resolved to %s.",
                        returnTypeChoosingSymbol, methodReturnType, type.resolvedVariableType))
                .build();
          }
        }
      }
    }
  }
  if (allOf(
          allOf(
              parentNode(FutureReturnValueIgnored::isObjectReturningLambdaExpression),
              not(AbstractReturnValueIgnored::expectedExceptionTest)),
          specializedMatcher(),
          not((t, s) -> ASTHelpers.isVoidType(ASTHelpers.getType(t), s)))
      .matches(tree, state)) {
    return describe(tree, state);
  }

  return Description.NO_MATCH;
}
 
开发者ID:google,项目名称:error-prone,代码行数:79,代码来源:FutureReturnValueIgnored.java

示例3: matchAnnotation

import com.sun.tools.javac.code.Symbol.MethodSymbol; //导入方法依赖的package包/类
@Override
public Description matchAnnotation(AnnotationTree annoTree, VisitorState state) {
  if (!IS_COMPATIBLE_WITH_ANNOTATION.matches(annoTree, state)) {
    return Description.NO_MATCH;
  }

  // Hunt for type args on the declared method
  // TODO(glorioso): Once annotation is TYPE_USE, make sure that the node is actually a method
  // parameter
  MethodTree methodTree = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
  MethodSymbol declaredMethod = ASTHelpers.getSymbol(methodTree);

  // If this method overrides other methods, ensure that none of them have @CompatibleWith.
  // This restriction may need to be removed to allow more complex declaration hierarchies.
  for (MethodSymbol methodSymbol :
      ASTHelpers.findSuperMethods(declaredMethod, state.getTypes())) {
    if (methodSymbol
        .params()
        .stream()
        .anyMatch(p -> ASTHelpers.hasAnnotation(p, CompatibleWith.class, state))) {
      return describeWithMessage(
          annoTree,
          String.format(
              "This method overrides a method in %s that already has @CompatibleWith",
              methodSymbol.owner.getSimpleName()));
    }
  }

  List<TypeVariableSymbol> potentialTypeVars =
      new ArrayList<>(declaredMethod.getTypeParameters());

  // Check enclosing types (not superclasses)
  ClassSymbol cs = (ClassSymbol) declaredMethod.owner;
  do {
    potentialTypeVars.addAll(cs.getTypeParameters());
    cs = cs.isInner() ? cs.owner.enclClass() : null;
  } while (cs != null);

  if (potentialTypeVars.isEmpty()) {
    return describeWithMessage(
        annoTree, "There are no type arguments in scope to match against.");
  }

  Set<String> validNames =
      potentialTypeVars
          .stream()
          .map(TypeVariableSymbol::getSimpleName)
          .map(Object::toString)
          .collect(toImmutableSet());
  String constValue = valueArgumentFromCompatibleWithAnnotation(annoTree);

  if (constValue == null || constValue.isEmpty()) {
    return describeWithMessage(
        annoTree,
        String.format(
            "The value of @CompatibleWith must not be empty (valid arguments are %s)",
            printTypeArgs(validNames)));
  }

  return validNames.contains(constValue)
      ? Description.NO_MATCH
      : describeWithMessage(
          annoTree,
          String.format(
              "%s is not a valid type argument. Valid arguments are: %s",
              constValue, printTypeArgs(validNames)));
}
 
开发者ID:google,项目名称:error-prone,代码行数:68,代码来源:CompatibleWithMisuse.java


注:本文中的com.sun.tools.javac.code.Symbol.MethodSymbol.getTypeParameters方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。