If you have no choice, you absolutely need to keep all existing class names with their exact name ( as indicated in your comment , to my previous answer), then you should go with AspectJ.
Consider this class:
public class UnmodifyableClassWithMain { public static void main(String[] args) { System.out.println("In main"); new ClassUsingArgumentRegistry(); } }
First, you need something that contains command line arguments. For simplicity, I will use a simple class with a static field:
public class ArgumentRegistry { public static String[] ARGS; }
Then you need to define an Aspect that intercepts calls to the main one and stores the arguments.
public aspect StoreArgumentsOfMain { pointcut mainMethod(String[] arguments): execution(void main(String[])) && args(arguments); before(String[] arguments): mainMethod(arguments) { System.out.println("Storing arguments"); ArgumentRegistry.ARGS = arguments; } }
To test this, I also created the ClassUsingArgumentRegistry class:
public class ClassUsingArgumentRegistry { public ClassUsingArgumentRegistry() { System.out.println("Arguments: " + java.util.Arrays.toString(ArgumentRegistry.ARGS)); } }
What is it. If I turn on AspectJ time compilation and run the result using "java UnmodifyableClassWithMain Foo Bar Baz", I get the following output:
Storing arguments In main Arguments: [foo, bar, baz]
source share