|
14 | 14 | package ch.qos.logback.core.rolling.helper; |
15 | 15 |
|
16 | 16 | import java.io.File; |
17 | | -import java.io.IOException; |
18 | | -import java.nio.file.FileStore; |
19 | | -import java.nio.file.Files; |
20 | | -import java.nio.file.Path; |
| 17 | +import java.lang.reflect.Method; |
21 | 18 |
|
22 | 19 | /** |
23 | 20 | * A utility class using functionality available since JDK 1.7. |
|
27 | 24 | */ |
28 | 25 | public class FileStoreUtil { |
29 | 26 |
|
30 | | - static boolean areOnSameFileStore(File a, File b) throws IOException { |
31 | | - Path pathA = a.toPath(); |
32 | | - Path pathB = b.toPath(); |
| 27 | + static final String PATH_CLASS_STR = "java.nio.file.Path"; |
| 28 | + static final String FILES_CLASS_STR = "java.nio.file.Files"; |
33 | 29 |
|
34 | | - FileStore fileStoreA = Files.getFileStore(pathA); |
35 | | - FileStore fileStoreB = Files.getFileStore(pathB); |
| 30 | + static public boolean areOnSameFileStore(File a, File b) throws Exception { |
36 | 31 |
|
| 32 | +// Implement the following by reflection |
| 33 | +// Path pathA = a.toPath(); |
| 34 | +// Path pathB = b.toPath(); |
| 35 | +// |
| 36 | +// FileStore fileStoreA = Files.getFileStore(pathA); |
| 37 | +// FileStore fileStoreB = Files.getFileStore(pathB); |
| 38 | +// |
| 39 | +// return fileStoreA.equals(fileStoreB); |
| 40 | + |
| 41 | + Class pathClass = Class.forName(PATH_CLASS_STR); |
| 42 | + Class filesClass = Class.forName(FILES_CLASS_STR); |
| 43 | + |
| 44 | + Method toPath = File.class.getMethod("toPath"); |
| 45 | + Method getFileStoreMethod = filesClass.getMethod("getFileStore", pathClass); |
| 46 | + |
| 47 | + |
| 48 | + Object pathA = toPath.invoke(a); |
| 49 | + Object pathB = b.toPath(); |
| 50 | + |
| 51 | + Object fileStoreA = getFileStoreMethod.invoke(null, pathA); |
| 52 | + Object fileStoreB = getFileStoreMethod.invoke(null, pathB); |
37 | 53 | return fileStoreA.equals(fileStoreB); |
38 | 54 | } |
39 | 55 | } |
0 commit comments