In case you don't have a choice any you absolutely have to retain all the existing class names with their exact name (as stated in your comment to my previous answer), then you have to go with AspectJ.
Let's consider we have this class:
public class UnmodifyableClassWithMain {
public static void main(String[] args) {
System.out.println("In main");
new ClassUsingArgumentRegistry();
}
}
First, you need to something that holds the command line arguments. I will use a simple class with a static field for simplicity:
public class ArgumentRegistry {
public static String[] ARGS;
}
Then, you need you define an Aspect that intercepts calls to main and stores the arguments.
public aspect StoreArgumentsOfMain {
/**
* This pointcut intercepts all calls to methods called main with a string array as
* argument.
*/
pointcut mainMethod(String[] arguments): execution(void main(String[])) && args(arguments);
/**
* Before the original main method gets called, store the arguments in the registry.
*/
before(String[] arguments): mainMethod(arguments) {
System.out.println("Storing arguments");
ArgumentRegistry.ARGS = arguments;
}
}
For trying it out, I also created a ClassUsingArgumentRegistry:
public class ClassUsingArgumentRegistry {
public ClassUsingArgumentRegistry() {
System.out.println("Arguments: " + java.util.Arrays.toString(ArgumentRegistry.ARGS));
}
}
That's it. If I enable AspectJ's compile time weaving and run the result using "java UnmodifyableClassWithMain Foo Bar Baz", I get the follwing output:
Storing arguments
In main
Arguments: [foo, bar, baz]