Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use modern foreach loop #2315

Merged
merged 1 commit into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package spoon.support.visitor.replace;


import spoon.reflect.declaration.CtElement;

/**
* Used to replace an element by another one.
*
Expand Down Expand Up @@ -1347,8 +1349,8 @@ private <T extends spoon.reflect.declaration.CtElement> void replaceInListIfExis
if (shouldBeDeleted != null) {
list.remove(index);
if ((replace.length) > 0) {
for (int i = 0; i < (replace.length); i++) {
T ele = ((T) (replace[i]));
for (CtElement aReplace : replace) {
T ele = ((T) (aReplace));
if (ele != null) {
list.add(index, ele);
ele.setParent(shouldBeDeleted.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ private <T extends CtElement> void replaceInListIfExist(List<T> listProtected, R
if (shouldBeDeleted != null) {
list.remove(index);
if (replace.length > 0) {
for (int i = 0; i < replace.length; i++) {
T ele = (T) replace[i];
for (CtElement aReplace : replace) {
T ele = (T) aReplace;
if (ele != null) {
list.add(index, ele);
ele.setParent(shouldBeDeleted.getParent());
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/spoon/reflect/ast/AstCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public boolean matches(CtInvocation<?> element) {
return false;
}
boolean isDataStructure = false;
for (int i = 0; i < collectionsRef.size(); i++) {
CtTypeReference<?> ctTypeReference = collectionsRef.get(i);
for (CtTypeReference<?> ctTypeReference : collectionsRef) {
if (element.getType().isSubtypeOf(ctTypeReference)) {
isDataStructure = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ public void testCtInheritanceScanner() throws Throwable {
instance.accept(mocked);

// verify we call all methods
for (int i = 0; i < toInvoke.size(); i++) {
for (Method aToInvoke : toInvoke) {
try {
toInvoke.get(i).invoke(verify(mocked), instance);
aToInvoke.invoke(verify(mocked), instance);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof AssertionError) {
fail("visit" + instance.getClass().getSimpleName().replaceAll("Impl$", "") + " does not call " + toInvoke.get(i).getName());
fail("visit" + instance.getClass().getSimpleName().replaceAll("Impl$", "") + " does not call " + aToInvoke.getName());
} else {
throw e.getTargetException();
}
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/spoon/test/api/MetamodelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ public boolean matches(CtField candidate) {
roles.add(((CtFieldRead) roleExpression).getVariable().getSimpleName());
} else if (roleExpression instanceof CtNewArray) {
List<CtFieldRead> elements = ((CtNewArray) roleExpression).getElements();
for (int i = 0; i < elements.size(); i++) {
CtFieldRead ctFieldRead = elements.get(i);
for (CtFieldRead ctFieldRead : elements) {
roles.add(ctFieldRead.getVariable().getSimpleName());
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/spoon/test/condition/ConditionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public void testNoBlockInConditionAndLoop() throws Exception {
final CtType<Foo> aFoo = ModelUtils.buildClass(Foo.class);
CtMethod<Object> method = aFoo.getMethod("m3");
final List<CtIf> conditions = method.getElements(new TypeFilter<>(CtIf.class));
for (int i = 0; i < conditions.size(); i++) {
CtIf ctIf = conditions.get(i);

for (CtIf ctIf : conditions) {
// replace the block to a statement
CtStatement then = ((CtBlock) ctIf.getThenStatement()).getStatement(0);
ctIf.setThenStatement(then);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/spoon/test/imports/ImportScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ private boolean isTypePresentInStaticImports(String type, Collection<String> sta
private List<String> getStaticImportsFromSourceCode(String sourceCode) {
List<String> imports = new ArrayList<>();
String[] lines = sourceCode.split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i].trim();
for (String aLine : lines) {
String line = aLine.trim();
if (line.startsWith("import static ")) {
line = line.substring(13, line.length() - 1);
imports.add(line.trim());
Expand All @@ -201,8 +201,8 @@ private List<String> getStaticImportsFromSourceCode(String sourceCode) {
private List<String> getTypeImportsFromSourceCode(String sourceCode) {
List<String> imports = new ArrayList<>();
String[] lines = sourceCode.split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i].trim();
for (String aLine : lines) {
String line = aLine.trim();
if (line.startsWith("import ") && !line.contains(" static ")) {
line = line.substring(7, line.length() - 1);
imports.add(line.trim());
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ public void testPackageInNoClasspath () {
boolean containsStringReference = false;
boolean containsJoinerReference = false;

for (Iterator<CtTypeReference<?>> iterator = referencedTypes.iterator(); iterator.hasNext(); ) {
CtTypeReference<?> reference = iterator.next();
for (CtTypeReference<?> reference : referencedTypes) {
if (reference.toString().equals("Demo")) {
containsDemoReference = true;
} else if (reference.toString().equals("void")) {
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/spoon/test/type/TypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ public void test() {
CtType<?> ctType = launcher.getFactory().Class().getAll().get(0);
List<CtNewClass> elements = ctType.getElements(new TypeFilter<>(CtNewClass.class));
assertEquals(4, elements.size());
for (int i = 0; i < elements.size(); i++) {
CtNewClass ctNewClass = elements.get(i);
for (CtNewClass ctNewClass : elements) {
assertEquals("android.content.DialogInterface$OnClickListener", ctNewClass.getAnonymousClass().getSuperclass().getQualifiedName());
}
}
Expand Down