mercredi 29 juin 2016

How do I get the URI of a JavaFileObject in annotation testing?

I'm using XSLT to generate Java Code with the javax.annotation.processing API. It works well as long as I don't want to test it, which I of course do :) While running the processor, the JavaFileObject.toUri() gives me a "file:/"-URI, which I can use for creating a StreamResult for the Transformer.

This is not the case when testing the class I get, for example /CLASS_OUTPUT/com/example.MyClass.class

JavaFileObject jfo = filer.createSourceFile(className);
String uri = jfo.toUri().toASCIIString();
if (!uri.startsWith("file:/")) { // is test!
     messager.printMessage(Diagnostic.Kind.NOTE, "Not a file URI, we're testing!");
    return;
}
StreamResult result = new StreamResult(uri);
DOMSource src = new DOMSource(modelDoc);
DOMSource xslSource = new DOMSource(loadDocument(xslUri));
TransformerFactory tf = new net.sf.saxon.TransformerFactoryImpl();
Transformer transformer = tf.newTransformer(xslSource);
transformer.transform(src, result);

I have the following questions:

  1. Does the test create a real source file?
  2. If yes, what is its actual location?

Thanks!

Aucun commentaire:

Enregistrer un commentaire