-
Notifications
You must be signed in to change notification settings - Fork 207
refactor(#4729): move pinfos method to new PackageInfos class #4797
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| package org.eolang.maven; | ||
|
|
||
| import com.jcabi.log.Logger; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Package info classes. | ||
| * @since 0.60 | ||
| */ | ||
| final class PackageInfos { | ||
|
|
||
| /** | ||
| * Pattern for replacing EO in package. | ||
| */ | ||
| private static final Pattern PACKAGE = Pattern.compile("EO"); | ||
|
|
||
| /** | ||
| * Directory where create package info files. | ||
| */ | ||
| private final Path root; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * @param root In which directory create files. | ||
| */ | ||
| PackageInfos(final Path root) { | ||
| this.root = root; | ||
| } | ||
|
|
||
| /** | ||
| * Create {@code package-info.java} files in all the directories under the {@link #root}. | ||
| * @return Amount of created files | ||
| * @throws IOException If fails to create a file | ||
| */ | ||
| int create() throws IOException { | ||
| final int size; | ||
| if (Files.exists(this.root)) { | ||
| final List<Path> dirs = Files.walk(this.root) | ||
| .filter(file -> Files.isDirectory(file) && !file.equals(this.root)) | ||
| .collect(Collectors.toList()); | ||
|
Comment on lines
+48
to
+50
|
||
| for (final Path dir : dirs) { | ||
| final String pkg = this.root.relativize(dir).toString() | ||
| .replace(File.separator, "."); | ||
|
Comment on lines
+52
to
+53
|
||
| final Path saved = new Saved( | ||
| PackageInfos.content(pkg), dir.resolve("package-info.java") | ||
| ).value(); | ||
| Logger.debug(this, "Created %s", saved); | ||
| } | ||
| size = dirs.size(); | ||
| } else { | ||
| Logger.info( | ||
| this, | ||
| "No generated sources found, skipping package-info.java creation" | ||
| ); | ||
| size = 0; | ||
| } | ||
| return size; | ||
| } | ||
|
|
||
| private static String content(final String pkg) { | ||
| return String.join( | ||
| "\n", | ||
| "/**", | ||
| " * This file was auto-generated by eo-maven-plugin,", | ||
| " * don't modify it, all changes will be lost anyway.", | ||
| " */", | ||
| String.format( | ||
| "// @org.eolang.XmirPackage(\"%s\")", | ||
| PackageInfos.PACKAGE.matcher(pkg).replaceAll("") | ||
| ), | ||
| String.format("package %s;", pkg) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| package org.eolang.maven; | ||
|
|
||
| import com.yegor256.Mktmp; | ||
| import com.yegor256.MktmpResolver; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import org.hamcrest.MatcherAssert; | ||
| import org.hamcrest.Matchers; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
|
|
||
| /** | ||
| * Test cases for {@link PackageInfos}. | ||
| * @since 0.60 | ||
| */ | ||
| @ExtendWith(MktmpResolver.class) | ||
| final class PackageInfosTest { | ||
|
|
||
| @Test | ||
| void createsPackageInfosInSubDirectories(@Mktmp final Path tmp) throws IOException { | ||
| final Path subdir = tmp.resolve("subdir"); | ||
| final Path subsubdir = subdir.resolve("subsubdir"); | ||
| Files.createDirectory(subdir); | ||
| Files.createDirectories(subsubdir); | ||
| MatcherAssert.assertThat( | ||
| "We should create exactly two package-info.java files for two subdirectories", | ||
| new PackageInfos(tmp).create(), | ||
| Matchers.equalTo(2) | ||
| ); | ||
| MatcherAssert.assertThat( | ||
| "package-info.java should be created in the both subdirectories", | ||
| Files.exists(subdir.resolve("package-info.java")) | ||
| && Files.exists(subsubdir.resolve("package-info.java")), | ||
| Matchers.is(true) | ||
| ); | ||
|
Comment on lines
+30
to
+40
|
||
| } | ||
|
|
||
| @Test | ||
| void ignoresTheRootDirectoryItself(@Mktmp final Path tmp) throws IOException { | ||
| MatcherAssert.assertThat( | ||
| "No package-info.java files should be created in the root directory", | ||
| new PackageInfos(tmp).create(), | ||
| Matchers.equalTo(0) | ||
| ); | ||
| MatcherAssert.assertThat( | ||
| "package-info.java should not be created in the root directory", | ||
| Files.exists(tmp.resolve("package-info.java")), | ||
| Matchers.is(false) | ||
| ); | ||
|
Comment on lines
+45
to
+54
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resource leak:
Files.walk()stream is not closed.Files.walk()returns aStream<Path>backed by I/O resources that must be explicitly closed. Using.collect()does not close the underlying stream, which can lead to file handle exhaustion.🔧 Proposed fix using try-with-resources
if (Files.exists(this.root)) { - final List<Path> dirs = Files.walk(this.root) - .filter(file -> Files.isDirectory(file) && !file.equals(this.root)) - .collect(Collectors.toList()); + final List<Path> dirs; + try (java.util.stream.Stream<Path> stream = Files.walk(this.root)) { + dirs = stream + .filter(file -> Files.isDirectory(file) && !file.equals(this.root)) + .collect(Collectors.toList()); + } for (final Path dir : dirs) {📝 Committable suggestion
🤖 Prompt for AI Agents