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 TryHandler extends BlockParentHandler {
30
31
32
33
34
35
36
37
38
39 public TryHandler(IndentationCheck indentCheck,
40 DetailAST ast, AbstractExpressionHandler parent) {
41 super(indentCheck, "try", ast, parent);
42 }
43
44
45
46
47
48
49 private DetailAST getTryResLparen() {
50 return getMainAst().getFirstChild().getFirstChild();
51 }
52
53
54
55
56
57
58 private DetailAST getTryResRparen() {
59 return getMainAst().getFirstChild().getLastChild();
60 }
61
62 @Override
63 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
64 final IndentLevel result;
65 if (child instanceof CatchHandler
66 || child instanceof FinallyHandler
67 || child instanceof NewHandler
68 && isTryBlocksResourceSpecification(child)) {
69 result = getIndent();
70 }
71 else {
72 result = super.getSuggestedChildIndent(child);
73 }
74 return result;
75 }
76
77 @Override
78 public void checkIndentation() {
79 super.checkIndentation();
80 if (getMainAst().getFirstChild().getType() == TokenTypes.RESOURCE_SPECIFICATION) {
81 checkTryResParen(getTryResLparen(), "lparen");
82 checkTryResParen(getTryResRparen(), "rparen");
83 checkTryResources(getMainAst().getFirstChild());
84 }
85 }
86
87
88
89
90
91
92
93
94
95 private void checkTryResParen(final DetailAST parenAst,
96 final String subType) {
97 if (isOnStartOfLine(parenAst)) {
98 final IndentLevel expectedIdent = new IndentLevel(getIndent(), 0,
99 getIndentCheck().getLineWrappingIndentation());
100
101 checkChildIndentation(parenAst, subType, expectedIdent);
102 }
103 }
104
105
106
107
108
109
110
111
112
113
114 private void checkChildIndentation(DetailAST ast, String subType, IndentLevel expectedIdent) {
115 if (getIndentCheck().isForceStrictCondition()) {
116 if (!expectedIdent.isAcceptable(expandedTabsColumnNo(ast))) {
117 logError(ast, subType, expandedTabsColumnNo(ast), expectedIdent);
118 }
119 }
120 else {
121 if (expandedTabsColumnNo(ast) < expectedIdent.getFirstIndentLevel()) {
122 logError(ast, subType, expandedTabsColumnNo(ast), expectedIdent);
123 }
124 }
125 }
126
127
128
129
130
131
132 private void checkTryResources(final DetailAST resourcesSpecAst) {
133 final DetailAST resourcesAst = resourcesSpecAst.findFirstToken(TokenTypes.RESOURCES);
134 final int indentation = getIndent().getFirstIndentLevel()
135 + getIndentCheck().getLineWrappingIndentation();
136 final IndentLevel expectedResourceIndent = new IndentLevel(indentation);
137
138 final String subType = "resource";
139
140 DetailAST resourceAst = resourcesAst.getFirstChild();
141 while (resourceAst != null) {
142 if (resourceAst.getType() == TokenTypes.RESOURCE) {
143 final DetailAST nextSibling;
144 if (resourceAst.getNextSibling() == null) {
145 nextSibling = getTryResRparen();
146 }
147 else {
148 nextSibling = resourceAst.getNextSibling();
149 }
150 if (isOnStartOfLine(resourceAst)) {
151 checkChildIndentation(resourceAst, subType, expectedResourceIndent);
152 checkWrappingIndentation(
153 resourceAst,
154 nextSibling,
155 getIndentCheck().getLineWrappingIndentation(),
156 expectedResourceIndent.getFirstIndentLevel(),
157 true);
158 }
159 else {
160 checkWrappingIndentation(resourceAst, nextSibling);
161 }
162 }
163 resourceAst = resourceAst.getNextSibling();
164 }
165 }
166
167
168
169
170
171
172
173
174
175 private static boolean isTryBlocksResourceSpecification(AbstractExpressionHandler expression) {
176 boolean isResourceSpecificationExpression = false;
177
178 DetailAST ast = expression.getMainAst();
179
180 while (ast.getType() != TokenTypes.LITERAL_TRY) {
181 if (ast.getType() == TokenTypes.RESOURCE_SPECIFICATION) {
182 isResourceSpecificationExpression = true;
183 break;
184 }
185
186 ast = ast.getParent();
187 }
188
189 return isResourceSpecificationExpression;
190 }
191
192 }