Cleanup use of Guava library
[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.ImmutableSet;
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.Set;
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
25     private static final Set<String> LOG_METHODS = ImmutableSet.of(
26         "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 ast) {
33         final FullIdent ident = CheckUtils.createFullType(ast.findFirstToken(TokenTypes.TYPE));
34         return ident.getText();
35     }
36
37     public static boolean isLoggerType(final DetailAST ast) {
38         final String typeName = getTypeName(ast);
39         return typeName.equals(LOGGER_TYPE_FULL_NAME) || typeName.equals(LOGGER_TYPE_NAME);
40     }
41
42     public static String getVariableName(final DetailAST ast) {
43         DetailAST identifier = ast.findFirstToken(TokenTypes.IDENT);
44         return identifier.getText();
45     }
46
47     public static boolean isAFieldVariable(final DetailAST ast) {
48         return ast.getParent().getType() == TokenTypes.OBJBLOCK;
49     }
50
51     /**
52      * Returns the name the method (and the enclosing class) at a given point specified by the
53      * passed-in abstract syntax tree (AST).
54      *
55      * @param ast an abstract syntax tree (AST) pointing to method call
56      * @return the name of the method being called
57      */
58     public static String getMethodName(final DetailAST ast) {
59         if (ast.getFirstChild().getLastChild() != null) {
60             return ast.getFirstChild().getFirstChild().getText() + "." + ast.getFirstChild().getLastChild().getText();
61         }
62         return ast.getFirstChild().getText();
63     }
64
65     public static boolean isLogMethod(final String methodName) {
66         return LOG_METHODS.contains(methodName);
67     }
68
69     /**
70      * Returns the name of the closest enclosing class of the point by the passed-in abstract syntax
71      * tree (AST).
72      *
73      * @param ast an abstract syntax tree (AST)
74      * @return the name of the closest enclosign class
75      */
76     public static String getClassName(final DetailAST ast) {
77         DetailAST parent = ast.getParent();
78         while (parent.getType() != TokenTypes.CLASS_DEF && parent.getType() != TokenTypes.ENUM_DEF) {
79             parent = parent.getParent();
80         }
81         return parent.findFirstToken(TokenTypes.IDENT).getText();
82     }
83
84 }