Skip to content

Commit a983a33

Browse files
author
Fabrice-TIERCELIN
committed
Prepare future changes
1 parent 62c576f commit a983a33

1 file changed

Lines changed: 55 additions & 25 deletions

File tree

plugin/src/main/java/org/autorefactor/jdt/internal/corext/dom/ForLoopHelper.java

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,16 @@ public static ForLoopContent iterateOverContainer(ForStatement node) {
184184
final List<Expression> initializers= ASTNodes.initializers(node);
185185
final Expression condition= node.getExpression();
186186
final List<Expression> updaters= ASTNodes.updaters(node);
187+
187188
if (initializers.size() == 1) {
188189
Expression firstInit= initializers.get(0);
190+
189191
if (updaters.isEmpty()) {
190192
final Pair<Name, Expression> initPair= decomposeInitializer(firstInit);
191193
final Name init= initPair.getFirst();
192194
final MethodInvocation condMi= ASTNodes.as(node.getExpression(), MethodInvocation.class);
193195
final MethodInvocation initMi= ASTNodes.as(initPair.getSecond(), MethodInvocation.class);
196+
194197
if (condMi != null && ASTNodes.isSameVariable(init, condMi.getExpression())
195198
&& ASTNodes.usesGivenSignature(initMi, Collection.class.getCanonicalName(), "iterator") //$NON-NLS-1$
196199
&& ASTNodes.usesGivenSignature(condMi, Iterator.class.getCanonicalName(), "hasNext")) { //$NON-NLS-1$
@@ -209,32 +212,39 @@ public static ForLoopContent iterateOverContainer(ForStatement node) {
209212
}
210213
}
211214
}
215+
212216
return null;
213217
}
214218

215219
private static ForLoopContent getIteratorOnCollection(Expression containerVar, Expression iteratorVariable) {
216220
if (containerVar instanceof Name || containerVar instanceof FieldAccess) {
217221
return ForLoopContent.iteratedCollection(containerVar, iteratorVariable);
218222
}
223+
219224
return null;
220225
}
221226

222227
private static Name getUpdaterOperand(Expression updater) {
223228
Expression updaterOperand= null;
229+
224230
if (updater instanceof PostfixExpression) {
225231
final PostfixExpression pe= (PostfixExpression) updater;
232+
226233
if (ASTNodes.hasOperator(pe, PostfixExpression.Operator.INCREMENT)) {
227234
updaterOperand= pe.getOperand();
228235
}
229236
} else if (updater instanceof PrefixExpression) {
230237
final PrefixExpression pe= (PrefixExpression) updater;
238+
231239
if (ASTNodes.hasOperator(pe, PrefixExpression.Operator.INCREMENT)) {
232240
updaterOperand= pe.getOperand();
233241
}
234242
}
243+
235244
if (updaterOperand instanceof Name) {
236245
return (Name) updaterOperand;
237246
}
247+
238248
return null;
239249
}
240250

@@ -257,8 +267,10 @@ public static Pair<Name, Expression> decomposeInitializer(Expression init) {
257267
}
258268
} else if (init instanceof Assignment) {
259269
final Assignment as= (Assignment) init;
260-
if (ASTNodes.hasOperator(as, Assignment.Operator.ASSIGN) && as.getLeftHandSide() instanceof Name) {
261-
return Pair.of((Name) as.getLeftHandSide(), as.getRightHandSide());
270+
final Name name= ASTNodes.as(as.getLeftHandSide(), Name.class);
271+
272+
if (ASTNodes.hasOperator(as, Assignment.Operator.ASSIGN) && name != null) {
273+
return Pair.of(name, as.getRightHandSide());
262274
}
263275
}
264276
return Pair.empty();
@@ -271,47 +283,65 @@ private static ForLoopContent getIndexOnIterable(final Expression condition, Nam
271283
final Expression leftOp= ie.getLeftOperand();
272284
final Expression rightOp= ie.getRightOperand();
273285

286+
if (!(loopVariable instanceof Name)) {
287+
return null;
288+
}
289+
274290
if (ASTNodes.hasOperator(ie, InfixExpression.Operator.LESS) && ASTNodes.isSameLocalVariable(loopVariable, leftOp)) {
275-
return buildForLoopContent(loopVariable, rightOp);
291+
return buildForLoopContent((Name) loopVariable, rightOp);
276292
} else if (ASTNodes.hasOperator(ie, InfixExpression.Operator.GREATER) && ASTNodes.isSameLocalVariable(loopVariable, rightOp)) {
277-
return buildForLoopContent(loopVariable, leftOp);
293+
return buildForLoopContent((Name) loopVariable, leftOp);
278294
}
279295
}
280296

281297
return null;
282298
}
283299

284-
private static ForLoopContent buildForLoopContent(final Expression loopVar, final Expression containerVar) {
285-
if (!(loopVar instanceof Name)) {
286-
return null;
300+
private static ForLoopContent buildForLoopContent(final Name loopVar, final Expression containerVar) {
301+
Expression collectionOnSize= getCollectionOnSize(containerVar);
302+
303+
if (collectionOnSize != null) {
304+
return ForLoopContent.indexedCollection(collectionOnSize, loopVar);
287305
}
288-
if (containerVar instanceof MethodInvocation) {
289-
final MethodInvocation mi= (MethodInvocation) containerVar;
306+
307+
Expression arrayOnLength= getArrayOnLength(containerVar);
308+
309+
if (arrayOnLength != null) {
310+
return ForLoopContent.indexedArray(arrayOnLength, loopVar);
311+
}
312+
313+
return null;
314+
}
315+
316+
private static Expression getCollectionOnSize(final Expression containerVar) {
317+
final MethodInvocation mi= ASTNodes.as(containerVar, MethodInvocation.class);
318+
319+
if (mi != null) {
290320
final Expression containerVarName= ASTNodes.getUnparenthesedExpression(mi.getExpression());
321+
291322
if (containerVarName != null && ASTNodes.usesGivenSignature(mi, Collection.class.getCanonicalName(), "size")) { //$NON-NLS-1$
292-
return ForLoopContent.indexedCollection(containerVarName, (Name) loopVar);
323+
return containerVarName;
293324
}
294-
} else if (containerVar instanceof QualifiedName) {
325+
}
326+
327+
return null;
328+
}
329+
330+
private static Expression getArrayOnLength(final Expression containerVar) {
331+
if (containerVar instanceof QualifiedName) {
295332
final QualifiedName containerVarName= (QualifiedName) containerVar;
296-
if (isArrayLength(containerVarName)) {
297-
Name containerVariable= ((QualifiedName) containerVar).getQualifier();
298-
return ForLoopContent.indexedArray(containerVariable, (Name) loopVar);
333+
334+
if (ASTNodes.isArray(containerVarName.getQualifier()) && "length".equals(containerVarName.getName().getIdentifier())) { //$NON-NLS-1$
335+
return containerVarName.getQualifier();
299336
}
300337
} else if (containerVar instanceof FieldAccess) {
301338
final FieldAccess containerVarName= (FieldAccess) containerVar;
302-
if (isArrayLength(containerVarName)) {
303-
Expression containerVariable= ((FieldAccess) containerVar).getExpression();
304-
return ForLoopContent.indexedArray(containerVariable, (Name) loopVar);
339+
340+
if (ASTNodes.isArray(containerVarName.getExpression()) && "length".equals(containerVarName.getName().getIdentifier())) { //$NON-NLS-1$
341+
return containerVarName.getExpression();
305342
}
306343
}
307-
return null;
308-
}
309-
310-
private static boolean isArrayLength(final QualifiedName containerVarName) {
311-
return ASTNodes.isArray(containerVarName.getQualifier()) && "length".equals(containerVarName.getName().getIdentifier()); //$NON-NLS-1$
312-
}
313344

314-
private static boolean isArrayLength(final FieldAccess containerVarName) {
315-
return ASTNodes.isArray(containerVarName.getExpression()) && "length".equals(containerVarName.getName().getIdentifier()); //$NON-NLS-1$
345+
return null;
316346
}
317347
}

0 commit comments

Comments
 (0)