2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.checkstyle;
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.CheckUtil;
17 import org.slf4j.Logger;
19 public final class CheckLoggingUtil {
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";
25 private static final Set<String> LOG_METHODS = ImmutableSet.of(
26 "LOG.debug", "LOG.info", "LOG.error", "LOG.warn", "LOG.trace");
28 private CheckLoggingUtil() {
29 throw new UnsupportedOperationException("Utility class should not be instantiated!");
32 public static String getTypeName(final DetailAST ast) {
33 final FullIdent ident = CheckUtil.createFullType(ast.findFirstToken(TokenTypes.TYPE));
34 return ident.getText();
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);
42 public static String getVariableName(final DetailAST ast) {
43 DetailAST identifier = ast.findFirstToken(TokenTypes.IDENT);
44 return identifier.getText();
47 public static boolean isAFieldVariable(final DetailAST ast) {
48 return ast.getParent().getType() == TokenTypes.OBJBLOCK;
52 * Returns the name the method (and the enclosing class) at a given point specified by the
53 * passed-in abstract syntax tree (AST).
55 * @param ast an abstract syntax tree (AST) pointing to method call
56 * @return the name of the method being called
58 public static String getMethodName(final DetailAST ast) {
59 if (ast.getFirstChild().getLastChild() != null) {
60 return ast.getFirstChild().getFirstChild().getText() + "." + ast.getFirstChild().getLastChild().getText();
62 return ast.getFirstChild().getText();
65 public static boolean isLogMethod(final String methodName) {
66 return LOG_METHODS.contains(methodName);
70 * Returns the name of the closest enclosing class of the point by the passed-in abstract syntax
73 * @param ast an abstract syntax tree (AST)
74 * @return the name of the closest enclosign class
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();
81 return parent.findFirstToken(TokenTypes.IDENT).getText();