Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.puppycrawl.tools.checkstyle.checks.coding
Class EqualsAvoidNullCheck

    java.lang.Object
      com.puppycrawl.tools.checkstyle.api.AutomaticBean
          com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
              com.puppycrawl.tools.checkstyle.api.Check
                  com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck

All Implemented Interfaces:
  Configurable, Contextualizable

    public class EqualsAvoidNullCheck

extends Check

Checks that any combination of String literals with optional assignment is on the left side of an equals() comparison.

Rationale: Calling the equals() method on String literals will avoid a potential NullPointerException. Also, it is pretty common to see null check right before equals comparisons which is not necessary in the below example. For example:

        String nullString = null;
        nullString.equals("My_Sweet_String");

should be refactored to

        String nullString = null;
        "My_Sweet_String".equals(nullString);

Limitations: If the equals method is overridden or a covariant equals method is defined and the implementation is incorrect (where s.equals(t) does not return the same result as t.equals(s)) then rearranging the called on object and parameter may have unexpected results
Java's Autoboxing feature has an affect on how this check is implemented. Pre Java 5 all IDENT + IDENT object concatenations would not cause a NullPointerException even if null. Those situations could have been included in this check. They would simply act as if they surrounded by String.valueof() which would concatenate the String null.

The following example will cause a NullPointerException as a result of what autoboxing does.

     Integer i = null, j = null;
     String number = "5"
     number.equals(i + j);

Since, it is difficult to determine what kind of Object is being concatenated all ident concatenation is considered unsafe.

Author:
  Travis Schneeberger version 1.0

Constructor Summary
`EqualsAvoidNullCheck()`

Method Summary
`int[]` | `getDefaultTokens()` Returns the default token a check is interested in.
`void` | `setIgnoreEqualsIgnoreCase(boolean aNewValue)` Whether to ignore checking `String.equalsIgnoreCase(String)`.
`void` | `visitToken(DetailAST aMethodCall)` Called to process a token.

Methods inherited from class com.puppycrawl.tools.checkstyle.api.Check
`beginTree, destroy, finishTree, getAcceptableTokens, getClassLoader, getFileContents, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens`

Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
`getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity`

Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
`configure, contextualize, finishLocalSetup, getConfiguration, setupChild`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### EqualsAvoidNullCheck

    public EqualsAvoidNullCheck()

Method Detail

### getDefaultTokens

    public int[] getDefaultTokens()

  Description copied from class: `Check`
  Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.

  Specified by:
    `getDefaultTokens` in class `Check`
  Returns:
    the default tokens
  See Also:
    `TokenTypes`

### visitToken

    public void visitToken(DetailAST aMethodCall)

  Description copied from class: `Check`
  Called to process a token.

  Overrides:
    `visitToken` in class `Check`
  Parameters:
    `aMethodCall` - the token to process

### setIgnoreEqualsIgnoreCase

    public void setIgnoreEqualsIgnoreCase(boolean aNewValue)

  Whether to ignore checking `String.equalsIgnoreCase(String)`.

  Parameters:
    `aNewValue` - whether to ignore checking `String.equalsIgnoreCase(String)`.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2001-2011. All Rights Reserved.