-
Notifications
You must be signed in to change notification settings - Fork 82
/
GenerateForLoopAssistProposalCore.java
539 lines (458 loc) · 25.1 KB
/
GenerateForLoopAssistProposalCore.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*******************************************************************************
* Copyright (c) 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Red Hat Inc. - Body moved from org.eclipse.jdt.internal.ui.text.correction.proposals.GenerateForLoopAssistProposal
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text.correction.proposals;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.link.LinkedPosition;
import org.eclipse.jface.text.link.LinkedPositionGroup;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ArrayAccess;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.EnhancedForStatement;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.PostfixExpression;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation;
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
import org.eclipse.jdt.internal.ui.text.correction.IProposalRelevance;
public class GenerateForLoopAssistProposalCore extends LinkedCorrectionProposalCore {
public static final int GENERATE_FOREACH= 0;
public static final int GENERATE_ITERATOR_FOR= 1;
public static final int GENERATE_ITERATE_ARRAY= 2;
public static final int GENERATE_ITERATE_LIST= 3;
private ExpressionStatement fCurrentStatement;
private Expression fCurrentExpression;
private ITypeBinding fExpressionType;
private int fLoopTypeToGenerate= -1;
public GenerateForLoopAssistProposalCore(ICompilationUnit cu, ExpressionStatement currentStatement, int loopTypeToGenerate) {
super("", cu, null, IProposalRelevance.GENERATE_FOR_LOOP); //$NON-NLS-1$
fCurrentStatement= currentStatement;
fCurrentExpression= fCurrentStatement.getExpression();
fLoopTypeToGenerate= loopTypeToGenerate;
fExpressionType= fCurrentExpression.resolveTypeBinding();
switch (loopTypeToGenerate) {
case GenerateForLoopAssistProposalCore.GENERATE_FOREACH:
setDisplayName(CorrectionMessages.QuickAssistProcessor_generate_enhanced_for_loop);
setRelevance(IProposalRelevance.GENERATE_ENHANCED_FOR_LOOP);
break;
case GenerateForLoopAssistProposalCore.GENERATE_ITERATOR_FOR:
setDisplayName(CorrectionMessages.QuickAssistProcessor_generate_iterator_for_loop);
break;
case GenerateForLoopAssistProposalCore.GENERATE_ITERATE_ARRAY:
setDisplayName(CorrectionMessages.QuickAssistProcessor_generate_for_loop);
break;
case GenerateForLoopAssistProposalCore.GENERATE_ITERATE_LIST:
setDisplayName(CorrectionMessages.QuickAssistProcessor_generate_index_for_loop);
break;
default:
break;
}
}
@Override
protected ASTRewrite getRewrite() throws CoreException {
AST ast= fCurrentStatement.getAST();
createImportRewrite((CompilationUnit) fCurrentExpression.getRoot());
switch (fLoopTypeToGenerate) {
case GenerateForLoopAssistProposalCore.GENERATE_FOREACH:
return generateForEachRewrite(ast);
case GenerateForLoopAssistProposalCore.GENERATE_ITERATOR_FOR:
return generateIteratorBasedForRewrite(ast);
case GenerateForLoopAssistProposalCore.GENERATE_ITERATE_ARRAY:
return generateForRewrite(ast);
case GenerateForLoopAssistProposalCore.GENERATE_ITERATE_LIST:
return generateIndexBasedForRewrite(ast);
default:
return null;
}
}
private void replace(Statement loopStatement, ASTRewrite rewrite) {
if (ASTNodes.hasSemicolon(fCurrentStatement, getCompilationUnit())) {
rewrite.replace(fCurrentStatement, loopStatement, null);
} else {
rewrite.replace(fCurrentExpression, loopStatement, null);
}
}
/**
* Helper to generate a <code>foreach</code> loop to iterate over an {@link Iterable}.
*
* @param ast the {@link AST} instance to rewrite the loop to
* @return the complete {@link ASTRewrite} object
*/
private ASTRewrite generateForEachRewrite(AST ast) {
EnhancedForStatement loopStatement= ast.newEnhancedForStatement();
ASTRewrite rewrite= ASTRewrite.create(ast);
ITypeBinding loopOverType= extractType(ast);
// generate name proposals and add them to the variable declaration
SimpleName forDeclarationName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), null, true);
SingleVariableDeclaration forLoopInitializer= ast.newSingleVariableDeclaration();
forLoopInitializer.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentStatement, getImportRewrite()), TypeLocation.LOCAL_VARIABLE));
forLoopInitializer.setName(forDeclarationName);
loopStatement.setParameter(forLoopInitializer);
loopStatement.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
Block forLoopBody= ast.newBlock();
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
replace(loopStatement, rewrite);
return rewrite;
}
/**
* Helper to generate an iterator based <code>for</code> loop to iterate over an
* {@link Iterable}.
*
* @param ast the {@link AST} instance to rewrite the loop to
* @return the complete {@link ASTRewrite} object
*/
private ASTRewrite generateIteratorBasedForRewrite(AST ast) {
ASTRewrite rewrite= ASTRewrite.create(ast);
ForStatement loopStatement= ast.newForStatement();
ITypeBinding loopOverType= extractType(ast);
SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "iterator", null, true); //$NON-NLS-1$
loopStatement.initializers().add(getIteratorBasedForInitializer(rewrite, loopVariableName));
MethodInvocation loopExpression= ast.newMethodInvocation();
loopExpression.setName(ast.newSimpleName("hasNext")); //$NON-NLS-1$
SimpleName expressionName= ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(expressionName), LinkedPositionGroup.NO_STOP, expressionName.getIdentifier());
loopExpression.setExpression(expressionName);
loopStatement.setExpression(loopExpression);
Block forLoopBody= ast.newBlock();
Assignment assignResolvedVariable= getIteratorBasedForBodyAssignment(rewrite, loopOverType, loopVariableName);
forLoopBody.statements().add(ast.newExpressionStatement(assignResolvedVariable));
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
replace(loopStatement, rewrite);
return rewrite;
}
/**
* Generates the initializer for an iterator based <code>for</code> loop, which declares and
* initializes the variable to loop over.
*
* @param rewrite the instance of {@link ASTRewrite}
* @param loopVariableName the proposed name of the loop variable
* @return a {@link VariableDeclarationExpression} to use as initializer
*/
private VariableDeclarationExpression getIteratorBasedForInitializer(ASTRewrite rewrite, SimpleName loopVariableName) {
AST ast= rewrite.getAST();
IMethodBinding iteratorMethodBinding= Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {}); //$NON-NLS-1$
// initializing fragment
VariableDeclarationFragment varDeclarationFragment= ast.newVariableDeclarationFragment();
varDeclarationFragment.setName(loopVariableName);
MethodInvocation iteratorExpression= ast.newMethodInvocation();
iteratorExpression.setName(ast.newSimpleName(iteratorMethodBinding.getName()));
iteratorExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
varDeclarationFragment.setInitializer(iteratorExpression);
// declaration
VariableDeclarationExpression varDeclarationExpression= ast.newVariableDeclarationExpression(varDeclarationFragment);
varDeclarationExpression.setType(getImportRewrite().addImport(iteratorMethodBinding.getReturnType(), ast, new ContextSensitiveImportRewriteContext(fCurrentStatement, getImportRewrite()), TypeLocation.LOCAL_VARIABLE));
return varDeclarationExpression;
}
/**
* Generates the Assignment in an iterator based for, used in the first statement of an iterator
* based <code>for</code> loop body, to retrieve the next element of the {@link Iterable}
* instance.
*
* @param rewrite the current instance of {@link ASTRewrite}
* @param loopOverType the {@link ITypeBinding} of the loop variable
* @param loopVariableName the name of the loop variable
* @return an {@link Assignment}, which retrieves the next element of the {@link Iterable} using
* the active {@link Iterator}
*/
private Assignment getIteratorBasedForBodyAssignment(ASTRewrite rewrite, ITypeBinding loopOverType, SimpleName loopVariableName) {
AST ast= rewrite.getAST();
Assignment assignResolvedVariable= ast.newAssignment();
// left hand side
SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment();
resolvedVariableDeclarationFragment.setName(resolvedVariableName);
VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentStatement, getImportRewrite()), TypeLocation.LOCAL_VARIABLE));
assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);
// right hand side
MethodInvocation invokeIteratorNextExpression= ast.newMethodInvocation();
invokeIteratorNextExpression.setName(ast.newSimpleName("next")); //$NON-NLS-1$
SimpleName currentElementName= ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(currentElementName), LinkedPositionGroup.NO_STOP, currentElementName.getIdentifier());
invokeIteratorNextExpression.setExpression(currentElementName);
assignResolvedVariable.setRightHandSide(invokeIteratorNextExpression);
assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);
return assignResolvedVariable;
}
/**
* Helper to generate an index based <code>for</code> loop to iterate over an array.
*
* @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
* @return an applicable {@link ASTRewrite} instance
*/
private ASTRewrite generateForRewrite(AST ast) {
ASTRewrite rewrite= ASTRewrite.create(ast);
ForStatement loopStatement= ast.newForStatement();
SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); //$NON-NLS-1$
loopStatement.initializers().add(getForInitializer(ast, loopVariableName));
FieldAccess getArrayLengthExpression= ast.newFieldAccess();
getArrayLengthExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
getArrayLengthExpression.setName(ast.newSimpleName("length")); //$NON-NLS-1$
loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), getArrayLengthExpression, InfixExpression.Operator.LESS));
loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));
Block forLoopBody= ast.newBlock();
forLoopBody.statements().add(ast.newExpressionStatement(getForBodyAssignment(rewrite, loopVariableName)));
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
replace(loopStatement, rewrite);
return rewrite;
}
/**
* Creates an {@link Assignment} as first expression appearing in a <code>for</code> loop's
* body. This Assignment declares a local variable and initializes it using the array's current
* element identified by the loop index.
*
* @param rewrite the current {@link ASTRewrite} instance
* @param loopVariableName the name of the index variable in String representation
* @return a completed {@link Assignment} containing the mentioned declaration and
* initialization
*/
private Assignment getForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) {
AST ast= rewrite.getAST();
ITypeBinding loopOverType= extractType(ast);
Assignment assignResolvedVariable= ast.newAssignment();
// left hand side
SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment();
resolvedVariableDeclarationFragment.setName(resolvedVariableName);
VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentStatement, getImportRewrite()), TypeLocation.LOCAL_VARIABLE));
assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);
// right hand side
ArrayAccess access= ast.newArrayAccess();
access.setArray((Expression) rewrite.createCopyTarget(fCurrentExpression));
SimpleName indexName= ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(indexName), LinkedPositionGroup.NO_STOP, indexName.getIdentifier());
access.setIndex(indexName);
assignResolvedVariable.setRightHandSide(access);
assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);
return assignResolvedVariable;
}
/**
* Creates an {@link InfixExpression} which is linked to the group of the variableToIncrement.
*
* @param rewrite the current {@link ASTRewrite} instance
* @param variableToIncrement the name of the variable to generate the {@link InfixExpression}
* for
* @param rightHandSide the right hand side expression which shall be included in the
* {@link InfixExpression}
* @param operator the {@link org.eclipse.jdt.core.dom.InfixExpression.Operator} to use in the
* {@link InfixExpression} to create
* @return a filled, new {@link InfixExpression} instance
*/
private InfixExpression getLinkedInfixExpression(ASTRewrite rewrite, String variableToIncrement, Expression rightHandSide, InfixExpression.Operator operator) {
AST ast= rewrite.getAST();
InfixExpression loopExpression= ast.newInfixExpression();
SimpleName name= ast.newSimpleName(variableToIncrement);
addLinkedPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP, name.getIdentifier());
loopExpression.setLeftOperand(name);
loopExpression.setOperator(operator);
loopExpression.setRightOperand(rightHandSide);
return loopExpression;
}
/**
* Creates a {@link PostfixExpression} used to increment the loop variable of a <code>for</code>
* loop to iterate over an array.
*
* @param rewrite the current {@link ASTRewrite} instance
* @param variableToIncrement the name of the variable to increment
* @return a filled {@link PostfixExpression} realizing an incrementation of the specified
* variable
*/
private Expression getLinkedIncrementExpression(ASTRewrite rewrite, String variableToIncrement) {
AST ast= rewrite.getAST();
PostfixExpression incrementLoopVariable= ast.newPostfixExpression();
SimpleName name= ast.newSimpleName(variableToIncrement);
addLinkedPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP, name.getIdentifier());
incrementLoopVariable.setOperand(name);
incrementLoopVariable.setOperator(PostfixExpression.Operator.INCREMENT);
return incrementLoopVariable;
}
/**
* Generates a {@link VariableDeclarationExpression}, which initializes the loop variable to
* iterate over an array.
*
* @param ast the current {@link AST} instance
* @param loopVariableName the name of the variable which should be initialized
* @return a filled {@link VariableDeclarationExpression}, declaring a int variable, which is
* initializes with 0
*/
private VariableDeclarationExpression getForInitializer(AST ast, SimpleName loopVariableName) {
// initializing fragment
VariableDeclarationFragment firstDeclarationFragment= ast.newVariableDeclarationFragment();
firstDeclarationFragment.setName(loopVariableName);
NumberLiteral startIndex= ast.newNumberLiteral();
firstDeclarationFragment.setInitializer(startIndex);
// declaration
VariableDeclarationExpression variableDeclaration= ast.newVariableDeclarationExpression(firstDeclarationFragment);
PrimitiveType variableType= ast.newPrimitiveType(PrimitiveType.INT);
variableDeclaration.setType(variableType);
return variableDeclaration;
}
/**
* Helper to generate an index based <code>for</code> loop to iterate over a {@link List}
* implementation.
*
* @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
* @return an applicable {@link ASTRewrite} instance
*/
private ASTRewrite generateIndexBasedForRewrite(AST ast) {
ASTRewrite rewrite= ASTRewrite.create(ast);
ForStatement loopStatement= ast.newForStatement();
SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); //$NON-NLS-1$
loopStatement.initializers().add(getForInitializer(ast, loopVariableName));
MethodInvocation listSizeExpression= ast.newMethodInvocation();
listSizeExpression.setName(ast.newSimpleName("size")); //$NON-NLS-1$
Expression listExpression= (Expression) rewrite.createCopyTarget(fCurrentExpression);
listSizeExpression.setExpression(listExpression);
loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), listSizeExpression, InfixExpression.Operator.LESS));
loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));
Block forLoopBody= ast.newBlock();
forLoopBody.statements().add(ast.newExpressionStatement(getIndexBasedForBodyAssignment(rewrite, loopVariableName)));
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
replace(loopStatement, rewrite);
return rewrite;
}
/**
* Creates an {@link Assignment} as first expression appearing in an index based
* <code>for</code> loop's body. This Assignment declares a local variable and initializes it
* using the {@link List}'s current element identified by the loop index.
*
* @param rewrite the current {@link ASTRewrite} instance
* @param loopVariableName the name of the index variable in String representation
* @return a completed {@link Assignment} containing the mentioned declaration and
* initialization
*/
private Expression getIndexBasedForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) {
AST ast= rewrite.getAST();
ITypeBinding loopOverType= extractType(ast);
Assignment assignResolvedVariable= ast.newAssignment();
// left hand side
SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment();
resolvedVariableDeclarationFragment.setName(resolvedVariableName);
VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentStatement, getImportRewrite()), TypeLocation.LOCAL_VARIABLE));
assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);
// right hand side
MethodInvocation invokeGetExpression= ast.newMethodInvocation();
invokeGetExpression.setName(ast.newSimpleName("get")); //$NON-NLS-1$
SimpleName indexVariableName= ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(indexVariableName), LinkedPositionGroup.NO_STOP, indexVariableName.getIdentifier());
invokeGetExpression.arguments().add(indexVariableName);
invokeGetExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
assignResolvedVariable.setRightHandSide(invokeGetExpression);
assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);
return assignResolvedVariable;
}
/**
* Resolves name proposals by the given basename and adds a {@link LinkedPosition} to the
* returned {@link SimpleName} expression.
*
* @param rewrite the current instance of an {@link ASTRewrite}
* @param basename the base string to use for proposal calculation
* @param excludedName a name that cannot be used for the variable; <code>null</code> if none
* @param firstLinkedProposal true if the generated name is the first {@link LinkedPosition} to
* edit in the current {@link CompilationUnit}, false otherwise
* @return the linked {@link SimpleName} instance based on the name proposals
*/
private SimpleName resolveLinkedVariableNameWithProposals(ASTRewrite rewrite, String basename, String excludedName, boolean firstLinkedProposal) {
AST ast= rewrite.getAST();
String[] nameProposals= getVariableNameProposals(basename, excludedName);
SimpleName forDeclarationName= ast.newSimpleName(nameProposals.length > 0 ? nameProposals[0] : basename);
for (String nameProposal : nameProposals) {
addLinkedPositionProposal(forDeclarationName.getIdentifier(), nameProposal);
}
// mark declaration name as editable
addLinkedPosition(rewrite.track(forDeclarationName), firstLinkedProposal, forDeclarationName.getIdentifier());
return forDeclarationName;
}
/**
* Generates an empty statement, which is shown as blank line and is set as end position for the
* cursor.
*
* @param rewrite the current {@link ASTRewrite} instance
* @return an empty statement, shown as blank line
*/
private Statement createBlankLineStatementWithCursorPosition(ASTRewrite rewrite) {
Statement blankLineStatement= (Statement) rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$
setEndPosition(rewrite.track(blankLineStatement));
return blankLineStatement;
}
/**
* Retrieves name proposals for a fresh local variable.
*
* @param basename the basename of the proposals
* @param excludedName a name that cannot be used for the variable; <code>null</code> if none
* @return an array of proposal strings
*/
private String[] getVariableNameProposals(String basename, String excludedName) {
ASTNode surroundingBlock= fCurrentStatement.getParent();
while (!(surroundingBlock instanceof Block)) {
surroundingBlock= surroundingBlock.getParent();
}
Collection<String> localUsedNames= new ScopeAnalyzer((CompilationUnit) fCurrentExpression.getRoot()).getUsedVariableNames(surroundingBlock.getStartPosition(), surroundingBlock.getLength());
if (excludedName != null) {
localUsedNames.add(excludedName);
}
String[] names= StubUtility.getLocalNameSuggestions(getCompilationUnit().getJavaProject(), basename, 0, localUsedNames.toArray(new String[localUsedNames.size()]));
return names;
}
/**
* Extracts the type parameter of the variable contained in fCurrentExpression or the elements
* type to iterate over an array using <code>foreach</code>.
*
* @param ast the current {@link AST} instance
* @return the {@link ITypeBinding} of the elements to iterate over
*/
private ITypeBinding extractType(AST ast) {
if (fExpressionType.isArray()) {
return Bindings.normalizeForDeclarationUse(fExpressionType.getComponentType(), ast);
}
// extract elements type directly out of the bindings
IMethodBinding iteratorMethodBinding= Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {}); //$NON-NLS-1$
IMethodBinding iteratorNextMethodBinding= Bindings.findMethodInHierarchy(iteratorMethodBinding.getReturnType(), "next", new ITypeBinding[] {}); //$NON-NLS-1$
ITypeBinding currentElementBinding= iteratorNextMethodBinding.getReturnType();
return Bindings.normalizeForDeclarationUse(currentElementBinding, ast);
}
}