mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
support content loading for resource files from output roots (needed by some annotation processors)
This commit is contained in:
@@ -39,6 +39,8 @@ class JavacFileManager extends ForwardingJavaFileManager<StandardJavaFileManager
|
||||
private final Context myContext;
|
||||
private final Collection<JavaSourceTransformer> mySourceTransformers;
|
||||
private Map<File, Set<File>> myOutputsMap = Collections.emptyMap();
|
||||
@Nullable
|
||||
private String myEncodingName;
|
||||
|
||||
interface Context {
|
||||
boolean isCanceled();
|
||||
@@ -64,6 +66,38 @@ class JavacFileManager extends ForwardingJavaFileManager<StandardJavaFileManager
|
||||
myOutputsMap = outputDirToSrcRoots;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleOption(String current, final Iterator<String> remaining) {
|
||||
if ("-encoding".equalsIgnoreCase(current) && remaining.hasNext()) {
|
||||
final String encoding = remaining.next();
|
||||
myEncodingName = encoding;
|
||||
return super.handleOption(current, new Iterator<String>() {
|
||||
private boolean encodingConsumed = false;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !encodingConsumed || remaining.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String next() {
|
||||
if (!encodingConsumed) {
|
||||
encodingConsumed = true;
|
||||
return encoding;
|
||||
}
|
||||
return remaining.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
if (encodingConsumed) {
|
||||
remaining.remove();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return super.handleOption(current, remaining);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String inferBinaryName(Location location, JavaFileObject file) {
|
||||
return super.inferBinaryName(location, unwrapFileObject(file));
|
||||
@@ -194,7 +228,7 @@ class JavacFileManager extends ForwardingJavaFileManager<StandardJavaFileManager
|
||||
}
|
||||
}
|
||||
final File file = (dir == null? new File(fileName).getAbsoluteFile() : new File(dir, fileName));
|
||||
return new OutputFileObject(myContext, dir, fileName, file, kind, className, src != null? src.toUri() : null);
|
||||
return new OutputFileObject(myContext, dir, fileName, file, kind, className, src != null? src.toUri() : null, myEncodingName);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.jetbrains.jps.javac;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jps.api.RequestFuture;
|
||||
import org.jetbrains.jps.client.SimpleProtobufClient;
|
||||
import org.jetbrains.jps.client.UUIDGetter;
|
||||
@@ -42,7 +43,7 @@ public class JavacServerClient extends SimpleProtobufClient<JavacServerResponseH
|
||||
}
|
||||
|
||||
public RequestFuture<JavacServerResponseHandler> sendCompileRequest(List<String> options, Collection<File> files, Collection<File> classpath, Collection<File> platformCp, Collection<File> sourcePath, Map<File, Set<File>> outs, DiagnosticOutputConsumer diagnosticSink, OutputFileConsumer outputSink) {
|
||||
final JavacServerResponseHandler rh = new JavacServerResponseHandler(diagnosticSink, outputSink);
|
||||
final JavacServerResponseHandler rh = new JavacServerResponseHandler(diagnosticSink, outputSink, getEncodingName(options));
|
||||
final JavacRemoteProto.Message.Request request = JavacProtoUtil.createCompilationRequest(options, files, classpath, platformCp, sourcePath, outs);
|
||||
return sendRequest(request, rh, new RequestFuture.CancelAction<JavacServerResponseHandler>() {
|
||||
@Override
|
||||
@@ -61,4 +62,17 @@ public class JavacServerClient extends SimpleProtobufClient<JavacServerResponseH
|
||||
return sendMessage(requestId, JavacProtoUtil.toMessage(requestId, request), responseHandler, cancelAction);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getEncodingName(List<String> options) {
|
||||
boolean found = false;
|
||||
for (String option : options) {
|
||||
if (found) {
|
||||
return option;
|
||||
}
|
||||
if ("-encoding".equalsIgnoreCase(option)) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,12 @@ package org.jetbrains.jps.javac;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.MessageLite;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jps.client.ProtobufResponseHandler;
|
||||
import org.jetbrains.jps.incremental.BinaryContent;
|
||||
|
||||
import javax.tools.*;
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.JavaFileObject;
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.util.Collection;
|
||||
@@ -33,11 +35,14 @@ import java.util.Locale;
|
||||
public class JavacServerResponseHandler implements ProtobufResponseHandler{
|
||||
private final DiagnosticOutputConsumer myDiagnosticSink;
|
||||
private final OutputFileConsumer myOutputSink;
|
||||
@Nullable
|
||||
private final String myEncodingName;
|
||||
private volatile boolean myTerminatedSuccessfully;
|
||||
|
||||
public JavacServerResponseHandler(DiagnosticOutputConsumer diagnosticSink, OutputFileConsumer outputSink) {
|
||||
public JavacServerResponseHandler(DiagnosticOutputConsumer diagnosticSink, OutputFileConsumer outputSink, @Nullable final String encodingName) {
|
||||
myDiagnosticSink = diagnosticSink;
|
||||
myOutputSink = outputSink;
|
||||
myEncodingName = encodingName;
|
||||
}
|
||||
|
||||
public boolean handleMessage(MessageLite message) throws Exception {
|
||||
@@ -93,7 +98,7 @@ public class JavacServerResponseHandler implements ProtobufResponseHandler{
|
||||
convertKind(kind),
|
||||
outputObject.hasClassName()? outputObject.getClassName() : null,
|
||||
srcUri,
|
||||
fileObjectContent
|
||||
myEncodingName, fileObjectContent
|
||||
);
|
||||
|
||||
myOutputSink.save(fileObject);
|
||||
|
||||
@@ -15,12 +15,14 @@
|
||||
*/
|
||||
package org.jetbrains.jps.javac;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtilRt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jps.incremental.BinaryContent;
|
||||
import org.jetbrains.jps.incremental.Utils;
|
||||
|
||||
import javax.tools.*;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.SimpleJavaFileObject;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
|
||||
@@ -40,12 +42,28 @@ public final class OutputFileObject extends SimpleJavaFileObject {
|
||||
@Nullable private final URI mySourceUri;
|
||||
private volatile BinaryContent myContent;
|
||||
private final File mySourceFile;
|
||||
private final String myEncodingName;
|
||||
|
||||
public OutputFileObject(@NotNull JavacFileManager.Context context, @Nullable File outputRoot, String relativePath, @NotNull File file, @NotNull Kind kind, @Nullable String className, @Nullable final URI sourceUri) {
|
||||
this(context, outputRoot, relativePath, file, kind, className, sourceUri, null);
|
||||
public OutputFileObject(@NotNull JavacFileManager.Context context,
|
||||
@Nullable File outputRoot,
|
||||
String relativePath,
|
||||
@NotNull File file,
|
||||
@NotNull Kind kind,
|
||||
@Nullable String className,
|
||||
@Nullable final URI sourceUri,
|
||||
@Nullable final String encodingName) {
|
||||
this(context, outputRoot, relativePath, file, kind, className, sourceUri, encodingName, null);
|
||||
}
|
||||
|
||||
public OutputFileObject(@Nullable JavacFileManager.Context context, @Nullable File outputRoot, String relativePath, @NotNull File file, @NotNull Kind kind, @Nullable String className, @Nullable final URI srcUri, @Nullable BinaryContent content) {
|
||||
public OutputFileObject(@Nullable JavacFileManager.Context context,
|
||||
@Nullable File outputRoot,
|
||||
String relativePath,
|
||||
@NotNull File file,
|
||||
@NotNull Kind kind,
|
||||
@Nullable String className,
|
||||
@Nullable final URI srcUri,
|
||||
@Nullable final String encodingName,
|
||||
@Nullable BinaryContent content) {
|
||||
super(Utils.toURI(file.getPath()), kind);
|
||||
myContext = context;
|
||||
mySourceUri = srcUri;
|
||||
@@ -55,6 +73,7 @@ public final class OutputFileObject extends SimpleJavaFileObject {
|
||||
myFile = file;
|
||||
myClassName = className != null? className.replace('/', '.') : null;
|
||||
mySourceFile = srcUri != null? Utils.convertToFile(srcUri) : null;
|
||||
myEncodingName = encodingName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -116,10 +135,10 @@ public final class OutputFileObject extends SimpleJavaFileObject {
|
||||
@Override
|
||||
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
||||
final BinaryContent content = myContent;
|
||||
if (content == null) {
|
||||
throw new FileNotFoundException(toUri().getPath());
|
||||
if (content != null) {
|
||||
return new String(content.getBuffer(), content.getOffset(), content.getLength());
|
||||
}
|
||||
return new String(content.getBuffer(), content.getOffset(), content.getLength());
|
||||
return FileUtilRt.loadFile(myFile, myEncodingName, false);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
Reference in New Issue
Block a user