When compiling nested inner classes, there is a certain threshold where compilation times with become exponential with each additional nesting level. This can become a problem in learning environments where the Java Compiler API could be used to compile students scripts in a contained scenario.
The following source can be created with the attached generator class GenOuterJava . The generator expects an outer class name and a maximum nesting level. It then generates a source file with all the nested classes.
java GenOuterLevel JavacStalls 20
The resulting JavacStalls.java can then be processed with Javac or the Compiler API.
Levels from 30 above expose notable compilation times, especially for the relatively small file size of the originating (generated) source file.
public class JavacStalls {
public JavacStalls() {};
private void outerPrivate(String z) {
}
class aa_ extends java.util.ArrayList<Void> {
@Deprecated
public aa_() {};
class ab_ extends aa_ {
@Deprecated
public ab_() {};
class ac_ extends ab_ {
@Deprecated
public ac_() {};
[..]
class as_ extends ar_ {
@Deprecated
public as_() {};
class at_ extends as_ {
@Deprecated
public at_() {};
class Inner {
//A a;
public void innerPublic() {
outerPrivate(Inner.class.toString());
}
}
}}}}}}}}}}}}}}}}}}}} public void outerPublic() {
System.out.println();
}
};
The following source can be created with the attached generator class GenOuterJava . The generator expects an outer class name and a maximum nesting level. It then generates a source file with all the nested classes.
java GenOuterLevel JavacStalls 20
The resulting JavacStalls.java can then be processed with Javac or the Compiler API.
Levels from 30 above expose notable compilation times, especially for the relatively small file size of the originating (generated) source file.
public class JavacStalls {
public JavacStalls() {};
private void outerPrivate(String z) {
}
class aa_ extends java.util.ArrayList<Void> {
@Deprecated
public aa_() {};
class ab_ extends aa_ {
@Deprecated
public ab_() {};
class ac_ extends ab_ {
@Deprecated
public ac_() {};
[..]
class as_ extends ar_ {
@Deprecated
public as_() {};
class at_ extends as_ {
@Deprecated
public at_() {};
class Inner {
//A a;
public void innerPublic() {
outerPrivate(Inner.class.toString());
}
}
}}}}}}}}}}}}}}}}}}}} public void outerPublic() {
System.out.println();
}
};