Avoid catch (NullPointerException e), do explicit if == null 75/40975/3
authorMichael Vorburger <vorburger@redhat.com>
Tue, 28 Jun 2016 23:45:26 +0000 (01:45 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 29 Jun 2016 08:44:34 +0000 (08:44 +0000)
This will be required to migrate this code upstream, see
https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/457, so
might as well already do this better here, for now.

Change-Id: Ic5e90867353b5239a0d6a768e889b75190d33305
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
common/checkstyle-logging/src/main/java/org/opendaylight/yangtools/checkstyle/CheckLoggingUtil.java
common/checkstyle-logging/src/main/java/org/opendaylight/yangtools/checkstyle/LoggerFactoryClassParameterCheck.java

index 1c5a6e970c43d7565599a8b72f49425c673113fb..e8f86bffb8652565b98544aa1832d429aec98e80 100644 (file)
@@ -8,15 +8,13 @@
 
 package org.opendaylight.yangtools.checkstyle;
 
-import java.util.List;
-
-import org.slf4j.Logger;
-
 import com.google.common.collect.Lists;
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 import com.puppycrawl.tools.checkstyle.api.FullIdent;
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
+import java.util.List;
+import org.slf4j.Logger;
 
 public final class CheckLoggingUtil {
 
index 1dc307d76e56a96b35e76477998b454e4df54c82..b3812f8ae01709818c6b2a848928045ff19ea220 100644 (file)
@@ -27,16 +27,34 @@ public class LoggerFactoryClassParameterCheck extends AbstractCheck {
         final String methodName = CheckLoggingUtil.getMethodName(ast);
         if (methodName.equals(METHOD_NAME)) {
             final String className = CheckLoggingUtil.getClassName(ast);
-            try {
-                final String token = ast.findFirstToken(TokenTypes.ELIST).getFirstChild().getFirstChild()
-                    .getFirstChild().getText();
-                if (!token.equals(className)) {
-                    log(ast.getLineNo(), LOG_MESSAGE);
-                }
-            } catch (NullPointerException e) {
-                log(ast.getLineNo(), String.format("Invalid parameter in \"getLogger\" method call in class: %s",
-                    className));
+            DetailAST findFirstToken = ast.findFirstToken(TokenTypes.ELIST);
+            if (findFirstToken == null) {
+                logError(ast, className);
+                return;
+            }
+            DetailAST childToken = findFirstToken.getFirstChild();
+            if (childToken == null) {
+                logError(ast, className);
+                return;
+            }
+            childToken = childToken.getFirstChild();
+            if (childToken == null) {
+                logError(ast, className);
+                return;
+            }
+            childToken = childToken.getFirstChild();
+            if (childToken == null) {
+                logError(ast, className);
+                return;
+            }
+            final String token = childToken.getText();
+            if (!token.equals(className)) {
+                log(ast.getLineNo(), LOG_MESSAGE);
             }
         }
     }
-}
\ No newline at end of file
+
+    protected void logError(DetailAST ast, String className) {
+        log(ast.getLineNo(), String.format("Invalid parameter in \"getLogger\" method call in class: %s", className));
+    }
+}