The Generic Specialization is an advanced Java VM and Java Language feature incubated by the project Valhalla, planed for a futur version of Java SE.
The Generic Specialization aims to allow the use of primitive type with generics by specializing them at runtime. The two principal values are :
- Suppression of the boxing
- Suppression of pseudo-specialized types such as
IntStream,ToIntFunction...
The goal of the retroSpecialization project is to back port the Generic Specialization as described in the project Valhalla to Java 7 and 8. The project is more a rewriter than a compiler since Java files have to first be compiled with the Valhalla's compiler and then rewritten by the retroSpecialization executable to be executed on Java 7 and 8 VM.
class Box<any T> {
private final T t;
public Box(T t) { this.t = t; }
public T get() { return t; }
}
class Main {
public static void main(String[] args) {
Box<float> box = new Box<float>(3.4f);
System.out.println(box.get());
}
}
The project RetroSpecialization can be run from the distribution files, or directly from
a modified code by using the ant file.
In order to back port a java project containing generics instantiated with primitive types, the project has to be compiled by the Valhalla project's JDK (steps are described in the section Source Code and Building Valhalla) and respect the syntax imposed.
The output of the first step should be all the java files compiled into class files.
Download the files present in the dist directory
which consist in the Backport.jar and the rt folder.
The rt folder contains necessary files copied into the rewritten project in order
to perform the generics specialization at runtime.
Run the Backport, using Java 7+ on the class files produced by the Valhalla JDK.
Run java -jar Backport.jar -Dfolder=folder.
The output of the rewriting will be localized inside a folder named backport_result.
An ant build is available to run the project from the source code.
Run ant -Dfolder={Folder to rewrite} run to run the project.
Run ant help to get the ant usage.
- Does not support the inheritance
- Only supported by the OpenJDK's VM