Skip to content

Commit 3a11e11

Browse files
committed
fix: ClassLoader not working with Java 9+
1 parent 7a6025a commit 3a11e11

File tree

6 files changed

+26
-37
lines changed

6 files changed

+26
-37
lines changed

src/main/kotlin/app/revanced/cli/MainCommand.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package app.revanced.cli
22

3-
import app.revanced.patch.PatchLoader
43
import app.revanced.patch.Patches
54
import app.revanced.utils.adb.Adb
65
import picocli.CommandLine.*
@@ -49,8 +48,7 @@ internal object MainCommand : Runnable {
4948
override fun run() {
5049
if (listOnly) {
5150
patchBundles.forEach {
52-
PatchLoader.injectPatches(it)
53-
Patches.loadPatches().forEach {
51+
Patches.load(it).forEach {
5452
println(it().metadata)
5553
}
5654
}

src/main/kotlin/app/revanced/cli/Patcher.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package app.revanced.cli
22

3-
import app.revanced.patch.PatchLoader
43
import app.revanced.patch.Patches
54
import app.revanced.patcher.data.base.Data
65
import app.revanced.patcher.patch.base.Patch
@@ -59,9 +58,8 @@ internal class Patcher {
5958
val checkInclude = MainCommand.includedPatches.isNotEmpty()
6059

6160
MainCommand.patchBundles.forEach { bundle ->
62-
PatchLoader.injectPatches(bundle)
6361
val includedPatches = mutableListOf<Patch<Data>>()
64-
Patches.loadPatches().forEach patch@{
62+
Patches.load(bundle).forEach patch@{
6563
val patch = it()
6664

6765
// TODO: filter out incompatible patches with package metadata
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
package app.revanced.patch
22

33
import java.io.File
4-
import java.net.URL
54
import java.net.URLClassLoader
65

76
internal class PatchLoader {
87
internal companion object {
9-
internal fun injectPatches(file: File) {
10-
// This function will fail on Java 9 and above.
11-
try {
12-
val url = file.toURI().toURL()
13-
val classLoader = Thread.currentThread().contextClassLoader as URLClassLoader
14-
val method = URLClassLoader::class.java.getDeclaredMethod("addURL", URL::class.java)
15-
method.isAccessible = true
16-
method.invoke(classLoader, url)
17-
} catch (e: Exception) {
18-
throw Exception(
19-
"Failed to inject patches! The CLI does NOT work on Java 9 and above, please use Java 8!",
20-
e // propagate exception
21-
)
22-
}
23-
}
8+
249
}
2510
}
+21-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package app.revanced.patch
22

3+
import app.revanced.patcher.data.base.Data
4+
import app.revanced.patcher.patch.base.Patch
35
import app.revanced.patches.Index
6+
import java.io.File
7+
import java.net.URLClassLoader
48

5-
internal class Patches {
6-
internal companion object {
7-
// You may ask yourself, "why do this?".
8-
// We do it like this, because we don't want the Index class
9-
// to be loaded while the dependency hasn't been injected yet.
10-
// You can see this as "controlled class loading".
11-
// Whenever this class is loaded (because it is invoked), all the imports
12-
// will be loaded too. We don't want to do this until we've injected the class.
13-
internal fun loadPatches() = Index.patches
9+
internal object Patches {
10+
11+
12+
/**
13+
* This method loads patches from a given patch file
14+
* @return the loaded patches represented as a list of functions returning instances of [Patch]
15+
*/
16+
internal fun load(patchFile: File): List<() -> Patch<Data>> {
17+
val url = patchFile.toURI().toURL()
18+
val classLoader = URLClassLoader(arrayOf(url))
19+
return loadIndex(classLoader).patches
1420
}
15-
}
21+
private fun loadIndex(classLoader: ClassLoader) = classLoader
22+
.loadClass(Index::class.java.canonicalName)
23+
.fields
24+
.first()
25+
.get(null) as Index
26+
}

src/main/kotlin/app/revanced/utils/filesystem/FileSystemUtils.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ internal class FileSystemUtils(
1212
private var fileSystem: FileSystem
1313

1414
init {
15-
fileSystem = FileSystems.newFileSystem(
16-
file.toPath(),
17-
null
18-
)
15+
fileSystem = FileSystems.newFileSystem(file.toPath(), null as ClassLoader?)
1916
}
2017

2118
private fun deleteDirectory(dirPath: String) {

src/main/kotlin/app/revanced/utils/signing/Signer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ object Signer {
9191
(keyStore.getKey(alias, PASSWORD) as PrivateKey)
9292
)
9393

94-
val zip = FileSystems.newFileSystem(apkFile.toPath(), null)
94+
val zip = FileSystems.newFileSystem(apkFile.toPath(), null as ClassLoader?)
9595

9696
val dig = MessageDigest.getInstance("SHA1")
9797
val digests: MutableMap<String, String> = LinkedHashMap()

0 commit comments

Comments
 (0)