Bump odlparent to 3.0.0-SNAPSHOT and fix breakages
[yangtools.git] / common / checkstyle-logging / src / main / java / org / opendaylight / yangtools / checkstyle / LoggerVariableModifiersCheck.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 LoggerVariableModifiersCheck extends AbstractCheck {
16
17     private static final String LOG_MESSAGE = "Logger must be declared as private static final.";
18     private static final int[] TOKENS = { TokenTypes.VARIABLE_DEF };
19
20     @Override
21     public int[] getDefaultTokens() {
22         return TOKENS;
23     }
24
25     @Override
26     public int[] getAcceptableTokens() {
27         return TOKENS;
28     }
29
30     @Override
31     public int[] getRequiredTokens() {
32         return TOKENS;
33     }
34
35     @Override
36     public void visitToken(final DetailAST ast) {
37         if (CheckLoggingUtil.isAFieldVariable(ast) && CheckLoggingUtil.isLoggerType(ast)
38                 && !hasPrivatStaticFinalModifier(ast)) {
39             log(ast.getLineNo(), LOG_MESSAGE);
40         }
41     }
42
43     private static boolean hasPrivatStaticFinalModifier(final DetailAST ast) {
44         final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
45         return modifiers != null && modifiers.branchContains(TokenTypes.LITERAL_PRIVATE)
46                 && modifiers.branchContains(TokenTypes.LITERAL_STATIC) && modifiers.branchContains(TokenTypes.FINAL);
47     }
48 }