Fix[JNA]: use agent to replace class at runtime
Fixes duplicate module package on recent Forge versions.
This commit is contained in:
1 parent
4a789ab98c
commit
6eb7ecdde3
5 files changed
+47
-2
No files matched your search
@@ -0,0 +1,2 @@
|
||||
Agent-Class: net.kdt.patchjna.PatchJNAAgent
|
||||
Premain-Class: net.kdt.patchjna.PatchJNAAgent
|
||||
File renamed without changes.
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user