Skip to content

Commit 90ab4d2

Browse files
committed
Invoke premain method
1 parent 90e8e5c commit 90ab4d2

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.azisaba</groupId>
88
<artifactId>Log4j2Fix</artifactId>
9-
<version>1.0.1</version>
9+
<version>1.0.2</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>

src/main/java/net/azisaba/log4j2Fix/Log4j2Fix.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import java.util.zip.ZipFile;
1818

1919
public class Log4j2Fix {
20+
private static String arg;
21+
private static Instrumentation inst;
22+
2023
public static void main(String[] args) throws IOException {
2124
transformClasses();
2225
List<String> arguments = new ArrayList<>(Arrays.asList(args));
@@ -25,6 +28,7 @@ public static void main(String[] args) throws IOException {
2528
return;
2629
}
2730
String main = arguments.remove(0);
31+
String premain = null;
2832
File file = new File(main);
2933
if (file.exists()) {
3034
System.out.println("Using " + file.getAbsolutePath() + " for classpath");
@@ -45,7 +49,8 @@ public static void main(String[] args) throws IOException {
4549
if (read.startsWith("Main-Class: ")) {
4650
main = read.replace("Main-Class: ", "");
4751
found = true;
48-
break;
52+
} else if (read.startsWith("Premain-Class")) {
53+
premain = read.replace("Premain-Class: " , "");
4954
}
5055
}
5156
reader.close();
@@ -58,6 +63,16 @@ public static void main(String[] args) throws IOException {
5863
}
5964
}
6065
}
66+
if (premain != null) {
67+
try {
68+
Class<?> clazz = Class.forName(premain);
69+
Method m = clazz.getMethod("premain", String.class, Instrumentation.class);
70+
m.invoke(null, arg, inst);
71+
} catch (ReflectiveOperationException e) {
72+
System.err.println("Failed to invoke premain method of class " + premain);
73+
e.printStackTrace();
74+
}
75+
}
6176
try {
6277
Class<?> clazz = Class.forName(main);
6378
Method m = clazz.getMethod("main", String[].class);
@@ -70,10 +85,14 @@ public static void main(String[] args) throws IOException {
7085

7186
public static void agentmain(String args, Instrumentation instrumentation) throws IOException {
7287
transformClasses();
88+
arg = args;
89+
inst = instrumentation;
7390
}
7491

7592
public static void premain(String args, Instrumentation instrumentation) throws IOException {
7693
transformClasses();
94+
arg = args;
95+
inst = instrumentation;
7796
}
7897

7998
public static void transformClasses() throws IOException {

0 commit comments

Comments
 (0)