Java 8+: Unconditionally hook java.io.FileSystem class
This is done during JVM boot stage => unsupported FileSystem implementation will prevent JVM boot. Fixes skipping wrapper class sometimes when java.io.File.fs is static final.
This commit is contained in:
parent
487fc80c8e
commit
a1869cb4a1
|
@ -50,18 +50,17 @@ public abstract class BDFileSystem extends FileSystem {
|
||||||
private static FileSystem nativeFileSystem;
|
private static FileSystem nativeFileSystem;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
/* Java 8: getFileSystem() no longer exists on java.io.FileSystem */
|
|
||||||
try {
|
try {
|
||||||
nativeFileSystem = (FileSystem)Class.forName("java.io.DefaultFileSystem")
|
/* Java < 8 */
|
||||||
.getDeclaredMethod("getFileSystem", new Class[0])
|
nativeFileSystem = (FileSystem)FileSystem.class
|
||||||
|
.getDeclaredMethod("getFileSystem",new Class[0])
|
||||||
.invoke(null, new Object[0]);
|
.invoke(null, new Object[0]);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
try {
|
try {
|
||||||
nativeFileSystem = (FileSystem)FileSystem.class
|
/* Just use our wrapper. If it fails, JVM won't be booted anyway ... */
|
||||||
.getDeclaredMethod("getFileSystem",new Class[0])
|
nativeFileSystem = DefaultFileSystem.getNativeFileSystem();
|
||||||
.invoke(null, new Object[0]);
|
} catch (Throwable t) {
|
||||||
} catch (Exception t) {
|
System.err.print("Couldn't find native filesystem: " + e);
|
||||||
System.err.print("Couldn't find native filesystem: " + t);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,7 +76,7 @@ public abstract class BDFileSystem extends FileSystem {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Replace File.fs for Xlets
|
* Replace File.fs for Xlets (required with Java < 8 where this is not done unconditionally)
|
||||||
*
|
*
|
||||||
* (called by org.videolan.BDJClassLoader)
|
* (called by org.videolan.BDJClassLoader)
|
||||||
*/
|
*/
|
||||||
|
@ -101,15 +100,15 @@ public abstract class BDFileSystem extends FileSystem {
|
||||||
filesystem = c.getDeclaredField("fs");
|
filesystem = c.getDeclaredField("fs");
|
||||||
filesystem.setAccessible(true);
|
filesystem.setAccessible(true);
|
||||||
|
|
||||||
/* Java 8: remove "final" modifier from the field */
|
|
||||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
|
||||||
modifiersField.setAccessible(true);
|
|
||||||
modifiersField.setInt(filesystem, filesystem.getModifiers() & ~Modifier.FINAL);
|
|
||||||
|
|
||||||
FileSystem fs = (FileSystem)filesystem.get(null);
|
FileSystem fs = (FileSystem)filesystem.get(null);
|
||||||
if (fs instanceof BDFileSystemImpl) {
|
if (fs instanceof BDFileSystemImpl) {
|
||||||
//System.err.print("FileSystem already wrapped");
|
//System.err.print("FileSystem already wrapped");
|
||||||
} else {
|
} else {
|
||||||
|
/* Java 8: we should never end up here ... */
|
||||||
|
/* Java 8: remove "final" modifier from the field */
|
||||||
|
//Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||||
|
//modifiersField.setAccessible(true);
|
||||||
|
//modifiersField.setInt(filesystem, filesystem.getModifiers() & ~Modifier.FINAL);
|
||||||
filesystem.set(null, new BDFileSystemImpl(fs));
|
filesystem.set(null, new BDFileSystemImpl(fs));
|
||||||
}
|
}
|
||||||
} catch (Exception t) {
|
} catch (Exception t) {
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* This file is part of libbluray
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library. If not, see
|
||||||
|
* <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package java.io;
|
||||||
|
|
||||||
|
class DefaultFileSystem {
|
||||||
|
|
||||||
|
static FileSystem getNativeFileSystem() {
|
||||||
|
Exception e1, e2, e3;
|
||||||
|
try {
|
||||||
|
return (FileSystem)Class.forName(System.getProperty("org.videolan.bdj_filesystem")).newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e3 = e;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return (FileSystem)Class.forName("java.io.UnixFileSystem").newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e1 = e;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return (FileSystem)Class.forName("java.io.WinNTFileSystem").newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e2 = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* No way to recover from here */
|
||||||
|
System.err.println("Unsupported native filesystem !\n\t" + e1 + "\n\t" + e2 + "\n\t" + e3);
|
||||||
|
//Runtime.getRuntime().halt(-1);
|
||||||
|
throw new Error("No filesystem implementation found");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FileSystem getFileSystem() {
|
||||||
|
FileSystem nativeFs = getNativeFileSystem();
|
||||||
|
return new BDFileSystemImpl(nativeFs);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue