b3812f8ae01709818c6b2a848928045ff19ea220
[yangtools.git] / common / checkstyle-logging / src / main / java / org / opendaylight / yangtools / checkstyle / LoggerFactoryClassParameterCheck.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.checkstyle;
10
11 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
12 import com.puppycrawl.tools.checkstyle.api.DetailAST;
13 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
14
15 public class LoggerFactoryClassParameterCheck extends AbstractCheck {
16
17     private static final String LOG_MESSAGE = "LoggerFactory.getLogger Class argument is incorrect.";
18     private static final String METHOD_NAME = "LoggerFactory.getLogger";
19
20     @Override
21     public int[] getDefaultTokens() {
22         return new int[] { TokenTypes.METHOD_CALL };
23     }
24
25     @Override
26     public void visitToken(DetailAST ast) {
27         final String methodName = CheckLoggingUtil.getMethodName(ast);
28         if (methodName.equals(METHOD_NAME)) {
29             final String className = CheckLoggingUtil.getClassName(ast);
30             DetailAST findFirstToken = ast.findFirstToken(TokenTypes.ELIST);
31             if (findFirstToken == null) {
32                 logError(ast, className);
33                 return;
34             }
35             DetailAST childToken = findFirstToken.getFirstChild();
36             if (childToken == null) {
37                 logError(ast, className);
38                 return;
39             }
40             childToken = childToken.getFirstChild();
41             if (childToken == null) {
42                 logError(ast, className);
43                 return;
44             }
45             childToken = childToken.getFirstChild();
46             if (childToken == null) {
47                 logError(ast, className);
48                 return;
49             }
50             final String token = childToken.getText();
51             if (!token.equals(className)) {
52                 log(ast.getLineNo(), LOG_MESSAGE);
53             }
54         }
55     }
56
57     protected void logError(DetailAST ast, String className) {
58         log(ast.getLineNo(), String.format("Invalid parameter in \"getLogger\" method call in class: %s", className));
59     }
60 }