/* (non-Javadoc)
   * @see edu.umd.cs.findbugs.ba.INullnessAnnotationDatabase#parameterMustBeNonNull(edu.umd.cs.findbugs.ba.XMethod, int)
   */
  public boolean parameterMustBeNonNull(XMethod m, int param) {
    if (DEBUG) {
      System.out.print("Checking " + m + " param " + param + " for @Nonnull...");
    }
    TypeQualifierAnnotation tqa =
        TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(
            m, param, nonnullTypeQualifierValue);

    if (tqa == null && param == 0) {
      String name = m.getName();
      String signature = m.getSignature();
      if (name.equals("main")
          && signature.equals("([Ljava/lang/String;)V")
          && m.isStatic()
          && m.isPublic()) return true;
      else if (NullnessAnnotationDatabase.assertsFirstParameterIsNonnull(m)) return true;
      else if (name.equals("compareTo")
          && signature.substring(signature.indexOf(";") + 1).equals(")Z")
          && !m.isStatic()) return true;
    }
    boolean answer = (tqa != null) && tqa.when == When.ALWAYS;

    if (DEBUG) {
      System.out.println(answer ? "yes" : "no");
    }

    return answer;
  }
示例#2
0
  private boolean bad(Item left, Item right) {
    XMethod m = left.getReturnValueOf();

    if (m == null) return false;
    Object value = right.getConstant();
    if (!(value instanceof Integer) && ((Integer) value).intValue() == 0) return false;
    if (m.isStatic() || !m.isPublic()) return false;

    if (m.getName().equals("compareTo") && m.getSignature().equals("(Ljava/lang/Object;)I"))
      return true;
    if (m.getName().equals("compare")
        && m.getSignature().equals("(Ljava/lang/Object;Ljava/lang/Object;)I")) return true;

    return false;
  }
  public void visitClassContext(ClassContext classContext) {

    JavaClass jclass = classContext.getJavaClass();

    for (Method method : jclass.getMethods()) {
      XMethod xmethod = XFactory.createXMethod(classContext.getJavaClass(), method);
      ParameterProperty nonnullParameters =
          AnalysisContext.currentAnalysisContext()
              .getUnconditionalDerefParamDatabase()
              .getProperty(xmethod.getMethodDescriptor());
      if (nonnullParameters != null) {
        for (int p : nonnullParameters.iterable()) {
          TypeQualifierAnnotation directTypeQualifierAnnotation =
              TypeQualifierApplications.getDirectTypeQualifierAnnotation(
                  xmethod, p, nonnullTypeQualifierValue);
          if (directTypeQualifierAnnotation != null
              && directTypeQualifierAnnotation.when == When.UNKNOWN) {
            //
            // The LocalVariableAnnotation is constructed using the local variable
            // number of the parameter, not the parameter number.
            //
            int paramLocal = xmethod.isStatic() ? p : p + 1;

            reporter.reportBug(
                new BugInstance(
                        this,
                        "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE",
                        NORMAL_PRIORITY)
                    .addClassAndMethod(jclass, method)
                    .add(
                        LocalVariableAnnotation.getParameterLocalVariableAnnotation(
                            method, paramLocal)));
          }
        }
      }
    }
  }