From 43737a56bd0f4420a619c8ff563b48330d9571aa Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Thu, 4 Jul 2019 19:16:52 +0200 Subject: [PATCH] Use foreach where possible --- .../SimpleRoles2GrantedAuthoritiesMapperTests.java | 4 ++-- .../core/session/SessionRegistryImplTests.java | 4 ++-- .../springframework/security/crypto/codec/Hex.java | 6 +++--- .../security/crypto/bcrypt/BCryptTests.java | 14 +++++++------- .../src/main/java/sample/dms/AbstractElement.java | 4 +--- .../src/test/java/sample/DmsIntegrationTests.java | 14 +++++++------- ...ticatedWebAuthenticationDetailsSourceTests.java | 4 ++-- 7 files changed, 24 insertions(+), 26 deletions(-) diff --git a/core/src/test/java/org/springframework/security/core/authority/mapping/SimpleRoles2GrantedAuthoritiesMapperTests.java b/core/src/test/java/org/springframework/security/core/authority/mapping/SimpleRoles2GrantedAuthoritiesMapperTests.java index 0f6cfc600e..7388c594fb 100644 --- a/core/src/test/java/org/springframework/security/core/authority/mapping/SimpleRoles2GrantedAuthoritiesMapperTests.java +++ b/core/src/test/java/org/springframework/security/core/authority/mapping/SimpleRoles2GrantedAuthoritiesMapperTests.java @@ -129,8 +129,8 @@ public class SimpleRoles2GrantedAuthoritiesMapperTests { List result = mapper .getGrantedAuthorities(Arrays.asList(roles)); Collection resultColl = new ArrayList<>(result.size()); - for (int i = 0; i < result.size(); i++) { - resultColl.add(result.get(i).getAuthority()); + for (GrantedAuthority grantedAuthority : result) { + resultColl.add(grantedAuthority.getAuthority()); } Collection expectedColl = Arrays.asList(expectedGas); assertThat(expectedColl.containsAll(resultColl) diff --git a/core/src/test/java/org/springframework/security/core/session/SessionRegistryImplTests.java b/core/src/test/java/org/springframework/security/core/session/SessionRegistryImplTests.java index b1a22fd4ed..3fc2dc6ed8 100644 --- a/core/src/test/java/org/springframework/security/core/session/SessionRegistryImplTests.java +++ b/core/src/test/java/org/springframework/security/core/session/SessionRegistryImplTests.java @@ -180,8 +180,8 @@ public class SessionRegistryImplTests { private boolean contains(String sessionId, Object principal) { List info = sessionRegistry.getAllSessions(principal, false); - for (int i = 0; i < info.size(); i++) { - if (sessionId.equals(info.get(i).getSessionId())) { + for (SessionInformation sessionInformation : info) { + if (sessionId.equals(sessionInformation.getSessionId())) { return true; } } diff --git a/crypto/src/main/java/org/springframework/security/crypto/codec/Hex.java b/crypto/src/main/java/org/springframework/security/crypto/codec/Hex.java index c87e26a026..7555abd5b7 100644 --- a/crypto/src/main/java/org/springframework/security/crypto/codec/Hex.java +++ b/crypto/src/main/java/org/springframework/security/crypto/codec/Hex.java @@ -34,11 +34,11 @@ public final class Hex { char[] result = new char[2 * nBytes]; int j = 0; - for (int i = 0; i < nBytes; i++) { + for (byte aByte : bytes) { // Char for top 4 bits - result[j++] = HEX[(0xF0 & bytes[i]) >>> 4]; + result[j++] = HEX[(0xF0 & aByte) >>> 4]; // Bottom 4 - result[j++] = HEX[(0x0F & bytes[i])]; + result[j++] = HEX[(0x0F & aByte)]; } return result; diff --git a/crypto/src/test/java/org/springframework/security/crypto/bcrypt/BCryptTests.java b/crypto/src/test/java/org/springframework/security/crypto/bcrypt/BCryptTests.java index 3447502de3..dc242a49d9 100644 --- a/crypto/src/test/java/org/springframework/security/crypto/bcrypt/BCryptTests.java +++ b/crypto/src/test/java/org/springframework/security/crypto/bcrypt/BCryptTests.java @@ -122,10 +122,10 @@ public class BCryptTests { @Test public void testHashpw() { print("BCrypt.hashpw(): "); - for (int i = 0; i < test_vectors.length; i++) { - String plain = test_vectors[i][0]; - String salt = test_vectors[i][1]; - String expected = test_vectors[i][2]; + for (String[] test_vector : test_vectors) { + String plain = test_vector[0]; + String salt = test_vector[1]; + String expected = test_vector[2]; String hashed = BCrypt.hashpw(plain, salt); assertThat(expected).isEqualTo(hashed); print("."); @@ -176,9 +176,9 @@ public class BCryptTests { @Test public void testCheckpw_success() { print("BCrypt.checkpw w/ good passwords: "); - for (int i = 0; i < test_vectors.length; i++) { - String plain = test_vectors[i][0]; - String expected = test_vectors[i][2]; + for (String[] test_vector : test_vectors) { + String plain = test_vector[0]; + String expected = test_vector[2]; assertThat(BCrypt.checkpw(plain, expected)).isTrue(); print("."); } diff --git a/samples/xml/dms/src/main/java/sample/dms/AbstractElement.java b/samples/xml/dms/src/main/java/sample/dms/AbstractElement.java index 2e4477ccfa..0b8ae05a7c 100755 --- a/samples/xml/dms/src/main/java/sample/dms/AbstractElement.java +++ b/samples/xml/dms/src/main/java/sample/dms/AbstractElement.java @@ -16,7 +16,6 @@ package sample.dms; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.springframework.util.Assert; @@ -88,8 +87,7 @@ public abstract class AbstractElement { StringBuilder sb = new StringBuilder(); String lastCharacter = null; - for (Iterator i = strings.iterator(); i.hasNext();) { - String token = i.next(); + for (String token : strings) { if (!"/".equals(lastCharacter) && lastCharacter != null) { sb.append("/"); } diff --git a/samples/xml/dms/src/test/java/sample/DmsIntegrationTests.java b/samples/xml/dms/src/test/java/sample/DmsIntegrationTests.java index 136dfa497f..bf28298f2e 100644 --- a/samples/xml/dms/src/test/java/sample/DmsIntegrationTests.java +++ b/samples/xml/dms/src/test/java/sample/DmsIntegrationTests.java @@ -105,12 +105,12 @@ public class DmsIntegrationTests extends AbstractTransactionalJUnit4SpringContex assertThat(rootElements).hasSize(3); Directory homeDir = null; Directory nonHomeDir = null; - for (int i = 0; i < rootElements.length; i++) { - if (rootElements[i].getName().equals(username)) { - homeDir = (Directory) rootElements[i]; + for (AbstractElement rootElement : rootElements) { + if (rootElement.getName().equals(username)) { + homeDir = (Directory) rootElement; } else { - nonHomeDir = (Directory) rootElements[i]; + nonHomeDir = (Directory) rootElement; } } System.out.println("Home directory......: " + homeDir.getFullName()); @@ -135,9 +135,9 @@ public class DmsIntegrationTests extends AbstractTransactionalJUnit4SpringContex // Of course, we shouldn't find a "confidential" directory in the results if we're // filtering Directory nonHomeConfidentialDir = null; - for (int i = 0; i < nonHomeElements.length; i++) { - if (nonHomeElements[i].getName().equals("confidential")) { - nonHomeConfidentialDir = (Directory) nonHomeElements[i]; + for (AbstractElement nonHomeElement : nonHomeElements) { + if (nonHomeElement.getName().equals("confidential")) { + nonHomeConfidentialDir = (Directory) nonHomeElement; } } diff --git a/web/src/test/java/org/springframework/security/web/authentication/preauth/j2ee/J2eeBasedPreAuthenticatedWebAuthenticationDetailsSourceTests.java b/web/src/test/java/org/springframework/security/web/authentication/preauth/j2ee/J2eeBasedPreAuthenticatedWebAuthenticationDetailsSourceTests.java index 1d0677f48d..19310a990f 100644 --- a/web/src/test/java/org/springframework/security/web/authentication/preauth/j2ee/J2eeBasedPreAuthenticatedWebAuthenticationDetailsSourceTests.java +++ b/web/src/test/java/org/springframework/security/web/authentication/preauth/j2ee/J2eeBasedPreAuthenticatedWebAuthenticationDetailsSourceTests.java @@ -128,8 +128,8 @@ public class J2eeBasedPreAuthenticatedWebAuthenticationDetailsSourceTests { Collection expectedRolesColl = Arrays.asList(expectedRoles); Collection gasRolesSet = new HashSet<>(); - for (int i = 0; i < gas.size(); i++) { - gasRolesSet.add(gas.get(i).getAuthority()); + for (GrantedAuthority grantedAuthority : gas) { + gasRolesSet.add(grantedAuthority.getAuthority()); } assertThat(expectedRolesColl.containsAll(gasRolesSet) && gasRolesSet.containsAll(expectedRolesColl)).withFailMessage(