Skip to content

Replace non-compliant java.io.File usages with java.nio.file.Path per coding conventions #2118

Description

@maybeec

Problem

The project coding conventions (documentation/contributing/coding-conventions.adoc, section "Obsolete APIs") state:

java.io.File - use java.nio.file.Path

This is enforced by Checkstyle (IllegalImport in checkstyle.xml). However, Checkstyle in CI only runs on changed files, so a number of pre-existing import java.io.File; violations are currently latent — they will fail the build the moment their file is next edited (as just happened in #2088 / PR #2089 for SystemPath.java).

This issue tracks cleaning up the remaining non-compliant java.io.File usages so they follow the NIO/Path convention.

Affected files & classification

All cases have a convention-compliant substitute. Note that RandomAccessFile, FileLock and Apache Commons FileUtils are not banned — only java.io.File (the path abstraction) is. Where those APIs need a File, it can be obtained inline via path.toFile(), which does not trigger IllegalImport (no import).

A) File.separatorChar constant (test mocks)

  • cli/src/test/java/com/devonfw/tools/ide/context/PipRepositoryMock.java
  • cli/src/test/java/com/devonfw/tools/ide/context/MvnRepositoryMock.java
  • cli/src/test/java/com/devonfw/tools/ide/context/NpmRepositoryMock.java

Use: rel.toString().replace(File.separatorChar, '/') (normalize OS separators to /).
Substitute: System.getProperty("file.separator").charAt(0), or NIO-native FileSystems.getDefault().getSeparator(), or build the string by joining the Path name elements with / (fully OS-independent).

B) Real java.io.File object usage

  • cli/src/main/java/com/devonfw/tools/ide/locking/EclipseWorkspaceLockChecker.java (main) — new File(args[0]), isLocked(File), new RandomAccessFile(file, ...).
    Substitute: change signature to isLocked(Path), Path.of(args[0]), Files.isRegularFile(...); feed RandomAccessFile via lockfile.toFile() inline (or switch to FileChannel.open(...)). NB: this logic is duplicated in Eclipse.java#isLocked(Path) (see C) — could be de-duplicated.
  • cli/src/test/java/com/devonfw/tools/ide/locking/EclipseWorkspaceLockCheckerTest.java (test) — File.createTempFile, new File, .delete().
    Substitute: Files.createTempFile(...), Path.of(...), Files.deleteIfExists(...); RandomAccessFile via .toFile() inline. Follows the isLocked signature change above.
  • gui/src/test/java/com/devonfw/ide/gui/context/ProjectManagerTest.java (gui test) — ...toFile() locals with .createNewFile() and .renameTo(...).
    Substitute: Files.createFile(path) and Files.move(src, tgt); the FileUtils.copyDirectory/deleteDirectory calls keep their inline .toFile().

C) Stale javadoc only (genuine doc bug)

  • cli/src/main/java/com/devonfw/tools/ide/tool/eclipse/Eclipse.java (main) — the File import survives only because of a stale {@link File} javadoc on isLocked(Path lockfile), whose parameter is already Path.
    Substitute: fix javadoc to {@link Path} and drop the import. Zero functional change.

Acceptance criteria

  • No import java.io.File; remains in the codebase (grep -rn "import java.io.File;" --include=*.java returns nothing).
  • All replacements use java.nio.file.Path / Files / system-property (or FileSystems) separators per the coding conventions.
  • All existing tests still pass; mvn checkstyle:check passes for the changed files.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestinternalNothing to be added to CHANGELOG, only internal story

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
🏗 In progress

Relationships

None yet

Development

No branches or pull requests

Issue actions