Fixed some major sonar issues
[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 final 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("LOG.debug", "LOG.info", "LOG.error", "LOG.warn", "LOG.trace");
27
28     private CheckLoggingUtil() {
29         throw new UnsupportedOperationException("Utility class should not be instantiated!");
30     }
31
32     public static String getTypeName(final DetailAST aAST) {
33         final FullIdent ident = CheckUtils.createFullType(aAST.findFirstToken(TokenTypes.TYPE));
34         return ident.getText();
35     }
36
37     public static boolean isLoggerType(final DetailAST aAST) {
38         final String typeName = getTypeName(aAST);
39         return typeName.equals(LOGGER_TYPE_FULL_NAME) || typeName.equals(LOGGER_TYPE_NAME);
40     }
41
42     public static String getVariableName(final DetailAST aAST) {
43         DetailAST identifier = aAST.findFirstToken(TokenTypes.IDENT);
44         return identifier.getText();
45     }
46
47     public static boolean isAFieldVariable(final DetailAST aAST) {
48         return aAST.getParent().getType() == TokenTypes.OBJBLOCK;
49     }
50
51     public static String getMethodName(final DetailAST aAST) {
52         if(aAST.getFirstChild().getLastChild() != null) {
53             return aAST.getFirstChild().getFirstChild().getText() + "." + aAST.getFirstChild().getLastChild().getText();
54         }
55         return aAST.getFirstChild().getText();
56     }
57
58     public static boolean isLogMethod(final String methodName) {
59         return LOG_METHODS.contains(methodName);
60     }
61
62     public static String getClassName(final DetailAST aAST) {
63         DetailAST parent = aAST.getParent();
64         while(parent.getType() != TokenTypes.CLASS_DEF && parent.getType() != TokenTypes.ENUM_DEF) {
65             parent = parent.getParent();
66         }
67         return parent.findFirstToken(TokenTypes.IDENT).getText();
68     }
69
70 }