protected boolean validateAnnotationSemantics()

in nullaway/src/main/java/com/uber/nullaway/handlers/contract/fieldcontract/EnsuresNonNullHandler.java [72:112]


  protected boolean validateAnnotationSemantics(
      NullAway analysis, VisitorState state, MethodTree tree, Symbol.MethodSymbol methodSymbol) {
    String message;
    if (tree.getBody() == null) {
      return true;
    }
    Set<String> nonnullFieldsOfReceiverAtExit =
        analysis
            .getNullnessAnalysis(state)
            .getNonnullFieldsOfReceiverAtExit(new TreePath(state.getPath(), tree), state.context)
            .stream()
            .map(e -> e.getSimpleName().toString())
            .collect(Collectors.toSet());
    Set<String> fieldNames = getAnnotationValueArray(methodSymbol, annotName, false);
    if (fieldNames == null) {
      fieldNames = Collections.emptySet();
    }
    fieldNames = ContractUtils.trimReceivers(fieldNames);
    boolean isValidLocalPostCondition = nonnullFieldsOfReceiverAtExit.containsAll(fieldNames);
    if (!isValidLocalPostCondition) {
      fieldNames.removeAll(nonnullFieldsOfReceiverAtExit);
      message =
          "method: "
              + methodSymbol
              + " is annotated with @EnsuresNonNull annotation, it indicates that all fields in the annotation parameter"
              + " must be guaranteed to be nonnull at exit point. However, the method's body fails to ensure this for the following fields: "
              + fieldNames;

      state.reportMatch(
          analysis
              .getErrorBuilder()
              .createErrorDescription(
                  new ErrorMessage(ErrorMessage.MessageTypes.POSTCONDITION_NOT_SATISFIED, message),
                  tree,
                  analysis.buildDescription(tree),
                  state,
                  null));
      return false;
    }
    return true;
  }