Posts Tagged ‘reflection’

How to create a dynamic class “on the fly” :

com.sun.tools.javac.Main class has two methods which help us to invoke the compiler from a program. It will behave like javac command.
public static int compile(String []args);
public static int compile(String []args , PrintWriter out);

args-> command line arguments that would be normally be passed on the javac program.
out-> indicates where the compiler’s output is directed.
return (int) -> exit value

I would like to share a simple program which will create a java file on the fly (ie at Runtime) and it will be compiled using compile method provided by this class com.sun.tools.javac.Main.Once itsĀ  been compiled,using reflection apis we will invoke the method in the class(which is created at Runtime).. (more…)

Read Full Post »

We can access private variable of super class or any class using reflection api. I have created a sample class which will change the value of private variable to the new value. You need to set accessible flag as true in setAccessible() method of Field reflection class, then only it will allow us to read/change the private variable . There is one more way to suppress the access using SecurityManager class.

Read Full Post »