forked from thoriumrobot/VGRTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleanFlagRefactoring.java
More file actions
182 lines (162 loc) · 6.11 KB
/
BooleanFlagRefactoring.java
File metadata and controls
182 lines (162 loc) · 6.11 KB
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
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.ConditionalExpression;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.NullLiteral;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.dom.InfixExpression.Operator;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
/**
* This class represents a refactoring in which boolean flags are replaced with
* explicit null checks
*/
public class BooleanFlagRefactoring extends Refactoring {
public static final String NAME = "BooleanFlagRefactoring";
/**
* List of variable names identified as boolean flags, along with their
* corresponding initializer expression
*/
private final Map<IBinding, Expression> flagExpressions;
/** Default constructor (for RefactoringEngine integration) */
public BooleanFlagRefactoring() {
super();
this.flagExpressions = new HashMap<>();
}
@Override
public boolean isApplicable(ASTNode node) {
if (node instanceof VariableDeclarationStatement stmt) {
return isApplicable(stmt);
} else if (node instanceof IfStatement ifStmt) {
return isApplicable(ifStmt);
} else if (node instanceof Assignment assignment) {
checkReassignment(assignment);
}
return false;
}
/**
* Checks to see if a VariableDeclarationStatement defines a boolean flag that
* represents another variable's nullness
*/
private boolean isApplicable(VariableDeclarationStatement stmt) {
boolean isBooleanDeclaration = (stmt.getType() instanceof PrimitiveType pType
&& pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN);
if (!isBooleanDeclaration) {
return false;
}
boolean flagFound = false;
AST ast = stmt.getAST();
// Search through all declared variables in declaration node for a booleanflag
// Eclipse JDT API guarantees fragments() returns a live
// List<VariableDeclarationFragment>
// See
// https://help.eclipse.org/latest/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/VariableDeclarationStatement.html#fragments()
@SuppressWarnings("unchecked")
List<VariableDeclarationFragment> fragments = (List<VariableDeclarationFragment>) stmt.fragments();
for (VariableDeclarationFragment frag : fragments) {
Expression varInitializer = frag.getInitializer();
if (varInitializer == null) {
continue;
}
for (Expression expression : Refactoring.getSubExpressions(varInitializer)) {
if (expression instanceof ConditionalExpression cExpr) {
expression = cExpr.getExpression();
}
if (expression instanceof InfixExpression infix && isEqualityOperator(infix.getOperator())
&& getNullComparisonVariable(infix) != null) {
ParenthesizedExpression copiedExpression = ast.newParenthesizedExpression();
copiedExpression.setExpression((Expression) ASTNode.copySubtree(ast, varInitializer));
flagExpressions.put(frag.getName().resolveBinding(), copiedExpression);
flagFound = true;
}
}
}
return flagFound;
}
/**
* Analyzes an IfStatement to see if it contains a check utilizing an identified
* boolean flag
*/
private boolean isApplicable(IfStatement ifStmt) {
List<Expression> exprFragments = Refactoring.getSubExpressions(ifStmt.getExpression());
for (Expression expr : exprFragments) {
if (expr instanceof InfixExpression infix && isEqualityOperator(infix.getOperator())) {
Expression leftOperand = infix.getLeftOperand();
Expression rightOperand = infix.getRightOperand();
if ((leftOperand instanceof SimpleName lhs && isFlag(lhs))
|| (rightOperand instanceof SimpleName rhs && isFlag(rhs))) {
return true;
}
}
if (expr instanceof SimpleName varName && isFlag(varName)) {
return true;
}
}
return false;
}
private boolean isFlag(SimpleName potentialFlag) {
return flagExpressions.get(potentialFlag.resolveBinding()) != null;
}
private boolean isEqualityOperator(Operator op) {
return (op == Operator.NOT_EQUALS || op == Operator.EQUALS);
}
private @Nullable SimpleName getNullComparisonVariable(InfixExpression infix) {
Expression leftOperand = infix.getLeftOperand();
Expression rightOperand = infix.getRightOperand();
if (leftOperand instanceof SimpleName varName && rightOperand instanceof NullLiteral) {
return varName;
} else if (rightOperand instanceof SimpleName varName && leftOperand instanceof NullLiteral) {
return varName;
}
return null;
}
@Override
public void apply(ASTNode node, ASTRewrite rewriter) {
if (!(node instanceof IfStatement ifStmt)) {
return;
}
List<Expression> exprFragments = Refactoring.getSubExpressions(ifStmt.getExpression());
for (Expression expression : exprFragments) {
if (expression instanceof InfixExpression infix && isEqualityOperator(infix.getOperator())) {
SimpleName flagName = getNullComparisonVariable(infix);
apply(rewriter, flagName);
}
if (expression instanceof SimpleName flagName) {
apply(rewriter, flagName);
}
}
}
private void apply(ASTRewrite rewriter, @Nullable SimpleName flagName) {
if (flagName == null || !isFlag(flagName)) {
return;
}
Expression newExpr = flagExpressions.get(flagName.resolveBinding());
if (newExpr != null) {
rewriter.replace(flagName, newExpr, null);
}
}
/*
* Checks Assignment node to see if it re-assigns an existing boolean flag, and
* if so removes the flag from flagExpressions
*/
private void checkReassignment(Assignment assignmentNode) {
Expression lhs = assignmentNode.getLeftHandSide();
if (!(lhs instanceof SimpleName varName)) {
return;
}
if (isFlag(varName)) {
flagExpressions.remove(varName.resolveBinding());
}
}
}