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(lineStart, 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 checkWrappingIndentation(getMainAst(), getMethodDefParamRightParen(getMainAst()));
114
115 if (getLeftCurly() != null) {
116 super.checkIndentation();
117 }
118 }
119
120
121
122
123
124
125
126 private static DetailAST getMethodDefParamRightParen(DetailAST methodDefAst) {
127 return methodDefAst.findFirstToken(TokenTypes.RPAREN);
128 }
129
130
131
132
133
134
135
136 private static String getHandlerName(DetailAST ast) {
137 final String name;
138
139 if (ast.getType() == TokenTypes.CTOR_DEF) {
140 name = "ctor def";
141 }
142 else if (ast.getType() == TokenTypes.ANNOTATION_FIELD_DEF) {
143 name = "annotation field def";
144 }
145 else {
146 name = "method def";
147 }
148 return name;
149 }
150
151 }