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.CommonUtils;
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 * @author zenigata
031 */
032public class AnnotationOnSameLineCheck extends AbstractCheck {
033
034    /** A key is pointing to the warning message text in "messages.properties" file. */
035    public static final String MSG_KEY_ANNOTATION_ON_SAME_LINE = "annotation.same.line";
036
037    @Override
038    public int[] getDefaultTokens() {
039        return new int[] {
040            TokenTypes.CLASS_DEF,
041            TokenTypes.INTERFACE_DEF,
042            TokenTypes.ENUM_DEF,
043            TokenTypes.METHOD_DEF,
044            TokenTypes.CTOR_DEF,
045            TokenTypes.VARIABLE_DEF,
046        };
047    }
048
049    @Override
050    public int[] getAcceptableTokens() {
051        return new int[] {
052            TokenTypes.CLASS_DEF,
053            TokenTypes.INTERFACE_DEF,
054            TokenTypes.ENUM_DEF,
055            TokenTypes.METHOD_DEF,
056            TokenTypes.CTOR_DEF,
057            TokenTypes.VARIABLE_DEF,
058            TokenTypes.PARAMETER_DEF,
059            TokenTypes.ANNOTATION_DEF,
060            TokenTypes.TYPECAST,
061            TokenTypes.LITERAL_THROWS,
062            TokenTypes.IMPLEMENTS_CLAUSE,
063            TokenTypes.TYPE_ARGUMENT,
064            TokenTypes.LITERAL_NEW,
065            TokenTypes.DOT,
066            TokenTypes.ANNOTATION_FIELD_DEF,
067        };
068    }
069
070    @Override
071    public int[] getRequiredTokens() {
072        return CommonUtils.EMPTY_INT_ARRAY;
073    }
074
075    @Override
076    public void visitToken(DetailAST ast) {
077        DetailAST nodeWithAnnotations = ast;
078        if (ast.getType() == TokenTypes.TYPECAST) {
079            nodeWithAnnotations = ast.findFirstToken(TokenTypes.TYPE);
080        }
081        DetailAST modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.MODIFIERS);
082        if (modifiersNode == null) {
083            modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.ANNOTATIONS);
084        }
085        if (modifiersNode != null) {
086            for (DetailAST annotationNode = modifiersNode.getFirstChild();
087                    annotationNode != null;
088                    annotationNode = annotationNode.getNextSibling()) {
089                if (annotationNode.getType() == TokenTypes.ANNOTATION
090                        && annotationNode.getLineNo() != getNextNode(annotationNode).getLineNo()) {
091                    log(annotationNode.getLineNo(), MSG_KEY_ANNOTATION_ON_SAME_LINE,
092                          getAnnotationName(annotationNode));
093                }
094            }
095        }
096    }
097
098    /**
099     * Finds next node of ast tree.
100     * @param node current node
101     * @return node that is next to given
102     */
103    private static DetailAST getNextNode(DetailAST node) {
104        DetailAST nextNode = node.getNextSibling();
105        if (nextNode == null) {
106            nextNode = node.getParent().getNextSibling();
107        }
108        return nextNode;
109    }
110
111    /**
112     * Returns the name of the given annotation.
113     * @param annotation annotation node.
114     * @return annotation name.
115     */
116    private static String getAnnotationName(DetailAST annotation) {
117        DetailAST identNode = annotation.findFirstToken(TokenTypes.IDENT);
118        if (identNode == null) {
119            identNode = annotation.findFirstToken(TokenTypes.DOT).getLastChild();
120        }
121        return identNode.getText();
122    }
123
124}