001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
027import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
028
029/**
030 * <div>
031 * Checks that annotations are located on the same line with their targets.
032 * Verifying with this check is not good practice, but it is using by some style guides.
033 * </div>
034 *
035 * @since 8.2
036 */
037@StatelessCheck
038public class AnnotationOnSameLineCheck extends AbstractCheck {
039
040    /** A key is pointing to the warning message text in "messages.properties" file. */
041    public static final String MSG_KEY_ANNOTATION_ON_SAME_LINE = "annotation.same.line";
042
043    @Override
044    public int[] getDefaultTokens() {
045        return new int[] {
046            TokenTypes.CLASS_DEF,
047            TokenTypes.INTERFACE_DEF,
048            TokenTypes.ENUM_DEF,
049            TokenTypes.METHOD_DEF,
050            TokenTypes.CTOR_DEF,
051            TokenTypes.VARIABLE_DEF,
052            TokenTypes.RECORD_DEF,
053            TokenTypes.COMPACT_CTOR_DEF,
054        };
055    }
056
057    @Override
058    public int[] getAcceptableTokens() {
059        return new int[] {
060            TokenTypes.CLASS_DEF,
061            TokenTypes.INTERFACE_DEF,
062            TokenTypes.ENUM_DEF,
063            TokenTypes.METHOD_DEF,
064            TokenTypes.CTOR_DEF,
065            TokenTypes.VARIABLE_DEF,
066            TokenTypes.PARAMETER_DEF,
067            TokenTypes.ANNOTATION_DEF,
068            TokenTypes.TYPECAST,
069            TokenTypes.LITERAL_THROWS,
070            TokenTypes.IMPLEMENTS_CLAUSE,
071            TokenTypes.TYPE_ARGUMENT,
072            TokenTypes.LITERAL_NEW,
073            TokenTypes.DOT,
074            TokenTypes.ANNOTATION_FIELD_DEF,
075            TokenTypes.RECORD_DEF,
076            TokenTypes.COMPACT_CTOR_DEF,
077        };
078    }
079
080    @Override
081    public int[] getRequiredTokens() {
082        return CommonUtil.EMPTY_INT_ARRAY;
083    }
084
085    @Override
086    public void visitToken(DetailAST ast) {
087        DetailAST nodeWithAnnotations = ast;
088        if (ast.getType() == TokenTypes.TYPECAST) {
089            nodeWithAnnotations = ast.findFirstToken(TokenTypes.TYPE);
090        }
091        DetailAST modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.MODIFIERS);
092        if (modifiersNode == null) {
093            modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.ANNOTATIONS);
094        }
095        if (modifiersNode != null) {
096            for (DetailAST annotationNode = modifiersNode.getFirstChild();
097                    annotationNode != null;
098                    annotationNode = annotationNode.getNextSibling()) {
099                if (annotationNode.getType() == TokenTypes.ANNOTATION
100                        && !TokenUtil.areOnSameLine(annotationNode,
101                        modifiersNode.getNextSibling())) {
102                    log(annotationNode, MSG_KEY_ANNOTATION_ON_SAME_LINE,
103                          getAnnotationName(annotationNode));
104                }
105            }
106        }
107    }
108
109    /**
110     * Returns the name of the given annotation.
111     *
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}