1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.indentation;
21
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24
25
26
27
28
29 public class MethodDefHandler extends BlockParentHandler {
30
31
32
33
34
35
36
37
38
39 public MethodDefHandler(IndentationCheck indentCheck,
40 DetailAST ast, AbstractExpressionHandler parent) {
41 super(indentCheck, getHandlerName(ast), ast, parent);
42 }
43
44 @Override
45 protected DetailAST getTopLevelAst() {
46
47 return null;
48 }
49
50 @Override
51 protected void checkModifiers() {
52 final DetailAST modifier = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
53 if (isOnStartOfLine(modifier)
54 && !getIndent().isAcceptable(expandedTabsColumnNo(modifier))) {
55 logError(modifier, "modifier", expandedTabsColumnNo(modifier));
56 }
57 }
58
59
60
61
62 private void checkThrows() {
63 final DetailAST throwsAst = getMainAst().findFirstToken(TokenTypes.LITERAL_THROWS);
64
65 if (throwsAst != null) {
66 checkWrappingIndentation(throwsAst, throwsAst.getNextSibling(), getIndentCheck()
67 .getThrowsIndent(), getLineStart(getMethodDefLineStart(getMainAst())),
68 !isOnStartOfLine(throwsAst));
69 }
70 }
71
72
73
74
75
76
77
78
79
80
81 private static int getMethodDefLineStart(DetailAST mainAst) {
82
83 int lineStart = mainAst.findFirstToken(TokenTypes.IDENT).getLineNo();
84
85
86 final DetailAST typeNode = mainAst.findFirstToken(TokenTypes.TYPE);
87 if (typeNode != null) {
88 lineStart = getFirstLine(typeNode);
89 }
90
91
92 for (DetailAST node = mainAst.findFirstToken(TokenTypes.MODIFIERS).getFirstChild();
93 node != null;
94 node = node.getNextSibling()) {
95
96 if (node.getType() == TokenTypes.ANNOTATION) {
97 continue;
98 }
99
100 if (node.getLineNo() < lineStart) {
101 lineStart = node.getLineNo();
102 }
103 }
104
105 return lineStart;
106 }
107
108 @Override
109 public void checkIndentation() {
110 checkModifiers();
111 checkThrows();
112
113 if (getMethodDefParamRightParen(getMainAst()) != null) {
114 checkWrappingIndentation(getMainAst(), getMethodDefParamRightParen(getMainAst()));
115 }
116
117 if (getLeftCurly() != null) {
118 super.checkIndentation();
119 }
120 }
121
122
123
124
125
126
127
128
129 private static DetailAST getMethodDefParamRightParen(DetailAST methodDefAst) {
130 return methodDefAst.findFirstToken(TokenTypes.RPAREN);
131 }
132
133
134
135
136
137
138
139 private static String getHandlerName(DetailAST ast) {
140 final String name;
141
142 switch (ast.getType()) {
143 case TokenTypes.CTOR_DEF:
144 name = "ctor def";
145 break;
146 case TokenTypes.ANNOTATION_FIELD_DEF:
147 name = "annotation field def";
148 break;
149 case TokenTypes.COMPACT_CTOR_DEF:
150 name = "compact ctor def";
151 break;
152 default:
153 name = "method def";
154 }
155
156 return name;
157 }
158
159 }