3232package org .scijava .annotations ;
3333
3434import java .io .ByteArrayOutputStream ;
35+ import java .io .File ;
3536import java .io .FileNotFoundException ;
37+ import java .io .FileOutputStream ;
3638import java .io .IOException ;
3739import java .io .InputStream ;
3840import java .io .OutputStream ;
6264import javax .lang .model .util .Elements ;
6365import javax .lang .model .util .Types ;
6466import javax .tools .Diagnostic .Kind ;
67+ import javax .tools .FileObject ;
6568import javax .tools .StandardLocation ;
6669
6770import org .scijava .annotations .AbstractIndexWriter .StreamFactory ;
@@ -90,14 +93,14 @@ public boolean process(final Set<? extends TypeElement> elements,
9093 try {
9194 writer .write (writer );
9295 }
93- catch (IOException e ) {
96+ catch (final IOException e ) {
9497 final ByteArrayOutputStream out = new ByteArrayOutputStream ();
9598 e .printStackTrace (new PrintStream (out ));
9699 try {
97100 out .close ();
98101 processingEnv .getMessager ().printMessage (Kind .ERROR , out .toString ());
99102 }
100- catch (IOException e2 ) {
103+ catch (final IOException e2 ) {
101104 processingEnv .getMessager ().printMessage (Kind .ERROR ,
102105 e2 .getMessage () + " while printing " + e .getMessage ());
103106 }
@@ -152,8 +155,9 @@ public void add(final TypeElement element) {
152155 }
153156
154157 @ SuppressWarnings ("unchecked" )
155- private Map <String , Object > adapt (List <? extends AnnotationMirror > mirrors ,
156- TypeMirror annotationType )
158+ private Map <String , Object > adapt (
159+ final List <? extends AnnotationMirror > mirrors ,
160+ final TypeMirror annotationType )
157161 {
158162 final Map <String , Object > result = new TreeMap <String , Object >();
159163 for (final AnnotationMirror mirror : mirrors ) {
@@ -208,7 +212,8 @@ else if (o instanceof VariableElement) {
208212 }
209213
210214 private AnnotationMirror getMirror (final TypeElement element ) {
211- for (AnnotationMirror candidate : utils .getAllAnnotationMirrors (element ))
215+ for (final AnnotationMirror candidate : utils
216+ .getAllAnnotationMirrors (element ))
212217 {
213218 final Name binaryName =
214219 utils .getBinaryName ((TypeElement ) candidate .getAnnotationType ()
@@ -221,7 +226,9 @@ private AnnotationMirror getMirror(final TypeElement element) {
221226 }
222227
223228 @ Override
224- public InputStream openInput (String annotationName ) throws IOException {
229+ public InputStream openInput (final String annotationName )
230+ throws IOException
231+ {
225232 try {
226233 return filer .getResource (StandardLocation .CLASS_OUTPUT , "" ,
227234 Index .INDEX_PREFIX + annotationName ).openInputStream ();
@@ -232,16 +239,37 @@ public InputStream openInput(String annotationName) throws IOException {
232239 }
233240
234241 @ Override
235- public OutputStream openOutput (String annotationName ) throws IOException {
242+ public OutputStream openOutput (final String annotationName )
243+ throws IOException
244+ {
236245 final List <Element > originating = originatingElements .get (annotationName );
237- return filer .createResource (StandardLocation .CLASS_OUTPUT , "" ,
238- Index .INDEX_PREFIX + annotationName ,
239- originating .toArray (new Element [originating .size ()]))
240- .openOutputStream ();
246+ final String path = Index .INDEX_PREFIX + annotationName ;
247+ final FileObject fileObject =
248+ filer .createResource (StandardLocation .CLASS_OUTPUT , "" , path ,
249+ originating .toArray (new Element [originating .size ()]));
250+
251+ /*
252+ * Verify that the generated file is in the META-INF/json/ subdirectory;
253+ * Despite our asking for it explicitly, the DefaultFileManager will
254+ * strip out the directory if javac was called without an explicit
255+ * output directory (i.e. without <code>-d</code> option).
256+ */
257+ final String uri = fileObject .toUri ().toString ();
258+ if (uri != null && uri .endsWith ("/" + path )) {
259+ return fileObject .openOutputStream ();
260+ }
261+ final String prefix =
262+ uri .substring (0 , uri .length () - annotationName .length ());
263+ final File file = new File (prefix + path );
264+ final File parent = file .getParentFile ();
265+ if (parent != null && !parent .isDirectory () && !parent .mkdirs ()) {
266+ throw new IOException ("Could not create directory: " + parent );
267+ }
268+ return new FileOutputStream (file );
241269 }
242270
243271 @ Override
244- public boolean isClassObsolete (String className ) {
272+ public boolean isClassObsolete (final String className ) {
245273 return false ;
246274 }
247275
0 commit comments