-
-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix[JNA]: use agent to replace class at runtime
Fixes duplicate module package on recent Forge versions.
- Loading branch information
1 parent
4a789ab
commit 6eb7ecd
Showing
5 changed files
with
47 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Agent-Class: net.kdt.patchjna.PatchJNAAgent | ||
Premain-Class: net.kdt.patchjna.PatchJNAAgent |
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
JavaApp/src/patchjna_agent/net/kdt/patchjna/PatchJNAAgent.java
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package net.kdt.patchjna; | ||
|
||
import java.io.*; | ||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.lang.instrument.Instrumentation; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.security.ProtectionDomain; | ||
|
||
public class PatchJNAAgent implements ClassFileTransformer { | ||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, | ||
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { | ||
byte[] transformeredByteCode = classfileBuffer; | ||
if (className.equals("com/sun/jna/Platform")) { | ||
System.out.println("PatchJNAAgent: Replacing class"); | ||
try { | ||
InputStream inputStream = PatchJNAAgent.class.getClassLoader().getResourceAsStream("com/sun/jna/Platform.class.patch"); | ||
transformeredByteCode = new byte[inputStream.available()]; | ||
DataInputStream dataInputStream = new DataInputStream(inputStream); | ||
dataInputStream.readFully(transformeredByteCode); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return transformeredByteCode; | ||
} | ||
|
||
public static void premain(String args, Instrumentation instrumentation) { | ||
System.out.println("PatchJNAAgent: premain called"); | ||
instrumentation.addTransformer(new PatchJNAAgent()); | ||
} | ||
} |
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