Merge "Bug 1113 - ietf-restconf needs to specify the version of ietf-yangtypes that...
[yangtools.git] / common / checkstyle-logging / src / main / java / org / opendaylight / yangtools / checkstyle / CheckLoggingUtil.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 java.util.List;
12
13 import org.slf4j.Logger;
14
15 import com.google.common.collect.Lists;
16 import com.puppycrawl.tools.checkstyle.api.DetailAST;
17 import com.puppycrawl.tools.checkstyle.api.FullIdent;
18 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
19 import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
20
21 public class CheckLoggingUtil {
22
23     public static final String LOGGER_TYPE_NAME = Logger.class.getSimpleName();
24     public static final String LOGGER_TYPE_FULL_NAME = Logger.class.getName();
25     public static final String LOGGER_VAR_NAME = "LOG";
26     private static final List<String> LOG_METHODS = Lists.newArrayList("debug", "info", "error", "warn", "trace");
27
28     private CheckLoggingUtil() {}
29
30     public static String getTypeName(final DetailAST aAST) {
31         final FullIdent ident = CheckUtils.createFullType(aAST.findFirstToken(TokenTypes.TYPE));
32         return ident.getText();
33     }
34
35     public static boolean isLoggerType(final DetailAST aAST) {
36         final String typeName = getTypeName(aAST);
37         if(typeName.equals(LOGGER_TYPE_FULL_NAME) || typeName.equals(LOGGER_TYPE_NAME)) {
38             return true;
39         }
40         return false;
41     }
42
43     public static String getVariableName(final DetailAST aAST) {
44         DetailAST identifier = aAST.findFirstToken(TokenTypes.IDENT);
45         return identifier.getText();
46     }
47
48     public static boolean itsAFieldVariable(final DetailAST aAST) {
49         return aAST.getParent().getType() == TokenTypes.OBJBLOCK;
50     }
51
52     public static String getMethodName(final DetailAST aAST) {
53         if(aAST.getFirstChild().getLastChild() != null) {
54             return aAST.getFirstChild().getLastChild().getText();
55         }
56         return aAST.getFirstChild().getText();
57     }
58
59     public static boolean isLogMethod(final String methodName) {
60         return LOG_METHODS.contains(methodName);
61     }
62
63 }