forked from j-hc/revanced-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ClassLoader not working with Java 9+
- Loading branch information
Showing
6 changed files
with
26 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,10 @@ | ||
package app.revanced.patch | ||
|
||
import java.io.File | ||
import java.net.URL | ||
import java.net.URLClassLoader | ||
|
||
internal class PatchLoader { | ||
internal companion object { | ||
internal fun injectPatches(file: File) { | ||
// This function will fail on Java 9 and above. | ||
try { | ||
val url = file.toURI().toURL() | ||
val classLoader = Thread.currentThread().contextClassLoader as URLClassLoader | ||
val method = URLClassLoader::class.java.getDeclaredMethod("addURL", URL::class.java) | ||
method.isAccessible = true | ||
method.invoke(classLoader, url) | ||
} catch (e: Exception) { | ||
throw Exception( | ||
"Failed to inject patches! The CLI does NOT work on Java 9 and above, please use Java 8!", | ||
e // propagate exception | ||
) | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
package app.revanced.patch | ||
|
||
import app.revanced.patcher.data.base.Data | ||
import app.revanced.patcher.patch.base.Patch | ||
import app.revanced.patches.Index | ||
import java.io.File | ||
import java.net.URLClassLoader | ||
|
||
internal class Patches { | ||
internal companion object { | ||
// You may ask yourself, "why do this?". | ||
// We do it like this, because we don't want the Index class | ||
// to be loaded while the dependency hasn't been injected yet. | ||
// You can see this as "controlled class loading". | ||
// Whenever this class is loaded (because it is invoked), all the imports | ||
// will be loaded too. We don't want to do this until we've injected the class. | ||
internal fun loadPatches() = Index.patches | ||
internal object Patches { | ||
|
||
|
||
/** | ||
* This method loads patches from a given patch file | ||
* @return the loaded patches represented as a list of functions returning instances of [Patch] | ||
*/ | ||
internal fun load(patchFile: File): List<() -> Patch<Data>> { | ||
val url = patchFile.toURI().toURL() | ||
val classLoader = URLClassLoader(arrayOf(url)) | ||
return loadIndex(classLoader).patches | ||
} | ||
} | ||
private fun loadIndex(classLoader: ClassLoader) = classLoader | ||
.loadClass(Index::class.java.canonicalName) | ||
.fields | ||
.first() | ||
.get(null) as Index | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters