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 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
25
26
27
28
29 public class NewHandler extends AbstractExpressionHandler {
30
31
32 private final DetailAST mainAst;
33
34
35
36
37
38
39
40
41
42 public NewHandler(IndentationCheck indentCheck,
43 DetailAST ast,
44 AbstractExpressionHandler parent) {
45 super(indentCheck, "new", ast, parent);
46 mainAst = ast;
47 }
48
49 @Override
50 public void checkIndentation() {
51
52 if (isOnStartOfLine(mainAst)) {
53 final int columnNo = expandedTabsColumnNo(mainAst);
54 final IndentLevel level = getIndentImpl();
55
56 final boolean forceStrictCondition = getIndentCheck().isForceStrictCondition();
57 if (forceStrictCondition && !level.isAcceptable(columnNo)
58 || !forceStrictCondition && level.isGreaterThan(columnNo)) {
59 logError(mainAst, "", columnNo, level);
60 }
61 }
62
63 final DetailAST firstChild = mainAst.getFirstChild();
64 if (firstChild != null) {
65 checkExpressionSubtree(firstChild, getIndent(), false, false);
66 }
67
68 final DetailAST lparen = mainAst.findFirstToken(TokenTypes.LPAREN);
69 checkLeftParen(lparen);
70 }
71
72 @Override
73 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
74 final int offset;
75 if (TokenUtil.isOfType(child.getMainAst(), TokenTypes.OBJBLOCK)) {
76 offset = getBasicOffset();
77 }
78 else {
79 offset = getLineWrappingIndent();
80 }
81 return new IndentLevel(getIndent(), offset);
82 }
83
84 @Override
85 protected IndentLevel getIndentImpl() {
86 IndentLevel result;
87
88
89 if (getLineStart(mainAst) == mainAst.getColumnNo()) {
90 result = super.getIndentImpl();
91
92 final boolean isLineWrappedNew = TokenUtil.isOfType(mainAst.getParent().getParent(),
93 TokenTypes.ASSIGN, TokenTypes.LITERAL_RETURN);
94
95 if (isLineWrappedNew || doesChainedMethodNeedsLineWrapping()) {
96 result = new IndentLevel(result, getLineWrappingIndent());
97 }
98 }
99 else {
100 result = new IndentLevel(getLineStart(mainAst));
101 }
102
103 return result;
104 }
105
106
107
108
109
110
111
112 private int getLineWrappingIndent() {
113 return getIndentCheck().getLineWrappingIndentation();
114 }
115
116 @Override
117 protected boolean shouldIncreaseIndent() {
118 return false;
119 }
120
121
122
123
124
125
126
127
128 private boolean doesChainedMethodNeedsLineWrapping() {
129 DetailAST ast = mainAst.getParent();
130
131 while (TokenUtil.isOfType(ast, TokenTypes.DOT, TokenTypes.METHOD_CALL, TokenTypes.EXPR)) {
132 ast = ast.getParent();
133 }
134
135 return TokenUtil.isOfType(ast, TokenTypes.ASSIGN, TokenTypes.LITERAL_RETURN);
136 }
137
138 }