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


Java MethodSymbol.getReturnType方法代码示例

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


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

示例1: matches

import com.sun.tools.javac.code.Symbol.MethodSymbol; //导入方法依赖的package包/类
@Override
public boolean matches(ExpressionTree tree, VisitorState state) {
  Type futureType =
      Objects.requireNonNull(state.getTypeFromString("java.util.concurrent.Future"));
  Symbol untypedSymbol = ASTHelpers.getSymbol(tree);
  if (!(untypedSymbol instanceof MethodSymbol)) {
    Type resultType = ASTHelpers.getResultType(tree);
    return resultType != null
        && ASTHelpers.isSubtype(
            ASTHelpers.getUpperBound(resultType, state.getTypes()), futureType, state);
  }
  MethodSymbol sym = (MethodSymbol) untypedSymbol;
  if (hasAnnotation(sym, CanIgnoreReturnValue.class, state)) {
    return false;
  }
  for (MethodSymbol superSym : ASTHelpers.findSuperMethods(sym, state.getTypes())) {
    // There are interfaces annotated with @CanIgnoreReturnValue (like Guava's Function)
    // whose return value really shouldn't be ignored - as a heuristic, check if the super's
    // method is returning a future subtype.
    if (hasAnnotation(superSym, CanIgnoreReturnValue.class, state)
        && ASTHelpers.isSubtype(
            ASTHelpers.getUpperBound(superSym.getReturnType(), state.getTypes()),
            futureType,
            state)) {
      return false;
    }
  }
  if (BLACKLIST.matches(tree, state)) {
    return false;
  }
  Type returnType = sym.getReturnType();
  return ASTHelpers.isSubtype(
      ASTHelpers.getUpperBound(returnType, state.getTypes()), futureType, state);
}
 
开发者ID:google,项目名称:error-prone,代码行数:35,代码来源:FutureReturnValueIgnored.java

示例2: matchMethod

import com.sun.tools.javac.code.Symbol.MethodSymbol; //导入方法依赖的package包/类
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
  MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodTree);

  if (methodSymbol.isConstructor()) {
    return Description.NO_MATCH;
  }

  if (isMethodCanBeOverridden(methodSymbol, state)) {
    return Description.NO_MATCH;
  }

  if (ANNOTATED_WITH_PRODUCES_OR_PROVIDES.matches(methodTree, state)) {
    return Description.NO_MATCH;
  }

  Type returnType = methodSymbol.getReturnType();
  if (ImmutableCollections.isImmutableType(returnType)) {
    return Description.NO_MATCH;
  }

  ImmutableSet<ClassType> returnStatementsTypes = getMethodReturnTypes(methodTree);
  if (returnStatementsTypes.isEmpty()) {
    return Description.NO_MATCH;
  }
  boolean alwaysReturnsImmutableType =
      returnStatementsTypes.stream().allMatch(ImmutableCollections::isImmutableType);
  if (!alwaysReturnsImmutableType) {
    return Description.NO_MATCH;
  }

  Optional<String> immutableReturnType =
      ImmutableCollections.mutableToImmutable(getTypeQualifiedName(returnType));
  if (!immutableReturnType.isPresent()) {
    immutableReturnType =
        getCommonImmutableTypeForAllReturnStatementsTypes(returnStatementsTypes);
  }
  if (!immutableReturnType.isPresent()) {
    return Description.NO_MATCH;
  }

  Type newReturnType = state.getTypeFromString(immutableReturnType.get());
  SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
  fixBuilder.replace(
      getTypeTree(methodTree.getReturnType()),
      SuggestedFixes.qualifyType(state, fixBuilder, newReturnType.asElement()));
  SuggestedFix fix = fixBuilder.build();

  return describeMatch(methodTree.getReturnType(), fix);
}
 
开发者ID:google,项目名称:error-prone,代码行数:51,代码来源:MutableMethodReturnType.java

示例3: 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


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