Skip to content

Commit 978bfe6

Browse files
#190 Removing unused code and unnecessary Javadocs
1 parent 4a8d324 commit 978bfe6

5 files changed

Lines changed: 46 additions & 22 deletions

File tree

‎change-proneness-ranker/src/test/java/org/hjug/git/GitLogReaderTest.java‎

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import static org.mockito.Mockito.when;
88

99
import java.io.*;
10+
import java.nio.file.Files;
11+
import java.nio.file.NoSuchFileException;
1012
import java.util.*;
1113
import org.eclipse.jgit.api.Git;
1214
import org.eclipse.jgit.api.LogCommand;
@@ -190,8 +192,6 @@ void testFileLogReturnsOnlyVerifiedPartialPathHistoryWhenFilteredWalkYieldsNothi
190192
void testFileLogReturnsOnlyVerifiedPartialPathHistoryWithMissingTree() throws Exception {
191193
// Simulates a shallow clone whose tree objects are missing: the filtered walk
192194
// may yield zero results due to missing objects, but should not fall back to total history.
193-
GitLogReader gitLogReader = new GitLogReader(git);
194-
195195
String attributeHandler = "AttributeHandler.java";
196196
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler);
197197
String contents = convertInputStreamToString(resourceAsStream);
@@ -208,11 +208,19 @@ void testFileLogReturnsOnlyVerifiedPartialPathHistoryWithMissingTree() throws Ex
208208

209209
deleteLooseObject(secondCommit.getTree());
210210

211-
ScmLogInfo scmLogInfo = Assertions.assertDoesNotThrow(() -> gitLogReader.fileLog(attributeHandler));
212-
213-
// With missing tree objects, the filtered walk may yield zero verified partial results
214-
// rather than falling back to total repository history
215-
Assertions.assertEquals(0, scmLogInfo.getCommitCount());
211+
// Read through a freshly opened repository: the commits above populated JGit's
212+
// in-memory loose-object cache (UnpackedObjectCache, one per ObjectDirectory
213+
// instance), which could otherwise serve the deleted tree from memory and let
214+
// the walk succeed with 2 commits instead of failing. A new Repository instance
215+
// gets an empty cache, so the missing object is really exercised on every OS.
216+
try (Git freshGit = Git.open(repository.getDirectory())) {
217+
GitLogReader gitLogReader = new GitLogReader(freshGit);
218+
ScmLogInfo scmLogInfo = Assertions.assertDoesNotThrow(() -> gitLogReader.fileLog(attributeHandler));
219+
220+
// With missing tree objects, the filtered walk may yield zero verified partial results
221+
// rather than falling back to total repository history
222+
Assertions.assertEquals(0, scmLogInfo.getCommitCount());
223+
}
216224
}
217225

218226
@Test
@@ -232,16 +240,39 @@ void testFileLogDoesNotFabricateHistoryForFileNeverCommitted() throws Exception
232240
Assertions.assertEquals(0, scmLogInfo.getCommitCount());
233241
}
234242

235-
private void deleteLooseObject(ObjectId objectId) throws IOException {
243+
private void deleteLooseObject(ObjectId objectId) throws IOException, InterruptedException {
236244
String objectName = objectId.getName();
237245
File looseObject = new File(
238246
new File(repository.getDirectory(), "objects"),
239247
objectName.substring(0, 2) + '/' + objectName.substring(2));
240248
org.junit.jupiter.api.Assumptions.assumeTrue(
241249
looseObject.exists(), "loose object " + objectName + " should exist in a fresh repository");
242-
if (!looseObject.delete()) {
243-
throw new IOException("Unable to delete loose object " + looseObject);
250+
251+
// JGit writes loose objects with the read-only attribute set (as C git
252+
// does). POSIX unlink ignores the read-only bit, but Windows refuses to
253+
// delete a read-only file and every retry would fail with
254+
// AccessDeniedException — so the attribute must be cleared first.
255+
if (!looseObject.setWritable(true) && !looseObject.canWrite()) {
256+
throw new IOException("Unable to clear read-only flag on loose object " + looseObject);
257+
}
258+
259+
// On Windows, antivirus or the search indexer can also hold a newly
260+
// created file open briefly, making an immediate delete fail
261+
// spuriously. Retry for a short window before giving up (harmless on
262+
// Linux/macOS, where the first attempt succeeds).
263+
IOException lastError = null;
264+
for (int attempt = 0; attempt < 50; attempt++) {
265+
try {
266+
Files.delete(looseObject.toPath());
267+
return;
268+
} catch (NoSuchFileException alreadyGone) {
269+
return;
270+
} catch (IOException lockedOrBusy) {
271+
lastError = lockedOrBusy;
272+
Thread.sleep(50);
273+
}
244274
}
275+
throw new IOException("Unable to delete loose object " + looseObject, lastError);
245276
}
246277

247278
@Test

‎codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/Java25ParserFactory.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
* the versioned entry is invisible to the class loader — its class-file
1616
* version (69.0) can never be loaded — and this base variant answers.
1717
*
18-
* <p>This replaces the previous reflection-based loading, runtime-version
19-
* detection, and force-flag escape hatch: the JVM performs version selection
20-
* itself, and a forced load is meaningless because version-69 class files
21-
* physically cannot load on older runtimes.
22-
*
2318
* <p>Note: multi-release shadowing only applies when classes are loaded from
2419
* a <em>jar</em>. In exploded-directory classpaths (e.g. unit tests running
2520
* against {@code target/classes}) this base variant answers even on JDK 25.

‎codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/JavaSourceFileGraphBuilder.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public CodebaseGraphDTO buildGraph(String repositoryPath, String repositoryRoot,
4444
throws IOException {
4545
File srcDirectory = new File(repositoryPath);
4646

47-
JavaParser javaParser = createJavaParser(config);
47+
JavaParser javaParser = createJavaParser();
4848
ExecutionContext ctx = new InMemoryExecutionContext(e -> log.warn("OpenRewrite parse/visit error", e));
4949

5050
final Graph<String, DefaultWeightedEdge> classReferencesGraph =
@@ -113,7 +113,7 @@ public CodebaseGraphDTO buildGraph(String repositoryPath, String repositoryRoot,
113113
* JDK 25; the {@code fromJavaVersion()} elevation still yields a
114114
* Java 25-capable parser there when {@code rewrite-java-25} is present.
115115
*/
116-
static JavaParser createJavaParser(GraphBuilderConfig config) {
116+
static JavaParser createJavaParser() {
117117
return Java25ParserFactory.createJava25Parser().orElseGet(() -> {
118118
log.debug("Using JavaParser.fromJavaVersion() (Java 25 multi-release jar variant not active)");
119119
return JavaParser.fromJavaVersion().build();

‎codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/JavaSourceFileGraphBuilderJava25Test.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void standardParserUsedOnOlderRuntime() {
4444
if (Runtime.version().feature() >= 25) {
4545
return; // covered by the JAVA_25-only test below
4646
}
47-
JavaParser parser = JavaSourceFileGraphBuilder.createJavaParser(config());
47+
JavaParser parser = JavaSourceFileGraphBuilder.createJavaParser();
4848
assertFalse(
4949
parser.getClass().getSimpleName().contains("25"),
5050
"Expected a non-Java-25 parser, got: " + parser.getClass().getName());
@@ -60,7 +60,7 @@ void jdk25YieldsJava25CapableParser() {
6060
+ "(JEP 238 shadowing applies only to jars)");
6161
// fromJavaVersion() reflectively elevates to Java25Parser on a 25+
6262
// runtime because rewrite-java-25 is on the (test) classpath.
63-
JavaParser parser = JavaSourceFileGraphBuilder.createJavaParser(config());
63+
JavaParser parser = JavaSourceFileGraphBuilder.createJavaParser();
6464
assertNotNull(parser, "Parser creation must never fail");
6565
assertTrue(
6666
parser.getClass().getName().contains("Java25"),

‎report/src/test/java/org/hjug/refactorfirst/report/JsonGeneratorTest.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ public String execute() {
197197

198198
/**
199199
* Verifies report generation succeeds with parser selection left entirely
200-
* to the runtime (the parser-forcing overload was removed when
201-
* reflection-based parser loading was replaced by the JEP 238
202-
* multi-release jar design).
200+
* to the runtime (JEP 238 multi-release jar design).
203201
*/
204202
@Test
205203
void testExecuteWithoutParserFlags() throws Exception {

0 commit comments

Comments
 (0)