jdk6.0 compiler api strange behaviour (junit test case+code)
Hello,
i'm giving a first try to new jdk6 compiler api.
Consider the following test case:
import junit.framework.TestCase;
publicclass PojoCompilerTestextends TestCase{
private String pojoSrc ="public class Foo { "
+"private String name; "
+"Foo(){this.name = \"foo\";} "
+"public String getName(){return this.name;} "
+"public void setName(String name){this.name = name;} "
+"}";
private PojoCompiler pojoCompiler;
protectedvoid setUp()throws Exception{
super.setUp();
this.pojoCompiler =new PojoCompilerImpl();
}
publicvoid testCompilePojo()throws ClassNotFoundException, InstantiationException, IllegalAccessException{
Class clazz = this.pojoCompiler.compile("Foo", this.pojoSrc);
assertNotNull("Loaded class shoult not be null", clazz);
}
}
here is the class-under-test:
publicclass PojoCompilerImplimplements PojoCompiler{
private JavaCompiler compiler;
private DiagnosticCollector<JavaFileObject> diagnostic;
private StandardJavaFileManager fileManager;
public PojoCompilerImpl(){
this.compiler = ToolProvider.getSystemJavaCompiler();
this.diagnostic =new DiagnosticCollector<JavaFileObject>();
this.fileManager = this.compiler.getStandardFileManager(this.diagnostic, null,null);
}
/* (non-Javadoc)
* @see it.chi.infopipe.nr.model.output.PojoCompiler#compile(java.lang.String, java.lang.String)
*/
public Class compile(String className, String source)throws ClassNotFoundException, InstantiationException, IllegalAccessException{
JavaSourceFromString pojo =new JavaSourceFromString(className, source);
Iterable<?extends JavaFileObject> compilationUnits=
Arrays.asList(new JavaSourceFromString[]{pojo});
Iterable<String> options = Arrays.asList(new String[]{"-verbose","-Xlint"});
boolean compilationErrors =
compiler.getTask(null, fileManager, this.diagnostic, options, null, compilationUnits).call();
for (Diagnostic diagnostic : this.diagnostic.getDiagnostics()){
System.out.format("Error on line %d%s%n", diagnostic.getLineNumber(), diagnostic);
}
Class clazz = ClassLoader.getSystemClassLoader().loadClass("Foo");
clazz.newInstance();
return clazz;
}
/**
* A file object used to represent source coming from a string.
* (from javadoc)
*/
publicclass JavaSourceFromStringextends SimpleJavaFileObject{
/**
* The source code of this "file".
*/
final String code;
/**
* Constructs a new JavaSourceFromString.
* @param name the name of the compilation unit represented by this file object
* @param code the source code for the compilation unit represented by this file object
*/
JavaSourceFromString(String name, String code){
super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension),
Kind.SOURCE);
this.code = code;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors){
return code;
}
}
}
the test case fails, but i don't get any compiler error shown or others. this is the ouput to stderr:
[parsing started string:///Foo.java from JavaSourceFromString]
[parsing completed 53ms]
[loading /usr/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]
[loading /usr/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]
[checking Foo]
[loading /usr/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/io/Serializable.class)]
[wrote Foo.class]
[total 587ms]
i'm using :
java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b97)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b97, mixed mode, sharing)
on linux-i586
any idea ?
thanks,
Valerio

