From 0a02898ff11ffe6ebf9334d8a43162884308542c Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Sun, 18 Dec 2016 00:36:22 +0100 Subject: [PATCH] Don't cache search classpath, only build it when looking for a class Fixes #4748 Libraries imported in opened sketches will be still locked by latest preprocessed CompilationUnit. Otherwise we would have to copy library jars somewhere else so they can be available to CompilationUnit while Contribution Manager updates original unlocked jars. --- java/src/processing/mode/java/pdex/PDEX.java | 7 ++++++- java/src/processing/mode/java/pdex/PreprocessedSketch.java | 6 +++--- .../processing/mode/java/pdex/PreprocessingService.java | 7 +++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/java/src/processing/mode/java/pdex/PDEX.java b/java/src/processing/mode/java/pdex/PDEX.java index b6a7e0ffb..9c2d0b30d 100644 --- a/java/src/processing/mode/java/pdex/PDEX.java +++ b/java/src/processing/mode/java/pdex/PDEX.java @@ -1,6 +1,7 @@ package processing.mode.java.pdex; import com.google.classpath.ClassPath; +import com.google.classpath.ClassPathFactory; import com.google.classpath.RegExpResourceFilter; import org.eclipse.jdt.core.compiler.IProblem; @@ -44,6 +45,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -1106,6 +1108,8 @@ public class PDEX { } } + AtomicReference searchClassPath = new AtomicReference<>(null); + if (problems.isEmpty()) { List cuProblems = Arrays.stream(iproblems) // Filter Warnings if they are not enabled @@ -1129,7 +1133,8 @@ public class PDEX { // Handle import suggestions if (JavaMode.importSuggestEnabled && isUndefinedTypeProblem(iproblem)) { - ClassPath cp = ps.searchClassPath; + ClassPath cp = searchClassPath.updateAndGet(prev -> prev != null ? + prev : new ClassPathFactory().createFromPaths(ps.searchClassPathArray)); String[] s = suggCache.computeIfAbsent(iproblem.getArguments()[0], name -> getImportSuggestions(cp, name)); p.setImportSuggestions(s); diff --git a/java/src/processing/mode/java/pdex/PreprocessedSketch.java b/java/src/processing/mode/java/pdex/PreprocessedSketch.java index 53fa7402f..50838eff8 100644 --- a/java/src/processing/mode/java/pdex/PreprocessedSketch.java +++ b/java/src/processing/mode/java/pdex/PreprocessedSketch.java @@ -26,7 +26,7 @@ public class PreprocessedSketch { public final ClassPath classPath; public final URLClassLoader classLoader; - public final ClassPath searchClassPath; + public final String[] searchClassPathArray; public final int[] tabStartOffsets; @@ -206,7 +206,7 @@ public class PreprocessedSketch { public ClassPath classPath; public URLClassLoader classLoader; - public ClassPath searchClassPath; + public String[] searchClassPathArray; public int[] tabStartOffsets = new int[0]; @@ -242,7 +242,7 @@ public class PreprocessedSketch { classPath = b.classPath; classLoader = b.classLoader; - searchClassPath = b.searchClassPath; + searchClassPathArray = b.searchClassPathArray; tabStartOffsets = b.tabStartOffsets; diff --git a/java/src/processing/mode/java/pdex/PreprocessingService.java b/java/src/processing/mode/java/pdex/PreprocessingService.java index 6ac0981ef..ce535a92d 100644 --- a/java/src/processing/mode/java/pdex/PreprocessingService.java +++ b/java/src/processing/mode/java/pdex/PreprocessingService.java @@ -341,7 +341,7 @@ public class PreprocessingService { boolean rebuildClassPath = reloadCodeFolder || rebuildLibraryClassPath || prevResult.classLoader == null || prevResult.classPath == null || - prevResult.classPathArray == null || prevResult.searchClassPath == null; + prevResult.classPathArray == null || prevResult.searchClassPathArray == null; if (reloadCodeFolder) { codeFolderClassPath = buildCodeFolderClassPath(sketch); @@ -381,13 +381,12 @@ public class PreprocessingService { searchClassPath.addAll(coreLibraryClassPath); searchClassPath.addAll(codeFolderClassPath); - String[] searchClassPathArray = searchClassPath.stream().toArray(String[]::new); - result.searchClassPath = classPathFactory.createFromPaths(searchClassPathArray); + result.searchClassPathArray = searchClassPath.stream().toArray(String[]::new); } } else { result.classLoader = prevResult.classLoader; result.classPath = prevResult.classPath; - result.searchClassPath = prevResult.searchClassPath; + result.searchClassPathArray = prevResult.searchClassPathArray; result.classPathArray = prevResult.classPathArray; } }