e5e079d88b6b4c73d67239b1998f269b60d6eadb
[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 com.google.common.collect.Lists;
12 import com.puppycrawl.tools.checkstyle.api.DetailAST;
13 import com.puppycrawl.tools.checkstyle.api.FullIdent;
14 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
15 import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
16 import java.util.List;
17 import org.slf4j.Logger;
18
19 public final class CheckLoggingUtil {
20
21     public static final String LOGGER_TYPE_NAME = Logger.class.getSimpleName();
22     public static final String LOGGER_TYPE_FULL_NAME = Logger.class.getName();
23     public static final String LOGGER_VAR_NAME = "LOG";
24     private static final List<String> LOG_METHODS =
25             Lists.newArrayList("LOG.debug", "LOG.info", "LOG.error", "LOG.warn", "LOG.trace");
26
27     private CheckLoggingUtil() {
28         throw new UnsupportedOperationException("Utility class should not be instantiated!");
29     }
30
31     public static String getTypeName(final DetailAST ast) {
32         final FullIdent ident = CheckUtils.createFullType(ast.findFirstToken(TokenTypes.TYPE));
33         return ident.getText();
34     }
35
36     public static boolean isLoggerType(final DetailAST ast) {
37         final String typeName = getTypeName(ast);
38         return typeName.equals(LOGGER_TYPE_FULL_NAME) || typeName.equals(LOGGER_TYPE_NAME);
39     }
40
41     public static String getVariableName(final DetailAST ast) {
42         DetailAST identifier = ast.findFirstToken(TokenTypes.IDENT);
43         return identifier.getText();
44     }
45
46     public static boolean isAFieldVariable(final DetailAST ast) {
47         return ast.getParent().getType() == TokenTypes.OBJBLOCK;
48     }
49
50     /**
51      * Returns the name the method (and the enclosing class) at a given point specified by the
52      * passed-in abstract syntax tree (AST).
53      *
54      * @param ast an abstract syntax tree (AST) pointing to method call
55      * @return the name of the method being called
56      */
57     public static String getMethodName(final DetailAST ast) {
58         if (ast.getFirstChild().getLastChild() != null) {
59             return ast.getFirstChild().getFirstChild().getText() + "." + ast.getFirstChild().getLastChild().getText();
60         }
61         return ast.getFirstChild().getText();
62     }
63
64     public static boolean isLogMethod(final String methodName) {
65         return LOG_METHODS.contains(methodName);
66     }
67
68     /**
69      * Returns the name of the closest enclosing class of the point by the passed-in abstract syntax
70      * tree (AST).
71      *
72      * @param ast an abstract syntax tree (AST)
73      * @return the name of the closest enclosign class
74      */
75     public static String getClassName(final DetailAST ast) {
76         DetailAST parent = ast.getParent();
77         while (parent.getType() != TokenTypes.CLASS_DEF && parent.getType() != TokenTypes.ENUM_DEF) {
78             parent = parent.getParent();
79         }
80         return parent.findFirstToken(TokenTypes.IDENT).getText();
81     }
82
83 }