Ejemplo n.º 1
0
 /** {@inheritDoc} */
 @Override
 public boolean isValidOn(final DetailAST ast) {
   final int type = ast.getType();
   return type == TokenTypes.PACKAGE_DEF
       || type == TokenTypes.CLASS_DEF
       || type == TokenTypes.INTERFACE_DEF
       || type == TokenTypes.ENUM_DEF
       || type == TokenTypes.ANNOTATION_DEF
       || type == TokenTypes.METHOD_DEF
       || type == TokenTypes.CTOR_DEF
       || type == TokenTypes.VARIABLE_DEF && !ScopeUtils.isLocalVariableDef(ast);
 }
  @Override
  public void visitToken(DetailAST ast) {
    // do not check local variables and
    // fields declared in interface/annotations
    if (ScopeUtils.isLocalVariableDef(ast) || ScopeUtils.inInterfaceOrAnnotationBlock(ast)) {
      return;
    }

    final DetailAST assign = ast.findFirstToken(TokenTypes.ASSIGN);
    if (assign == null) {
      // no assign - no check
      return;
    }

    final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
    if (modifiers != null && modifiers.branchContains(TokenTypes.FINAL)) {
      // do not check final variables
      return;
    }

    final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
    final DetailAST ident = ast.findFirstToken(TokenTypes.IDENT);
    final DetailAST exprStart = assign.getFirstChild().getFirstChild();
    if (isObjectType(type) && exprStart.getType() == TokenTypes.LITERAL_NULL) {
      log(ident, MSG_KEY, ident.getText(), "null");
    }

    final int primitiveType = type.getFirstChild().getType();
    if (primitiveType == TokenTypes.LITERAL_BOOLEAN
        && exprStart.getType() == TokenTypes.LITERAL_FALSE) {
      log(ident, MSG_KEY, ident.getText(), "false");
    }
    if (isNumericType(primitiveType) && isZero(exprStart)) {
      log(ident, MSG_KEY, ident.getText(), "0");
    }
    if (primitiveType == TokenTypes.LITERAL_CHAR
        && (isZero(exprStart)
            || exprStart.getType() == TokenTypes.CHAR_LITERAL
                && "'\\0'".equals(exprStart.getText()))) {
      log(ident, MSG_KEY, ident.getText(), "\\0");
    }
  }
Ejemplo n.º 3
0
    /** {@inheritDoc} */
    @Override
    public boolean isValidOn(final DetailAST ast) {
      final int type = ast.getType();

      return type == TokenTypes.VARIABLE_DEF && !ScopeUtils.isLocalVariableDef(ast);
    }