-
Notifications
You must be signed in to change notification settings - Fork 37
/
ObsoleteIncrementStatementRatherThanIncrementExpressionCleanUp.java
257 lines (219 loc) · 9.75 KB
/
ObsoleteIncrementStatementRatherThanIncrementExpressionCleanUp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* AutoRefactor - Eclipse plugin to automatically refactor Java code bases.
*
* Copyright (C) 2020 Fabrice Tiercelin - initial API and implementation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program under LICENSE-GNUGPL. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution under LICENSE-ECLIPSE, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.autorefactor.jdt.internal.ui.fix;
import org.autorefactor.jdt.core.dom.ASTRewrite;
import org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory;
import org.autorefactor.jdt.internal.corext.dom.ASTNodes;
import org.autorefactor.jdt.internal.corext.dom.BlockSubVisitor;
import org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ConditionalExpression;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.PostfixExpression;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.text.edits.TextEditGroup;
/** See {@link #getDescription()} method. */
public class ObsoleteIncrementStatementRatherThanIncrementExpressionCleanUp extends AbstractCleanUpRule {
@Override
public String getName() {
return MultiFixMessages.ObsoleteIncrementStatementRatherThanIncrementExpressionCleanUp_name;
}
@Override
public String getDescription() {
return MultiFixMessages.ObsoleteIncrementStatementRatherThanIncrementExpressionCleanUp_description;
}
@Override
public String getReason() {
return MultiFixMessages.ObsoleteIncrementStatementRatherThanIncrementExpressionCleanUp_reason;
}
@Override
public boolean visit(final Block visited) {
ExpressionVisitor expressionVisitor= new ExpressionVisitor();
expressionVisitor.visitNode(visited);
return expressionVisitor.result;
}
private final class ExpressionVisitor extends BlockSubVisitor {
@Override
public boolean visit(final PrefixExpression visited) {
if (ASTNodes.hasOperator(visited, PrefixExpression.Operator.INCREMENT, PrefixExpression.Operator.DECREMENT)) {
return visitExpression(visited, visited.getOperand());
}
return true;
}
@Override
public boolean visit(final PostfixExpression visited) {
if (ASTNodes.hasOperator(visited, PostfixExpression.Operator.INCREMENT, PostfixExpression.Operator.DECREMENT)) {
return visitExpression(visited, visited.getOperand());
}
return true;
}
public boolean visitExpression(final Expression visited, final Expression variable) {
SimpleName variableName= ASTNodes.as(variable, SimpleName.class);
if (result
&& !(visited.getParent() instanceof ExpressionStatement)
&& variableName != null
&& variableName.resolveBinding() != null
&& variableName.resolveBinding().getKind() == IBinding.VARIABLE
&& ASTNodes.isLocalVariable(variableName.resolveBinding())) {
return visitParent(visited, variable, visited);
}
return true;
}
public boolean visitParent(final Expression visited, final Expression variable, final ASTNode parent) {
ASTNode ancestor= parent.getParent();
if (ancestor != null) {
switch (ancestor.getNodeType()) {
case ASTNode.IF_STATEMENT:
IfStatement statement= (IfStatement) ancestor;
if (visited instanceof PrefixExpression
&& parent.getLocationInParent() == IfStatement.EXPRESSION_PROPERTY
&& !ASTNodes.isInElse(statement)) {
return maybeExtractIncrement(visited, variable, statement);
}
return true;
case ASTNode.LABELED_STATEMENT:
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
case ASTNode.EXPRESSION_STATEMENT:
return maybeExtractIncrement(visited, variable, (Statement) ancestor);
case ASTNode.THROW_STATEMENT:
case ASTNode.RETURN_STATEMENT:
if (visited instanceof PrefixExpression) {
return maybeExtractIncrement(visited, variable, (Statement) ancestor);
}
return true;
case ASTNode.CONSTRUCTOR_INVOCATION:
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
if (visited instanceof PostfixExpression) {
return maybeExtractIncrement(visited, variable, (Statement) ancestor);
}
return true;
case ASTNode.QUALIFIED_NAME:
case ASTNode.SIMPLE_NAME:
case ASTNode.FIELD_ACCESS:
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
case ASTNode.ASSIGNMENT:
case ASTNode.INSTANCEOF_EXPRESSION:
case ASTNode.CLASS_INSTANCE_CREATION:
case ASTNode.METHOD_INVOCATION:
case ASTNode.SUPER_METHOD_INVOCATION:
case ASTNode.CAST_EXPRESSION:
case ASTNode.PARENTHESIZED_EXPRESSION:
case ASTNode.POSTFIX_EXPRESSION:
case ASTNode.PREFIX_EXPRESSION:
case ASTNode.ARRAY_ACCESS:
case ASTNode.ARRAY_CREATION:
case ASTNode.ARRAY_INITIALIZER:
return visitParent(visited, variable, ancestor);
case ASTNode.INFIX_EXPRESSION:
if (parent.getLocationInParent() == InfixExpression.LEFT_OPERAND_PROPERTY
|| ASTNodes.hasOperator((InfixExpression) ancestor,
InfixExpression.Operator.AND,
InfixExpression.Operator.DIVIDE,
InfixExpression.Operator.EQUALS,
InfixExpression.Operator.GREATER,
InfixExpression.Operator.GREATER_EQUALS,
InfixExpression.Operator.LEFT_SHIFT,
InfixExpression.Operator.LESS,
InfixExpression.Operator.LESS_EQUALS,
InfixExpression.Operator.MINUS,
InfixExpression.Operator.NOT_EQUALS,
InfixExpression.Operator.OR,
InfixExpression.Operator.PLUS,
InfixExpression.Operator.REMAINDER,
InfixExpression.Operator.RIGHT_SHIFT_SIGNED,
InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED,
InfixExpression.Operator.TIMES,
InfixExpression.Operator.XOR)) {
return visitParent(visited, variable, ancestor);
}
return true;
case ASTNode.CONDITIONAL_EXPRESSION:
if (parent.getLocationInParent() == ConditionalExpression.EXPRESSION_PROPERTY) {
return visitParent(visited, variable, ancestor);
}
return true;
default:
}
}
return true;
}
private boolean maybeExtractIncrement(final Expression visited, final Expression variable, final Statement statement) {
SimpleName variableName= ASTNodes.as(variable, SimpleName.class);
VarDefinitionsUsesVisitor varDefinitionsUsesVisitor= new VarDefinitionsUsesVisitor((IVariableBinding) variableName.resolveBinding(), statement, true);
if (varDefinitionsUsesVisitor.getWrites().isEmpty()
&& varDefinitionsUsesVisitor.getReads().size() == 1
&& (visited instanceof PrefixExpression || !ASTNodes.fallsThrough(statement))) {
extractIncrement(visited, variable, statement);
result= false;
return false;
}
return true;
}
private void extractIncrement(final Expression visited, final Expression variable,
final Statement statement) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
ASTNodeFactory ast= cuRewrite.getASTBuilder();
TextEditGroup group= new TextEditGroup(MultiFixMessages.ObsoleteIncrementStatementRatherThanIncrementExpressionCleanUp_description);
ASTNodes.replaceButKeepComment(rewrite, ASTNodes.getHighestCompatibleNode(visited, ParenthesizedExpression.class), ast.createCopyTarget(variable), group);
if (visited instanceof PostfixExpression) {
Statement newAssignment= ast.newExpressionStatement(ASTNodes.createMoveTarget(rewrite, visited));
if (ASTNodes.canHaveSiblings(statement)) {
rewrite.insertAfter(newAssignment, statement, group);
} else {
Block newBlock= ast.newBlock();
newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, statement));
newBlock.statements().add(newAssignment);
ASTNodes.replaceButKeepComment(rewrite, statement, newBlock, group);
}
} else {
Statement newAssignment;
if (ASTNodes.hasOperator((PrefixExpression) visited, PrefixExpression.Operator.INCREMENT)) {
newAssignment= ast.newExpressionStatement(ast.newPostfixExpression(ASTNodes.createMoveTarget(rewrite, variable), PostfixExpression.Operator.INCREMENT));
} else {
newAssignment= ast.newExpressionStatement(ast.newPostfixExpression(ASTNodes.createMoveTarget(rewrite, variable), PostfixExpression.Operator.DECREMENT));
}
if (ASTNodes.canHaveSiblings(statement)) {
rewrite.insertBefore(newAssignment, statement, group);
} else {
Block newBlock= ast.newBlock();
newBlock.statements().add(newAssignment);
newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, statement));
ASTNodes.replaceButKeepComment(rewrite, statement, newBlock, group);
}
}
}
}
}