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 java.util.List;
13 import org.slf4j.Logger;
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.utils.CheckUtils;
21 public final class CheckLoggingUtil {
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 =
27 Lists.newArrayList("LOG.debug", "LOG.info", "LOG.error", "LOG.warn", "LOG.trace");
29 private CheckLoggingUtil() {
30 throw new UnsupportedOperationException("Utility class should not be instantiated!");
33 public static String getTypeName(final DetailAST ast) {
34 final FullIdent ident = CheckUtils.createFullType(ast.findFirstToken(TokenTypes.TYPE));
35 return ident.getText();
38 public static boolean isLoggerType(final DetailAST ast) {
39 final String typeName = getTypeName(ast);
40 return typeName.equals(LOGGER_TYPE_FULL_NAME) || typeName.equals(LOGGER_TYPE_NAME);
43 public static String getVariableName(final DetailAST ast) {
44 DetailAST identifier = ast.findFirstToken(TokenTypes.IDENT);
45 return identifier.getText();
48 public static boolean isAFieldVariable(final DetailAST ast) {
49 return ast.getParent().getType() == TokenTypes.OBJBLOCK;
52 public static String getMethodName(final DetailAST ast) {
53 if (ast.getFirstChild().getLastChild() != null) {
54 return ast.getFirstChild().getFirstChild().getText() + "." + ast.getFirstChild().getLastChild().getText();
56 return ast.getFirstChild().getText();
59 public static boolean isLogMethod(final String methodName) {
60 return LOG_METHODS.contains(methodName);
63 public static String getClassName(final DetailAST ast) {
64 DetailAST parent = ast.getParent();
65 while (parent.getType() != TokenTypes.CLASS_DEF && parent.getType() != TokenTypes.ENUM_DEF) {
66 parent = parent.getParent();
68 return parent.findFirstToken(TokenTypes.IDENT).getText();