Skip to content

Commit 78063ba

Browse files
Copilotslachiewicz
andcommitted
Add Path-based methods and deprecate File-based methods
Co-authored-by: slachiewicz <[email protected]>
1 parent f5206e8 commit 78063ba

File tree

8 files changed

+275
-43
lines changed

8 files changed

+275
-43
lines changed

src/main/java/org/codehaus/plexus/components/io/attributes/AttributeUtils.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,37 @@ public class AttributeUtils {
3838
/*
3939
Reads last-modified with proper failure handling if something goes wrong.
4040
*/
41-
public static long getLastModified(@Nonnull File file) {
41+
public static long getLastModified(@Nonnull Path path) {
4242
try {
43-
BasicFileAttributes basicFileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
43+
BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
4444
return basicFileAttributes.lastModifiedTime().toMillis();
4545
} catch (IOException e) {
4646
throw new RuntimeException(e);
4747
}
4848
}
4949

50-
public static void chmod(@Nonnull File file, int mode) throws IOException {
51-
final Path path = file.toPath();
50+
/**
51+
* @deprecated Use {@link #getLastModified(Path)} instead
52+
*/
53+
@Deprecated
54+
public static long getLastModified(@Nonnull File file) {
55+
return getLastModified(file.toPath());
56+
}
57+
58+
public static void chmod(@Nonnull Path path, int mode) throws IOException {
5259
if (!Files.isSymbolicLink(path)) {
5360
Files.setPosixFilePermissions(path, getPermissions(mode));
5461
}
5562
}
5663

64+
/**
65+
* @deprecated Use {@link #chmod(Path, int)} instead
66+
*/
67+
@Deprecated
68+
public static void chmod(@Nonnull File file, int mode) throws IOException {
69+
chmod(file.toPath(), mode);
70+
}
71+
5772
@Nonnull
5873
public static Set<PosixFilePermission> getPermissions(int mode) {
5974
Set<PosixFilePermission> perms = new HashSet<>();
@@ -91,13 +106,17 @@ public static Set<PosixFilePermission> getPermissions(int mode) {
91106
}
92107

93108
@Nonnull
94-
public static PosixFileAttributes getPosixFileAttributes(@Nonnull File file) throws IOException {
95-
return Files.readAttributes(file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
109+
public static PosixFileAttributes getPosixFileAttributes(@Nonnull Path path) throws IOException {
110+
return Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
96111
}
97112

113+
/**
114+
* @deprecated Use {@link #getPosixFileAttributes(Path)} instead
115+
*/
116+
@Deprecated
98117
@Nonnull
99-
public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws IOException {
100-
return getFileAttributes(file.toPath());
118+
public static PosixFileAttributes getPosixFileAttributes(@Nonnull File file) throws IOException {
119+
return getPosixFileAttributes(file.toPath());
101120
}
102121

103122
public static BasicFileAttributes getFileAttributes(Path path) throws IOException {
@@ -111,16 +130,34 @@ public static BasicFileAttributes getFileAttributes(Path path) throws IOExceptio
111130
return Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
112131
}
113132

133+
/**
134+
* @deprecated Use {@link #getFileAttributes(Path)} instead
135+
*/
136+
@Deprecated
137+
@Nonnull
138+
public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws IOException {
139+
return getFileAttributes(file.toPath());
140+
}
141+
114142
public static boolean isUnix(Path path) {
115143
return path.getFileSystem().supportedFileAttributeViews().contains("unix");
116144
}
117145

118146
@Nullable
119-
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull File file) throws IOException {
147+
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull Path path) throws IOException {
120148
try {
121-
return Files.getFileAttributeView(file.toPath(), FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
149+
return Files.getFileAttributeView(path, FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
122150
} catch (UnsupportedOperationException e) {
123151
return null;
124152
}
125153
}
154+
155+
/**
156+
* @deprecated Use {@link #getFileOwnershipInfo(Path)} instead
157+
*/
158+
@Deprecated
159+
@Nullable
160+
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull File file) throws IOException {
161+
return getFileOwnershipInfo(file.toPath());
162+
}
126163
}

src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,26 @@ public FileAttributes(
8686
this(file);
8787
}
8888

89+
/**
90+
* @deprecated Use {@link #FileAttributes(Path)} instead
91+
*/
92+
@Deprecated
8993
public FileAttributes(@Nonnull File file) throws IOException {
9094
this(file.toPath(), false);
9195
}
9296

97+
/**
98+
* @deprecated Use {@link #FileAttributes(Path, boolean)} instead
99+
*/
100+
@Deprecated
93101
public FileAttributes(@Nonnull File file, boolean followLinks) throws IOException {
94102
this(file.toPath(), followLinks);
95103
}
96104

105+
public FileAttributes(@Nonnull Path path) throws IOException {
106+
this(path, false);
107+
}
108+
97109
private static Map<Integer, String> getUserCache(FileSystem fs) {
98110
return UIDS_CACHE.computeIfAbsent(fs, f -> new ConcurrentHashMap<>());
99111
}
@@ -199,8 +211,16 @@ public FileAttributes(
199211
this.lastModifiedTime = lastModifiedTime;
200212
}
201213

214+
public static @Nonnull PlexusIoResourceAttributes uncached(@Nonnull Path path) throws IOException {
215+
return new FileAttributes(path);
216+
}
217+
218+
/**
219+
* @deprecated Use {@link #uncached(Path)} instead
220+
*/
221+
@Deprecated
202222
public static @Nonnull PlexusIoResourceAttributes uncached(@Nonnull File file) throws IOException {
203-
return new FileAttributes(file);
223+
return uncached(file.toPath());
204224
}
205225

206226
@Nullable

src/main/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributeUtils.java

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,36 +118,49 @@ public static boolean isOctalModeEnabled(int mode, int targetMode) {
118118
return (mode & targetMode) != 0;
119119
}
120120

121+
public static PlexusIoResourceAttributes getFileAttributes(java.nio.file.Path path) throws IOException {
122+
return getFileAttributes(path, false);
123+
}
124+
125+
public static PlexusIoResourceAttributes getFileAttributes(java.nio.file.Path path, boolean followLinks)
126+
throws IOException {
127+
return new FileAttributes(path, followLinks);
128+
}
129+
130+
/**
131+
* @deprecated Use {@link #getFileAttributes(java.nio.file.Path)} instead
132+
*/
133+
@Deprecated
121134
public static PlexusIoResourceAttributes getFileAttributes(File file) throws IOException {
122-
return getFileAttributes(file, false);
135+
return getFileAttributes(file.toPath(), false);
123136
}
124137

138+
/**
139+
* @deprecated Use {@link #getFileAttributes(java.nio.file.Path, boolean)} instead
140+
*/
141+
@Deprecated
125142
public static PlexusIoResourceAttributes getFileAttributes(File file, boolean followLinks) throws IOException {
126-
Map<String, PlexusIoResourceAttributes> byPath = getFileAttributesByPath(file, false, followLinks);
127-
final PlexusIoResourceAttributes o = byPath.get(file.getAbsolutePath());
128-
if (o == null) {
129-
// We're on a crappy old java version (5) or the OS from hell. Just "fail".
130-
return SimpleResourceAttributes.lastResortDummyAttributesForBrokenOS();
131-
}
132-
return o;
143+
return getFileAttributes(file.toPath(), followLinks);
133144
}
134145

135-
public static Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(File dir) throws IOException {
146+
public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(java.nio.file.Path dir)
147+
throws IOException {
136148
return getFileAttributesByPath(dir, true);
137149
}
138150

139151
public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(
140-
@Nonnull File dir, boolean recursive) throws IOException {
152+
@Nonnull java.nio.file.Path dir, boolean recursive) throws IOException {
141153
return getFileAttributesByPath(dir, recursive, false);
142154
}
143155

144156
public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(
145-
@Nonnull File dir, boolean recursive, boolean followLinks) throws IOException {
157+
@Nonnull java.nio.file.Path dir, boolean recursive, boolean followLinks) throws IOException {
146158
final List<String> fileAndDirectoryNames;
147-
if (recursive && dir.isDirectory()) {
148-
fileAndDirectoryNames = FileUtils.getFileAndDirectoryNames(dir, null, null, true, true, true, true);
159+
File dirAsFile = dir.toFile();
160+
if (recursive && java.nio.file.Files.isDirectory(dir)) {
161+
fileAndDirectoryNames = FileUtils.getFileAndDirectoryNames(dirAsFile, null, null, true, true, true, true);
149162
} else {
150-
fileAndDirectoryNames = Collections.singletonList(dir.getAbsolutePath());
163+
fileAndDirectoryNames = Collections.singletonList(dirAsFile.getAbsolutePath());
151164
}
152165

153166
final Map<String, PlexusIoResourceAttributes> attributesByPath = new LinkedHashMap<>();
@@ -157,4 +170,30 @@ public static Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(Fi
157170
}
158171
return attributesByPath;
159172
}
173+
174+
/**
175+
* @deprecated Use {@link #getFileAttributesByPath(java.nio.file.Path)} instead
176+
*/
177+
@Deprecated
178+
public static Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(File dir) throws IOException {
179+
return getFileAttributesByPath(dir.toPath(), true);
180+
}
181+
182+
/**
183+
* @deprecated Use {@link #getFileAttributesByPath(java.nio.file.Path, boolean)} instead
184+
*/
185+
@Deprecated
186+
public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(
187+
@Nonnull File dir, boolean recursive) throws IOException {
188+
return getFileAttributesByPath(dir.toPath(), recursive, false);
189+
}
190+
191+
/**
192+
* @deprecated Use {@link #getFileAttributesByPath(java.nio.file.Path, boolean, boolean)} instead
193+
*/
194+
@Deprecated
195+
public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(
196+
@Nonnull File dir, boolean recursive, boolean followLinks) throws IOException {
197+
return getFileAttributesByPath(dir.toPath(), recursive, followLinks);
198+
}
160199
}

src/main/java/org/codehaus/plexus/components/io/attributes/SymlinkUtils.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,42 @@
2727
* @author Kristian Rosenvold
2828
*/
2929
public class SymlinkUtils {
30+
/**
31+
* Reads the target of the symbolic link
32+
*
33+
* @param symlink A path that is a symlink
34+
* @return A path that is the target of the symlink
35+
* @throws java.io.IOException
36+
*/
37+
public static @Nonnull Path readSymbolicLink(@Nonnull Path symlink) throws IOException {
38+
return Files.readSymbolicLink(symlink);
39+
}
40+
3041
/**
3142
* Reads the target of the symbolic link
3243
*
3344
* @param symlink A file that is a symlink
3445
* @return A file that is the target of the symlink
3546
* @throws java.io.IOException
47+
* @deprecated Use {@link #readSymbolicLink(Path)} instead
3648
*/
49+
@Deprecated
3750
public static @Nonnull File readSymbolicLink(@Nonnull File symlink) throws IOException {
38-
return Files.readSymbolicLink(symlink.toPath()).toFile();
51+
return readSymbolicLink(symlink.toPath()).toFile();
3952
}
4053

41-
public static @Nonnull File createSymbolicLink(@Nonnull File symlink, File target) throws IOException {
42-
Path link = symlink.toPath();
43-
if (!Files.exists(link, LinkOption.NOFOLLOW_LINKS)) {
44-
link = Files.createSymbolicLink(link, target.toPath());
54+
public static @Nonnull Path createSymbolicLink(@Nonnull Path symlink, Path target) throws IOException {
55+
if (!Files.exists(symlink, LinkOption.NOFOLLOW_LINKS)) {
56+
return Files.createSymbolicLink(symlink, target);
4557
}
46-
return link.toFile();
58+
return symlink;
59+
}
60+
61+
/**
62+
* @deprecated Use {@link #createSymbolicLink(Path, Path)} instead
63+
*/
64+
@Deprecated
65+
public static @Nonnull File createSymbolicLink(@Nonnull File symlink, File target) throws IOException {
66+
return createSymbolicLink(symlink.toPath(), target.toPath()).toFile();
4767
}
4868
}

src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoArchiveResourceCollection.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,33 @@ public abstract class AbstractPlexusIoArchiveResourceCollection extends Abstract
3535
protected AbstractPlexusIoArchiveResourceCollection() {}
3636

3737
/**
38-
* Sets the zip file
38+
* Sets the archive file
3939
*/
40+
public void setFile(java.nio.file.Path file) {
41+
this.file = file.toFile();
42+
}
43+
44+
/**
45+
* Sets the archive file
46+
* @deprecated Use {@link #setFile(java.nio.file.Path)} instead
47+
*/
48+
@Deprecated
4049
public void setFile(File file) {
4150
this.file = file;
4251
}
4352

4453
/**
45-
* Returns the zip file
54+
* Returns the archive file as a Path
55+
*/
56+
public java.nio.file.Path getFileAsPath() {
57+
return file != null ? file.toPath() : null;
58+
}
59+
60+
/**
61+
* Returns the archive file
62+
* @deprecated Use {@link #getFileAsPath()} instead
4663
*/
64+
@Deprecated
4765
public File getFile() {
4866
return file;
4967
}

src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoCompressedFileResourceCollection.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,26 @@ public abstract class PlexusIoCompressedFileResourceCollection
4242

4343
private InputStreamTransformer streamTransformers = AbstractPlexusIoResourceCollection.identityTransformer;
4444

45+
public java.nio.file.Path getFileAsPath() {
46+
return file != null ? file.toPath() : null;
47+
}
48+
49+
/**
50+
* @deprecated Use {@link #getFileAsPath()} instead
51+
*/
52+
@Deprecated
4553
public File getFile() {
4654
return file;
4755
}
4856

57+
public void setFile(java.nio.file.Path file) {
58+
this.file = file.toFile();
59+
}
60+
61+
/**
62+
* @deprecated Use {@link #setFile(java.nio.file.Path)} instead
63+
*/
64+
@Deprecated
4965
public void setFile(File file) {
5066
this.file = file;
5167
}

src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,31 @@ public String getName(PlexusIoResource resource) {
7575
/**
7676
* @param baseDir The base directory of the file collection
7777
*/
78+
public void setBaseDir(java.nio.file.Path baseDir) {
79+
this.baseDir = baseDir.toFile();
80+
}
81+
82+
/**
83+
* @param baseDir The base directory of the file collection
84+
* @deprecated Use {@link #setBaseDir(java.nio.file.Path)} instead
85+
*/
86+
@Deprecated
7887
public void setBaseDir(File baseDir) {
7988
this.baseDir = baseDir;
8089
}
8190

91+
/**
92+
* @return Returns the file collections base directory as a Path.
93+
*/
94+
public java.nio.file.Path getBaseDirAsPath() {
95+
return baseDir != null ? baseDir.toPath() : null;
96+
}
97+
8298
/**
8399
* @return Returns the file collections base directory.
100+
* @deprecated Use {@link #getBaseDirAsPath()} instead
84101
*/
102+
@Deprecated
85103
public File getBaseDir() {
86104
return baseDir;
87105
}

0 commit comments

Comments
 (0)