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.checks.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 = Lists.newArrayList("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 aAST) {
33 final FullIdent ident = CheckUtils.createFullType(aAST.findFirstToken(TokenTypes.TYPE));
34 return ident.getText();
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);
42 public static String getVariableName(final DetailAST aAST) {
43 DetailAST identifier = aAST.findFirstToken(TokenTypes.IDENT);
44 return identifier.getText();
47 public static boolean isAFieldVariable(final DetailAST aAST) {
48 return aAST.getParent().getType() == TokenTypes.OBJBLOCK;
51 public static String getMethodName(final DetailAST aAST) {
52 if(aAST.getFirstChild().getLastChild() != null) {
53 return aAST.getFirstChild().getFirstChild().getText() + "." + aAST.getFirstChild().getLastChild().getText();
55 return aAST.getFirstChild().getText();
58 public static boolean isLogMethod(final String methodName) {
59 return LOG_METHODS.contains(methodName);
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();
67 return parent.findFirstToken(TokenTypes.IDENT).getText();