Skip to content

Commit 0b033f1

Browse files
committed
Merge pull request #126 from scijava/javac-annotations
Fix annotation processing when running javac without -d <directory>
2 parents 93c340f + c637b29 commit 0b033f1

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

src/main/java/org/scijava/annotations/AnnotationProcessor.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
package org.scijava.annotations;
3333

3434
import java.io.ByteArrayOutputStream;
35+
import java.io.File;
3536
import java.io.FileNotFoundException;
37+
import java.io.FileOutputStream;
3638
import java.io.IOException;
3739
import java.io.InputStream;
3840
import java.io.OutputStream;
@@ -62,6 +64,7 @@
6264
import javax.lang.model.util.Elements;
6365
import javax.lang.model.util.Types;
6466
import javax.tools.Diagnostic.Kind;
67+
import javax.tools.FileObject;
6568
import javax.tools.StandardLocation;
6669

6770
import 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

Comments
 (0)