jlahoda closed pull request #442: If compilation during indexing fails on exception, try to fallback to? URL: https://github.com/apache/incubator-netbeans/pull/442
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java b/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java index e7e26ffd5..d7203ad0d 100644 --- a/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java +++ b/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java @@ -59,8 +59,12 @@ import com.sun.tools.javac.util.Names; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; import java.util.*; import java.util.Map.Entry; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.logging.Level; import java.util.stream.Collectors; @@ -72,7 +76,9 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.tools.JavaFileObject; +import javax.tools.StandardLocation; import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.queries.BinaryForSourceQuery; import org.netbeans.api.java.queries.CompilerOptionsQuery; import org.netbeans.api.java.source.ClasspathInfo; import org.netbeans.api.java.source.ElementHandle; @@ -85,8 +91,10 @@ import org.netbeans.modules.parsing.spi.indexing.Context; import org.netbeans.modules.parsing.spi.indexing.Indexable; import org.netbeans.modules.parsing.spi.indexing.SuspendStatus; +import org.openide.filesystems.FileObject; import org.openide.filesystems.FileSystem; import org.openide.filesystems.FileUtil; +import org.openide.filesystems.URLMapper; import org.openide.util.Exceptions; /** @@ -350,7 +358,7 @@ public void run() throws IOException { JavaIndex.LOG.log(Level.FINEST, "VanillaCompileWorker was canceled in root: " + FileUtil.getFileDisplayName(context.getRoot()), ca); //NOI18N } } catch (Throwable t) { - if (t instanceof ThreadDeath) { + if (t instanceof ThreadDeath) { throw (ThreadDeath) t; } else { Level level = t instanceof FatalError ? Level.FINEST : Level.WARNING; @@ -367,8 +375,79 @@ public void run() throws IOException { JavaIndex.LOG.log(level, message, t); //NOI18N } } + //fallback: copy output classes to caches, so that editing is not extremely slow/broken: + BinaryForSourceQuery.Result res2 = BinaryForSourceQuery.findBinaryRoots(context.getRootURI()); + Set<String> filter; + if (!context.isAllFilesIndexing()) { + filter = new HashSet<>(); + for (CompileTuple toIndex : files) { + String path = toIndex.indexable.getRelativePath(); + filter.add(path.substring(0, path.lastIndexOf("."))); + } + } else { + filter = null; + } + try { + final Future<Void> done = FileManagerTransaction.runConcurrent(() -> { + File cache = JavaIndex.getClassFolder(context.getRootURI(), false, false); + for (URL u : res2.getRoots()) { + FileObject binaryFO = URLMapper.findFileObject(u); + if (binaryFO == null) + continue; + FileManagerTransaction fmtx = TransactionContext.get().get(FileManagerTransaction.class); + copyRecursively(binaryFO, cache, cache, filter, fmtx); + } + }); + done.get(); + } catch (IOException | InterruptedException | ExecutionException ex) { + Exceptions.printStackTrace(ex); + } + } + return ParsingOutput.success(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated); + } + + private static void copyRecursively(FileObject source, File targetRoot, File target, Set<String> filter, FileManagerTransaction fmtx) throws IOException { + if (source.isFolder()) { + if (target.exists() && !target.isDirectory()) { + throw new IOException("Cannot create folder: " + target.getAbsolutePath() + ", already exists as a file."); + } + + FileObject[] listed = source.getChildren(); + + for (FileObject f : listed) { + String name = f.getNameExt(); + if (name.endsWith(".class")) + name = name.substring(0, name.length() - FileObjects.CLASS.length()) + FileObjects.SIG; + copyRecursively(f, targetRoot, new File(target, name), filter, fmtx); + } + } else { + if (target.isDirectory()) { + throw new IOException("Cannot create file: " + target.getAbsolutePath() + ", already exists as a folder."); + } + + boolean copy; + + if (filter != null) { + String path = FileObjects.getRelativePath(targetRoot, target); + int dot = path.lastIndexOf('.'); + if (dot != (-1)) path = path.substring(0, dot); + int dollar = path.indexOf('$'); + if (dollar != (-1)) path = path.substring(0, dollar); + copy = filter.contains(path); + } else { + copy = true; + } + + if (copy) + copyFile(source, fmtx.createFileObject(StandardLocation.CLASS_OUTPUT, target, targetRoot, null, null)); + } + } + + private static void copyFile(FileObject updatedFile, JavaFileObject target) throws IOException { + try (InputStream ins = updatedFile.getInputStream(); + OutputStream out = target.openOutputStream()) { + FileUtil.copy(ins, out); } - return ParsingOutput.failure(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated); } private void dropMethodsAndErrors(com.sun.tools.javac.util.Context ctx, CompilationUnitTree cut) { diff --git a/java.source.base/src/org/netbeans/modules/java/source/parsing/FileManagerTransaction.java b/java.source.base/src/org/netbeans/modules/java/source/parsing/FileManagerTransaction.java index 5c52fdce9..c7f841da0 100644 --- a/java.source.base/src/org/netbeans/modules/java/source/parsing/FileManagerTransaction.java +++ b/java.source.base/src/org/netbeans/modules/java/source/parsing/FileManagerTransaction.java @@ -94,7 +94,7 @@ public final boolean canWrite() { * @return */ @NonNull - abstract JavaFileObject createFileObject( + public abstract JavaFileObject createFileObject( @NonNull Location location, @NonNull File file, @NonNull File root, @@ -229,7 +229,7 @@ protected void rollBack() throws IOException { @Override @NonNull - JavaFileObject createFileObject( + public JavaFileObject createFileObject( @NonNull final Location location, @NonNull final File file, @NonNull final File root, @@ -261,7 +261,7 @@ public void delete (@NonNull final File file) { @Override @NonNull - JavaFileObject createFileObject( + public JavaFileObject createFileObject( @NonNull final Location location, @NonNull final File file, @NonNull final File root, @@ -304,7 +304,7 @@ public void delete (@NonNull final File file) { @Override @NonNull - JavaFileObject createFileObject( + public JavaFileObject createFileObject( @NonNull final Location location, @NonNull final File file, @NonNull final File root, @@ -355,7 +355,7 @@ public void delete(File file) { } @Override - JavaFileObject createFileObject(Location location, File file, File root, JavaFileFilterImplementation filter, Charset encoding) { + public JavaFileObject createFileObject(Location location, File file, File root, JavaFileFilterImplementation filter, Charset encoding) { return getDelegate().createFileObject(location, file, root, filter, encoding); } diff --git a/java.source.base/src/org/netbeans/modules/java/source/parsing/WriteBackTransaction.java b/java.source.base/src/org/netbeans/modules/java/source/parsing/WriteBackTransaction.java index 09c36cd29..beaf73d67 100644 --- a/java.source.base/src/org/netbeans/modules/java/source/parsing/WriteBackTransaction.java +++ b/java.source.base/src/org/netbeans/modules/java/source/parsing/WriteBackTransaction.java @@ -192,7 +192,7 @@ public int compareTo(@NonNull final JavaFileObject o) { @Override @NonNull - JavaFileObject createFileObject( + public JavaFileObject createFileObject( @NonNull final Location location, @NonNull final File file, @NonNull final File root, ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org For additional commands, e-mail: notifications-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists