1717import java .util .zip .ZipFile ;
1818
1919public 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