304572d476178c56cc2db7c166f91677dc07f4ec
[yangtools.git] / common / checkstyle-logging / src / main / java / org / opendaylight / yangtools / checkstyle / LoggerDeclarationsCountCheck.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 static org.opendaylight.yangtools.checkstyle.CheckLoggingUtil.isAFieldVariable;
12
13 import com.puppycrawl.tools.checkstyle.api.Check;
14 import com.puppycrawl.tools.checkstyle.api.DetailAST;
15 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
16
17 public class LoggerDeclarationsCountCheck extends Check {
18
19     private static final String LOG_MESSAGE = "Logger might be declared only once.";
20     private String prevClassName = "";
21
22     @Override
23     public int[] getDefaultTokens() {
24         return new int[]{TokenTypes.VARIABLE_DEF};
25     }
26
27     @Override
28     public void visitToken(DetailAST ast) {
29         if (CheckLoggingUtil.isLoggerType(ast) && isAFieldVariable(ast)) {
30             final String className = CheckLoggingUtil.getClassName(ast);
31             if (this.prevClassName.equals(className)) {
32                 log(ast.getLineNo(), LOG_MESSAGE);
33             }
34             this.prevClassName = className;
35         }
36     }
37
38     @Override
39     public void finishTree(DetailAST rootAST) {
40         super.finishTree(rootAST);
41         this.prevClassName = "";
42     }
43
44 }