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 result = getIndent();
68 }
69 else {
70 result = super.getSuggestedChildIndent(child);
71 }
72 return result;
73 }
74
75 @Override
76 public void checkIndentation() {
77 super.checkIndentation();
78 if (getMainAst().getFirstChild().getType() == TokenTypes.RESOURCE_SPECIFICATION) {
79 checkTryResParen(getTryResLparen(), "lparen");
80 checkTryResParen(getTryResRparen(), "rparen");
81 checkTryResources(getMainAst().getFirstChild());
82 }
83 }
84
85
86
87
88
89
90
91
92
93 private void checkTryResParen(final DetailAST parenAst,
94 final String subType) {
95 if (isOnStartOfLine(parenAst)) {
96 final IndentLevel expectedIdent = new IndentLevel(getIndent(), 0,
97 getIndentCheck().getLineWrappingIndentation());
98
99 checkChildIndentation(parenAst, subType, expectedIdent);
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 private void checkChildIndentation(DetailAST ast, String subType, IndentLevel expectedIdent) {
113 if (getIndentCheck().isForceStrictCondition()) {
114 if (!expectedIdent.isAcceptable(expandedTabsColumnNo(ast))) {
115 logError(ast, subType, expandedTabsColumnNo(ast), expectedIdent);
116 }
117 }
118 else {
119 if (expandedTabsColumnNo(ast) < expectedIdent.getFirstIndentLevel()) {
120 logError(ast, subType, expandedTabsColumnNo(ast), expectedIdent);
121 }
122 }
123 }
124
125
126
127
128
129
130 private void checkTryResources(final DetailAST resourcesSpecAst) {
131 final DetailAST resourcesAst = resourcesSpecAst.findFirstToken(TokenTypes.RESOURCES);
132 final int indentation = getIndent().getFirstIndentLevel()
133 + getIndentCheck().getLineWrappingIndentation();
134 final IndentLevel expectedResourceIndent = new IndentLevel(indentation);
135
136 final String subType = "resource";
137
138 DetailAST resourceAst = resourcesAst.getFirstChild();
139 while (resourceAst != null) {
140 if (resourceAst.getType() == TokenTypes.RESOURCE) {
141 final DetailAST nextSibling;
142 if (resourceAst.getNextSibling() == null) {
143 nextSibling = getTryResRparen();
144 }
145 else {
146 nextSibling = resourceAst.getNextSibling();
147 }
148 if (isOnStartOfLine(resourceAst)) {
149 checkChildIndentation(resourceAst, subType, expectedResourceIndent);
150 checkWrappingIndentation(
151 resourceAst,
152 nextSibling,
153 getIndentCheck().getLineWrappingIndentation(),
154 expectedResourceIndent.getFirstIndentLevel(),
155 true);
156 }
157 else {
158 checkWrappingIndentation(resourceAst, nextSibling);
159 }
160 }
161 resourceAst = resourceAst.getNextSibling();
162 }
163 }
164
165 }