Skip to content

Commit efeb2d2

Browse files
Flossyclaude
andcommitted
fix: reduce deep nesting from 83 to 51 statements through method extraction
Reduces deeply nested statements by 39% (from 83 to 51) by: 1. Extract nesting reduction in ClassLoaderCleanupUtil: - Split clearThreadLocals() into 5 focused methods to reduce nesting - Extract cleanupThreadLocalsForMatchingThreads() to eliminate nested if-statements - clearThreadLocalTable(), clearThreadLocalTableEntries(), clearThreadLocalEntryIfNeeded() all reduce nesting through early returns and guard clauses 2. Refactor AuthHelper to eliminate nested validation: - Extract configureBasicAuth() and configureBearerAuth() private methods - Use switch statement to dispatch instead of nesting validation inside switch cases 3. Extract JAR handling in NexusClassSource: - Create findAndExtractClassFromJar() to eliminate nested while loops - Create extractClassDataFromJarEntry() to handle inner loop and size validation 4. Simplify RetryPolicy retry logic: - Extract performBackoffDelay() method to reduce nesting in execute() - Use early continue with inverted guard clause 5. Apply guard clause in DatabaseClassSource: - Replace if-then-else with guard clause (early throw) to reduce nesting Impact: - Deep nesting statements reduced from 83 to 51 (39% improvement) - Cyclomatic complexity reduced through smaller, focused methods - Code maintainability and testability improved - Methods now follow single responsibility principle Fixes issue #251 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 58db5ee commit efeb2d2

15 files changed

Lines changed: 741 additions & 398 deletions

src/main/java/org/flossware/classloader/AuthHelper.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,33 @@ public static void configureAuth(HttpURLConnection connection, AuthConfig authCo
2424

2525
switch (authConfig.getAuthType()) {
2626
case BASIC:
27-
String username = authConfig.getUsername();
28-
String password = authConfig.getPassword();
29-
if (username == null || password == null) {
30-
throw new IllegalArgumentException("Username and password must not be null for BASIC authentication");
31-
}
32-
String credentials = username + ":" + password;
33-
String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8));
34-
connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
27+
configureBasicAuth(connection, authConfig);
3528
break;
3629
case BEARER:
37-
String token = authConfig.getToken();
38-
if (token == null) {
39-
throw new IllegalArgumentException("Token must not be null for BEARER authentication");
40-
}
41-
connection.setRequestProperty("Authorization", "Bearer " + token);
30+
configureBearerAuth(connection, authConfig);
4231
break;
4332
case NONE:
4433
default:
4534
break;
4635
}
4736
}
37+
38+
private static void configureBasicAuth(HttpURLConnection connection, AuthConfig authConfig) {
39+
String username = authConfig.getUsername();
40+
String password = authConfig.getPassword();
41+
if (username == null || password == null) {
42+
throw new IllegalArgumentException("Username and password must not be null for BASIC authentication");
43+
}
44+
String credentials = username + ":" + password;
45+
String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8));
46+
connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
47+
}
48+
49+
private static void configureBearerAuth(HttpURLConnection connection, AuthConfig authConfig) {
50+
String token = authConfig.getToken();
51+
if (token == null) {
52+
throw new IllegalArgumentException("Token must not be null for BEARER authentication");
53+
}
54+
connection.setRequestProperty("Authorization", "Bearer " + token);
55+
}
4856
}

src/main/java/org/flossware/classloader/DatabaseClassSource.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,10 @@ public byte[] loadClassData(String className) throws IOException {
101101
stmt.setString(1, className);
102102

103103
try (ResultSet rs = stmt.executeQuery()) {
104-
if (rs.next()) {
105-
return rs.getBytes(1);
106-
} else {
104+
if (!rs.next()) {
107105
throw new IOException("Class not found in database: " + className);
108106
}
107+
return rs.getBytes(1);
109108
}
110109

111110
} catch (SQLException e) {

0 commit comments

Comments
 (0)