Is there a recommended way of generating jtreg java test source?

Jonathan Gibbons jonathan.gibbons at
Wed Oct 15 21:17:36 UTC 2014

On 10/15/2014 01:04 PM, Martin Buchholz wrote:
> Occasionally, one would like to generate the java sources to be tested 
> instead of checking them in (e.g. the generated source files may be 
> huge).  Is there a way to do that?
> Ideally, you would want to have some java code be @run to create the 
> source files, and those could then in turn be @compile'd and @run'd, 
> but that does not seem to be possible.


We routinely do this in the OpenJDK langtools/test test suite, where we 
have a style of testing that generates hundreds, sometimes thousands of 
classes on the fly, to provide the test cases for a feature.

You can generate the Java source code in memory, and then use the Java 
Compiler API to compile it ( either to class 
files in memory or to class files on disk, depending on your ultimate 
usage.  Note if you compile to memory, you can execute the classes using 
an appropriate ClassLoader.

But, this all has to be done within a test class -- there is no inherent 
support in jtreg to @compile and @run generated stuff.

You can look at the file langtools/test/tools/lib/ for a 
utility class to facilitate creating in memory source objects, compiling 
them, and so on.  We wrote and used this class to replace many of the 
remaining shell tests in the langtools repo.

-- Jon

More information about the jtreg-use mailing list