001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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; 027 028/** 029 * The check does verifying that annotations are located on the same line with their targets. 030 * Verifying with this check is not good practice, but it is using by some style guides. 031 */ 032@StatelessCheck 033public class AnnotationOnSameLineCheck extends AbstractCheck { 034 035 /** A key is pointing to the warning message text in "messages.properties" file. */ 036 public static final String MSG_KEY_ANNOTATION_ON_SAME_LINE = "annotation.same.line"; 037 038 @Override 039 public int[] getDefaultTokens() { 040 return new int[] { 041 TokenTypes.CLASS_DEF, 042 TokenTypes.INTERFACE_DEF, 043 TokenTypes.ENUM_DEF, 044 TokenTypes.METHOD_DEF, 045 TokenTypes.CTOR_DEF, 046 TokenTypes.VARIABLE_DEF, 047 }; 048 } 049 050 @Override 051 public int[] getAcceptableTokens() { 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.PARAMETER_DEF, 060 TokenTypes.ANNOTATION_DEF, 061 TokenTypes.TYPECAST, 062 TokenTypes.LITERAL_THROWS, 063 TokenTypes.IMPLEMENTS_CLAUSE, 064 TokenTypes.TYPE_ARGUMENT, 065 TokenTypes.LITERAL_NEW, 066 TokenTypes.DOT, 067 TokenTypes.ANNOTATION_FIELD_DEF, 068 }; 069 } 070 071 @Override 072 public int[] getRequiredTokens() { 073 return CommonUtil.EMPTY_INT_ARRAY; 074 } 075 076 @Override 077 public void visitToken(DetailAST ast) { 078 DetailAST nodeWithAnnotations = ast; 079 if (ast.getType() == TokenTypes.TYPECAST) { 080 nodeWithAnnotations = ast.findFirstToken(TokenTypes.TYPE); 081 } 082 DetailAST modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.MODIFIERS); 083 if (modifiersNode == null) { 084 modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.ANNOTATIONS); 085 } 086 if (modifiersNode != null) { 087 for (DetailAST annotationNode = modifiersNode.getFirstChild(); 088 annotationNode != null; 089 annotationNode = annotationNode.getNextSibling()) { 090 if (annotationNode.getType() == TokenTypes.ANNOTATION 091 && annotationNode.getLineNo() != getNextNode(annotationNode).getLineNo()) { 092 log(annotationNode.getLineNo(), MSG_KEY_ANNOTATION_ON_SAME_LINE, 093 getAnnotationName(annotationNode)); 094 } 095 } 096 } 097 } 098 099 /** 100 * Finds next node of ast tree. 101 * @param node current node 102 * @return node that is next to given 103 */ 104 private static DetailAST getNextNode(DetailAST node) { 105 DetailAST nextNode = node.getNextSibling(); 106 if (nextNode == null) { 107 nextNode = node.getParent().getNextSibling(); 108 } 109 return nextNode; 110 } 111 112 /** 113 * Returns the name of the given annotation. 114 * @param annotation annotation node. 115 * @return annotation name. 116 */ 117 private static String getAnnotationName(DetailAST annotation) { 118 DetailAST identNode = annotation.findFirstToken(TokenTypes.IDENT); 119 if (identNode == null) { 120 identNode = annotation.findFirstToken(TokenTypes.DOT).getLastChild(); 121 } 122 return identNode.getText(); 123 } 124 125}