001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 /** 044 * Creates a new {@code AnnotationOnSameLineCheck} instance. 045 */ 046 public AnnotationOnSameLineCheck() { 047 // no code by default 048 } 049 050 @Override 051 public int[] getDefaultTokens() { 052 return new int[] { 053 TokenTypes.CLASS_DEF, 054 TokenTypes.INTERFACE_DEF, 055 TokenTypes.ENUM_DEF, 056 TokenTypes.METHOD_DEF, 057 TokenTypes.CTOR_DEF, 058 TokenTypes.VARIABLE_DEF, 059 TokenTypes.RECORD_DEF, 060 TokenTypes.COMPACT_CTOR_DEF, 061 }; 062 } 063 064 @Override 065 public int[] getAcceptableTokens() { 066 return new int[] { 067 TokenTypes.CLASS_DEF, 068 TokenTypes.INTERFACE_DEF, 069 TokenTypes.ENUM_DEF, 070 TokenTypes.METHOD_DEF, 071 TokenTypes.CTOR_DEF, 072 TokenTypes.VARIABLE_DEF, 073 TokenTypes.PARAMETER_DEF, 074 TokenTypes.ANNOTATION_DEF, 075 TokenTypes.TYPECAST, 076 TokenTypes.LITERAL_THROWS, 077 TokenTypes.IMPLEMENTS_CLAUSE, 078 TokenTypes.TYPE_ARGUMENT, 079 TokenTypes.LITERAL_NEW, 080 TokenTypes.DOT, 081 TokenTypes.ANNOTATION_FIELD_DEF, 082 TokenTypes.RECORD_DEF, 083 TokenTypes.COMPACT_CTOR_DEF, 084 }; 085 } 086 087 @Override 088 public int[] getRequiredTokens() { 089 return CommonUtil.EMPTY_INT_ARRAY; 090 } 091 092 @Override 093 public void visitToken(DetailAST ast) { 094 DetailAST nodeWithAnnotations = ast; 095 if (ast.getType() == TokenTypes.TYPECAST) { 096 nodeWithAnnotations = ast.findFirstToken(TokenTypes.TYPE); 097 } 098 DetailAST modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.MODIFIERS); 099 if (modifiersNode == null) { 100 modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.ANNOTATIONS); 101 } 102 if (modifiersNode != null) { 103 for (DetailAST annotationNode = modifiersNode.getFirstChild(); 104 annotationNode != null; 105 annotationNode = annotationNode.getNextSibling()) { 106 if (annotationNode.getType() == TokenTypes.ANNOTATION 107 && !TokenUtil.areOnSameLine(annotationNode, 108 modifiersNode.getNextSibling())) { 109 log(annotationNode, MSG_KEY_ANNOTATION_ON_SAME_LINE, 110 getAnnotationName(annotationNode)); 111 } 112 } 113 } 114 } 115 116 /** 117 * Returns the name of the given annotation. 118 * 119 * @param annotation annotation node. 120 * @return annotation name. 121 */ 122 private static String getAnnotationName(DetailAST annotation) { 123 DetailAST identNode = annotation.findFirstToken(TokenTypes.IDENT); 124 if (identNode == null) { 125 identNode = annotation.findFirstToken(TokenTypes.DOT).getLastChild(); 126 } 127 return identNode.getText(); 128 } 129 130}