private static boolean targetTypeMatches()

in nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java [345:383]


  private static boolean targetTypeMatches(Symbol sym, TypeAnnotationPosition position) {
    switch (sym.getKind()) {
      case LOCAL_VARIABLE:
        return position.type == TargetType.LOCAL_VARIABLE;
      case FIELD:
      case ENUM_CONSTANT: // treated like a field
        return position.type == TargetType.FIELD;
      case CONSTRUCTOR:
      case METHOD:
        return position.type == TargetType.METHOD_RETURN;
      case PARAMETER:
        if (position.type.equals(TargetType.METHOD_FORMAL_PARAMETER)) {
          int parameterIndex = position.parameter_index;
          if (position.onLambda != null) {
            com.sun.tools.javac.util.List<JCTree.JCVariableDecl> lambdaParams =
                position.onLambda.params;
            return parameterIndex < lambdaParams.size()
                && lambdaParams.get(parameterIndex).sym.equals(sym);
          } else {
            return ((Symbol.MethodSymbol) sym.owner).getParameters().indexOf(sym) == parameterIndex;
          }
        } else {
          return false;
        }
      case CLASS:
      case ENUM: // treated like a class
        // There are no type annotations on the top-level type of the class/enum being declared,
        // only on other types in the signature (e.g. `class Foo extends Bar<@A Baz> {}`).
        return false;
      default:
        // Compare with toString() to preserve compatibility with JDK 11
        if (sym.getKind().toString().equals("RECORD")) {
          // Records are treated like classes
          return false;
        } else {
          throw new AssertionError("unsupported element kind " + sym.getKind() + " symbol " + sym);
        }
    }
  }