Skip to content

Commit

Permalink
Fix AP file writer issue with non-file outputs, fixed #622
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumfrey committed Jun 7, 2024
1 parent 2d14c78 commit a362aad
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URI;
import java.util.List;

import javax.annotation.processing.Filer;
import javax.annotation.processing.FilerException;
import javax.tools.FileObject;
import javax.tools.JavaFileManager.Location;
import javax.tools.StandardLocation;

import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorRemappable;
Expand Down Expand Up @@ -129,7 +134,9 @@ public void write() {

try {
writer = this.newWriter(this.outRefMapFileName, "refmap");
this.refMapper.write(writer);
if (writer != null) {
this.refMapper.write(writer);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
Expand All @@ -154,9 +161,26 @@ private PrintWriter newWriter(String fileName, String description) throws IOExce
return new PrintWriter(outFile);
}

FileObject outResource = this.ap.getProcessingEnvironment().getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", fileName);
this.ap.printMessage(MessageType.INFO, "Writing " + description + " to " + new File(outResource.toUri()).getAbsolutePath());
return new PrintWriter(outResource.openWriter());
try {
Filer filer = this.ap.getProcessingEnvironment().getFiler();
FileObject outResource = null;
try {
outResource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", fileName);
} catch (Exception ex) {
// fileName is not a valid relative path?
outResource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", new File(fileName).getName());
}

URI resourceUri = outResource.toUri();
String absolutePath = "file".equals(resourceUri.getScheme()) ? new File(resourceUri).getAbsolutePath() : resourceUri.toString();
PrintWriter writer = new PrintWriter(outResource.openWriter());
this.ap.printMessage(MessageType.INFO, "Writing " + description + " to (" + resourceUri.getScheme() + ") " + absolutePath);
return writer;
} catch (Exception ex) {
this.ap.printMessage(MessageType.ERROR, "Cannot write " + description + " to (" + fileName + "): " + ex.getClass().getName()
+ ": " + ex.getMessage());
return null;
}
}

/* (non-Javadoc)
Expand Down

0 comments on commit a362aad

Please sign in to comment.