Skip to content

Commit 1f6bdc0

Browse files
Copilotslachiewicz
andcommitted
Add Path-based methods and deprecate File-based methods
Co-authored-by: slachiewicz <6705942+slachiewicz@users.noreply.github.com>
1 parent 3ba03f6 commit 1f6bdc0

15 files changed

+442
-43
lines changed

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

Lines changed: 55 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<>();
@@ -90,14 +105,22 @@ public static Set<PosixFilePermission> getPermissions(int mode) {
90105
return perms;
91106
}
92107

108+
/**
109+
* @deprecated Use {@link Files#readAttributes(Path, Class, java.nio.file.LinkOption...)} directly
110+
*/
111+
@Deprecated
93112
@Nonnull
94-
public static PosixFileAttributes getPosixFileAttributes(@Nonnull File file) throws IOException {
95-
return Files.readAttributes(file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
113+
public static PosixFileAttributes getPosixFileAttributes(@Nonnull Path path) throws IOException {
114+
return Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
96115
}
97116

117+
/**
118+
* @deprecated Use {@link Files#readAttributes(Path, Class, java.nio.file.LinkOption...)} directly
119+
*/
120+
@Deprecated
98121
@Nonnull
99-
public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws IOException {
100-
return getFileAttributes(file.toPath());
122+
public static PosixFileAttributes getPosixFileAttributes(@Nonnull File file) throws IOException {
123+
return getPosixFileAttributes(file.toPath());
101124
}
102125

103126
public static BasicFileAttributes getFileAttributes(Path path) throws IOException {
@@ -111,16 +134,38 @@ public static BasicFileAttributes getFileAttributes(Path path) throws IOExceptio
111134
return Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
112135
}
113136

137+
/**
138+
* @deprecated Use {@link #getFileAttributes(Path)} instead
139+
*/
140+
@Deprecated
141+
@Nonnull
142+
public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws IOException {
143+
return getFileAttributes(file.toPath());
144+
}
145+
114146
public static boolean isUnix(Path path) {
115147
return path.getFileSystem().supportedFileAttributeViews().contains("unix");
116148
}
117149

150+
/**
151+
* @deprecated Use {@link Files#getFileAttributeView(Path, Class, java.nio.file.LinkOption...)} directly
152+
*/
153+
@Deprecated
118154
@Nullable
119-
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull File file) throws IOException {
155+
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull Path path) throws IOException {
120156
try {
121-
return Files.getFileAttributeView(file.toPath(), FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
157+
return Files.getFileAttributeView(path, FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
122158
} catch (UnsupportedOperationException e) {
123159
return null;
124160
}
125161
}
162+
163+
/**
164+
* @deprecated Use {@link Files#getFileAttributeView(Path, Class, java.nio.file.LinkOption...)} directly
165+
*/
166+
@Deprecated
167+
@Nullable
168+
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull File file) throws IOException {
169+
return getFileOwnershipInfo(file.toPath());
170+
}
126171
}

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
@@ -20,6 +20,7 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.nio.file.Path;
2324
import java.util.Collections;
2425
import java.util.LinkedHashMap;
2526
import java.util.List;
@@ -118,36 +119,48 @@ public static boolean isOctalModeEnabled(int mode, int targetMode) {
118119
return (mode & targetMode) != 0;
119120
}
120121

122+
public static PlexusIoResourceAttributes getFileAttributes(Path path) throws IOException {
123+
return getFileAttributes(path, false);
124+
}
125+
126+
public static PlexusIoResourceAttributes getFileAttributes(Path path, boolean followLinks) throws IOException {
127+
return new FileAttributes(path, followLinks);
128+
}
129+
130+
/**
131+
* @deprecated Use {@link #getFileAttributes(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(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(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 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 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(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(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(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: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,48 @@
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+
* @deprecated Use {@link Files#readSymbolicLink(Path)} directly
37+
*/
38+
@Deprecated
39+
public static @Nonnull Path readSymbolicLink(@Nonnull Path symlink) throws IOException {
40+
return Files.readSymbolicLink(symlink);
41+
}
42+
3043
/**
3144
* Reads the target of the symbolic link
3245
*
3346
* @param symlink A file that is a symlink
3447
* @return A file that is the target of the symlink
3548
* @throws java.io.IOException
49+
* @deprecated Use {@link Files#readSymbolicLink(Path)} directly
3650
*/
51+
@Deprecated
3752
public static @Nonnull File readSymbolicLink(@Nonnull File symlink) throws IOException {
38-
return Files.readSymbolicLink(symlink.toPath()).toFile();
53+
return readSymbolicLink(symlink.toPath()).toFile();
3954
}
4055

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());
56+
/**
57+
* @deprecated Use {@link Files#createSymbolicLink(Path, Path, java.nio.file.attribute.FileAttribute[])} directly
58+
*/
59+
@Deprecated
60+
public static @Nonnull Path createSymbolicLink(@Nonnull Path symlink, Path target) throws IOException {
61+
if (!Files.exists(symlink, LinkOption.NOFOLLOW_LINKS)) {
62+
return Files.createSymbolicLink(symlink, target);
4563
}
46-
return link.toFile();
64+
return symlink;
65+
}
66+
67+
/**
68+
* @deprecated Use {@link Files#createSymbolicLink(Path, Path, java.nio.file.attribute.FileAttribute[])} directly
69+
*/
70+
@Deprecated
71+
public static @Nonnull File createSymbolicLink(@Nonnull File symlink, File target) throws IOException {
72+
return createSymbolicLink(symlink.toPath(), target.toPath()).toFile();
4773
}
4874
}

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
}

0 commit comments

Comments
 (0)