Merge "BUG 1130 STRING_DEFAULT_CODEC - deserialize can return null"
[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.Check;
12 import com.puppycrawl.tools.checkstyle.api.DetailAST;
13 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
14
15 public class LoggerVariableModifiersCheck extends Check {
16
17     private static final String LOG_MESSAGE = "Logger must be declared as private static final.";
18
19     @Override
20     public int[] getDefaultTokens() {
21         return new int[]{TokenTypes.VARIABLE_DEF};
22     }
23
24     @Override
25     public void visitToken(DetailAST aAST) {
26         if (CheckLoggingUtil.isAFieldVariable(aAST) && CheckLoggingUtil.isLoggerType(aAST) && !hasPrivatStaticFinalModifier(aAST)) {
27             log(aAST.getLineNo(), LOG_MESSAGE);
28         }
29     }
30
31     private boolean hasPrivatStaticFinalModifier(DetailAST aAST) {
32         DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS);
33         if(modifiers != null) {
34             if(modifiers.branchContains(TokenTypes.LITERAL_PRIVATE) && modifiers.branchContains(TokenTypes.LITERAL_STATIC)
35                     && modifiers.branchContains(TokenTypes.FINAL)) {
36                 return true;
37             }
38         }
39         return false;
40     }
41
42 }