Reputation: 6373
Is it possible in Java to give a class or even its functions a new name at run time. By reading in the new names as arguments or on a configuration file when the program is started?
UPDATE:
Here is the purpose of this. I am using Java Script Engine to allow any JSR 223 compatible Scripting Language to access our API. Some of our clients are not used to using Java and it's naming conventions and would feel more comfortable using their own specific naming conventions. So I am required to give them the capability to dynamically change the API's class and function names without actually changing them in the code. It was suggested I use a Map and some sort of binding with a string name and the actual Java name e.g.,
map.put("Hello",HelloWorld.class)
Object obj = new Hello();
which should be the same as,
Object obj = new HelloWorld();
If this is not possible please tell me why. I need a solid Java expertise answer. This is out of my league and I need facts to tell people why this is not possible even though myself I am almost sure it's not possible.
Possible Solution:
Here is the closets solution I have come up with. Using this link, https://weblogs.java.net/blog/2005/08/10/reflection-and-dynamically-changing-classes I could add in the names at run time, use composition to create an Adapter Class, and then compile the file, and voila the Script Language folks could use their defined names instead of my API's Java names.
Is this the only conceivable way to accomplish this?
UPDATE 2:
Here is another possible solution for anyone trying this too,
http://asm.ow2.org/doc/faq.html
That'll take you directly to their frequently asked questions which will have one for this exact problem.
Upvotes: 4
Views: 4913
Reputation: 291
Yes, you are able to do that using javassist. In particular, you have to edit NewExpr.
Upvotes: -1
Reputation: 1900
No, you will need to refactor for references and recompile for execution.
Upvotes: 2