Java Compilers

[quote]I haven’t investigated it yet, but hopefully its not too hard to use.
[/quote]
See and judge for yourself.

Test code:

public class Test {

    private int hidden = 99;

    public getHiddenVariable() {
        return hidden;
    }

    public static void main(String argv[]) {
        System.out.println("Hidden variable:" + new Test().getHiddenVariable());
    }

}

Compiler code:

public class CompileTest {

	public static void main(String[] args) {

		File[] files = { new File("C:/java/Test.java") };

		JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool();
		StandardJavaFileManager fileManager = compiler
				.getStandardFileManager(null);

		Iterable<? extends JavaFileObject> compilationUnits = fileManager
				.getJavaFileObjectsFromFiles(Arrays.asList(files));
		compiler.getTask(null, fileManager, null, null, null, compilationUnits)
				.run();

		try {
			fileManager.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


And standard output:

C:\java\Test.java:5: invalid method declaration; return type required
    public getHiddenVariable() {
           ^
1 error

Nice error messages as well!

I dont really understand is it possible to demo… or to explain further? So sorry…

Sure. When writing a fractal rendering program some time ago I supported the Mandelbrot set which is a quite beautiful nowhere differentiable set of complex numbers (google it!). It is based on a complex sequence [n+1] = z[n]^2 + c , for all natural numbers n, and the Mandelbrot set is the set of all complex c such that this sequence (with z[0] = 0) converges.

However it is also funny to tinker with this formula, changing the power for example or doing lots of other things. Therefore it would be cool if the user could enter a custom formula at runtime (or similarly just write a java file in a directory without the need to compile the program) to see how this stuff affects the fractal.

The logical ‘nice’ way to do it is to translate such an expression into some complex algebra using a predefined Complex class with add(), multiply() methods and so on, but doing this results in a performance degradation of 4-5 times! This is why real compilation and use of primitives is useful.

Thanks for showing me that benjamin, looks like a peice of cake! :slight_smile:

Some naive questions:
If all of this is provided in the java runtime client VM, do we still need the JDK (except for its other tools)?
Will this allow java viruses to take off since they can so easily modify themselves?

I believe that jsr 199 will only be included into jdk and thereby not introduce any new security risks.

Rats, I was under the impression that it would be in the standard jre library, I didn’t know it would be built on the JDK’s javac command. http://jcp.org/en/jsr/detail?id=199

That’s a bummer since who wants to distribute a JDK to get this feature? Looks like I’ll have to find out about Eclipse’s JDT and the others mentioned to dynamically compile code.

What about the scripting side of things? Do you know if that API will come with the regular JRE?

Yes jsr 223 should be included in the jre as well. Currently mustang is only shipped with Rhino though other scripting languages support this specifiation as well.

Well, there is… y’know… janino.

Great, thanks for the link. Pity it doesn’t support Java 1.5 stuff yet, but 1.4 is good enough! :slight_smile:

I didn’t see anything about Rhino, but in the JSR it looks like scripting will not be on the regular JRE either -
from http://jcp.org/en/jsr/detail?id=223:

2.2 What is the target Java platform? (i.e., desktop, server, personal, embedded, card, etc.)
The target platform is the same as that of Java Servlets, specifically JavaTM 2 Platform, Enterprise Edition (J2EE) 1.4.

Mozilla Rhino javascript will be available in the latest mustang. Try to google : mozilla rhino mustang for more information.

To make sure that the scripting will ended be shipped in jre I unzipped rt.jar and found the javax.script package… So the scripting will be included in jre and if it says otherwise in the official jsr page then I guess it is not up-to-date.