001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.annotation;
021
022import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
026
027/**
028 * The check does verifying that annotations are located on the same line with their targets.
029 * Verifying with this check is not good practice, but it is using by some style guides.
030 */
031public class AnnotationOnSameLineCheck extends AbstractCheck {
032
033    /** A key is pointing to the warning message text in "messages.properties" file. */
034    public static final String MSG_KEY_ANNOTATION_ON_SAME_LINE = "annotation.same.line";
035
036    @Override
037    public int[] getDefaultTokens() {
038        return new int[] {
039            TokenTypes.CLASS_DEF,
040            TokenTypes.INTERFACE_DEF,
041            TokenTypes.ENUM_DEF,
042            TokenTypes.METHOD_DEF,
043            TokenTypes.CTOR_DEF,
044            TokenTypes.VARIABLE_DEF,
045        };
046    }
047
048    @Override
049    public int[] getAcceptableTokens() {
050        return new int[] {
051            TokenTypes.CLASS_DEF,
052            TokenTypes.INTERFACE_DEF,
053            TokenTypes.ENUM_DEF,
054            TokenTypes.METHOD_DEF,
055            TokenTypes.CTOR_DEF,
056            TokenTypes.VARIABLE_DEF,
057            TokenTypes.PARAMETER_DEF,
058            TokenTypes.ANNOTATION_DEF,
059            TokenTypes.TYPECAST,
060            TokenTypes.LITERAL_THROWS,
061            TokenTypes.IMPLEMENTS_CLAUSE,
062            TokenTypes.TYPE_ARGUMENT,
063            TokenTypes.LITERAL_NEW,
064            TokenTypes.DOT,
065            TokenTypes.ANNOTATION_FIELD_DEF,
066        };
067    }
068
069    @Override
070    public int[] getRequiredTokens() {
071        return CommonUtil.EMPTY_INT_ARRAY;
072    }
073
074    @Override
075    public void visitToken(DetailAST ast) {
076        DetailAST nodeWithAnnotations = ast;
077        if (ast.getType() == TokenTypes.TYPECAST) {
078            nodeWithAnnotations = ast.findFirstToken(TokenTypes.TYPE);
079        }
080        DetailAST modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.MODIFIERS);
081        if (modifiersNode == null) {
082            modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.ANNOTATIONS);
083        }
084        if (modifiersNode != null) {
085            for (DetailAST annotationNode = modifiersNode.getFirstChild();
086                    annotationNode != null;
087                    annotationNode = annotationNode.getNextSibling()) {
088                if (annotationNode.getType() == TokenTypes.ANNOTATION
089                        && annotationNode.getLineNo() != getNextNode(annotationNode).getLineNo()) {
090                    log(annotationNode.getLineNo(), MSG_KEY_ANNOTATION_ON_SAME_LINE,
091                          getAnnotationName(annotationNode));
092                }
093            }
094        }
095    }
096
097    /**
098     * Finds next node of ast tree.
099     * @param node current node
100     * @return node that is next to given
101     */
102    private static DetailAST getNextNode(DetailAST node) {
103        DetailAST nextNode = node.getNextSibling();
104        if (nextNode == null) {
105            nextNode = node.getParent().getNextSibling();
106        }
107        return nextNode;
108    }
109
110    /**
111     * Returns the name of the given annotation.
112     * @param annotation annotation node.
113     * @return annotation name.
114     */
115    private static String getAnnotationName(DetailAST annotation) {
116        DetailAST identNode = annotation.findFirstToken(TokenTypes.IDENT);
117        if (identNode == null) {
118            identNode = annotation.findFirstToken(TokenTypes.DOT).getLastChild();
119        }
120        return identNode.getText();
121    }
122
123}