You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by ge...@apache.org on 2018/06/05 11:38:34 UTC

[incubator-netbeans] branch master updated: [NETBEANS-817] Preventing exceptions when opening project: (#552)

This is an automated email from the ASF dual-hosted git repository.

geertjan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 9295f7b  [NETBEANS-817] Preventing exceptions when opening project: (#552)
9295f7b is described below

commit 9295f7b096dad387bfd2ce5656d81ae32e9f1ec3
Author: Jan Lahoda <la...@gmail.com>
AuthorDate: Tue Jun 5 13:38:29 2018 +0200

    [NETBEANS-817] Preventing exceptions when opening project: (#552)
    
    -accepting duplicate ClassPath entries with module-infos
    -filtering out options that don't match the validated source level
---
 .../java/api/common/classpath/ModuleClassPaths.java  |  6 +++---
 .../api/common/classpath/ModuleClassPathsTest.java   | 20 ++++++++++++++++++++
 .../modules/java/source/indexing/APTUtils.java       |  2 +-
 .../modules/java/source/parsing/JavacParser.java     | 10 ++++++----
 .../modules/java/source/parsing/JavacParserTest.java | 15 ++++++++++++++-
 5 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/java.api.common/src/org/netbeans/modules/java/api/common/classpath/ModuleClassPaths.java b/java.api.common/src/org/netbeans/modules/java/api/common/classpath/ModuleClassPaths.java
index 10c6606..65a2797 100644
--- a/java.api.common/src/org/netbeans/modules/java/api/common/classpath/ModuleClassPaths.java
+++ b/java.api.common/src/org/netbeans/modules/java/api/common/classpath/ModuleClassPaths.java
@@ -676,7 +676,7 @@ final class ModuleClassPaths {
                 bestSoFar[1] = Boolean.TRUE;
                 return (List<? extends PathResourceImplementation>) bestSoFar[0];
             }
-            final Collection<File> newModuleInfos = new ArrayDeque<>();
+            final Collection<File> newModuleInfos = new LinkedHashSet<>();
             final List<URL> newActiveProjectSourceRoots = new ArrayList<>();
             collectProjectSourceRoots(systemModules, newActiveProjectSourceRoots);
             collectProjectSourceRoots(userModules, newActiveProjectSourceRoots);
@@ -922,9 +922,9 @@ final class ModuleClassPaths {
                             newModuleInfos
                         });
                     setCache(res);
-                    final Collection<File> added = new ArrayList<>(newModuleInfos);
+                    final Collection<File> added = new LinkedHashSet<>(newModuleInfos);
                     added.removeAll(moduleInfos);
-                    final Collection<File> removed = new ArrayList<>(moduleInfos);
+                    final Collection<File> removed = new LinkedHashSet<>(moduleInfos);
                     removed.removeAll(newModuleInfos);
                     removed.stream().forEach((f) -> FileUtil.removeFileChangeListener(this, f));
                     added.stream().forEach((f) -> FileUtil.addFileChangeListener(this, f));
diff --git a/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/classpath/ModuleClassPathsTest.java b/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/classpath/ModuleClassPathsTest.java
index 7414cd8..b30f824 100644
--- a/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/classpath/ModuleClassPathsTest.java
+++ b/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/classpath/ModuleClassPathsTest.java
@@ -64,6 +64,7 @@ import org.netbeans.modules.java.api.common.TestJavaPlatform;
 import org.netbeans.modules.java.api.common.TestProject;
 import org.netbeans.modules.java.api.common.project.ProjectProperties;
 import org.netbeans.modules.java.j2seplatform.platformdefinition.Util;
+import org.netbeans.modules.java.source.BootClassPathUtil;
 import org.netbeans.modules.parsing.api.indexing.IndexingManager;
 import org.netbeans.spi.java.classpath.ClassPathFactory;
 import org.netbeans.spi.java.classpath.ClassPathProvider;
@@ -583,6 +584,25 @@ public class ModuleClassPathsTest extends NbTestCase {
                 collectEntries(cp));
     }
 
+    public void testDuplicateSourceDirsNETBEANS_817() throws Exception {
+        if (systemModules == null) {
+            System.out.println("No jdk 9 home configured.");    //NOI18N
+            return;
+        }
+        assertNotNull(src);
+        final FileObject moduleInfo = createModuleInfo(src, "Modle", "java.logging"); //NOI18N
+        final ClassPath cp = ClassPathFactory.createClassPath(ModuleClassPaths.createModuleInfoBasedPath(
+                systemModules,
+                org.netbeans.spi.java.classpath.support.ClassPathSupport.createProxyClassPath(src, src),
+                systemModules,
+                ClassPath.EMPTY,
+                null,
+                null));
+        final Collection<URL> resURLs = collectEntries(cp);
+        final Collection<URL> expectedURLs = reads(systemModules, NamePredicate.create("java.logging"));  //NOI18N
+        assertEquals(expectedURLs, resURLs);
+    }
+
     private static void setSourceLevel(
             @NonNull final Project prj,
             @NonNull final String sourceLevel) throws IOException {
diff --git a/java.source.base/src/org/netbeans/modules/java/source/indexing/APTUtils.java b/java.source.base/src/org/netbeans/modules/java/source/indexing/APTUtils.java
index 68e4a4b..425b520 100644
--- a/java.source.base/src/org/netbeans/modules/java/source/indexing/APTUtils.java
+++ b/java.source.base/src/org/netbeans/modules/java/source/indexing/APTUtils.java
@@ -447,7 +447,7 @@ public class APTUtils implements ChangeListener, PropertyChangeListener {
                     return vote;
                 }
             }
-            final String cmpOptsStr = JavacParser.validateCompilerOptions(compilerOptions.getArguments())
+            final String cmpOptsStr = JavacParser.validateCompilerOptions(compilerOptions.getArguments(), null)
                     .stream()
                     .collect(Collectors.joining(" "));  //NOI18N
             if (JavaIndex.ensureAttributeValue(url, COMPILER_OPTIONS, cmpOptsStr, checkOnly)) {
diff --git a/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java b/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java
index 3aa15da..c237d57 100644
--- a/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java
+++ b/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java
@@ -871,7 +871,7 @@ public class JavacParser extends Parser {
             options.add("-proc:none"); // NOI18N, Disable annotation processors
         }
         if (compilerOptions != null) {
-            for (String compilerOption : validateCompilerOptions(compilerOptions.getArguments())) {
+            for (String compilerOption : validateCompilerOptions(compilerOptions.getArguments(), validatedSourceLevel)) {
                 options.add(compilerOption);
             }
         }
@@ -1029,8 +1029,9 @@ public class JavacParser extends Parser {
     }
 
     @NonNull
-    public static List<? extends String> validateCompilerOptions(@NonNull final List<? extends String> options) {
+    public static List<? extends String> validateCompilerOptions(@NonNull final List<? extends String> options, @NullAllowed com.sun.tools.javac.code.Source sourceLevel) {
         final List<String> res = new ArrayList<>();
+        boolean allowModularOptions = sourceLevel == null || com.sun.tools.javac.code.Source.lookup("9").compareTo(sourceLevel) <= 0;
         boolean xmoduleSeen = false;
         for (int i = 0; i < options.size(); i++) {
             String option = options.get(i);
@@ -1047,12 +1048,13 @@ public class JavacParser extends Parser {
                 xmoduleSeen = true;
             } else if (option.equals("-parameters") || option.startsWith("-Xlint")) {     //NOI18N
                 res.add(option);
-            } else if (
+            } else if ((
                     option.startsWith("--add-modules") ||   //NOI18N
                     option.startsWith("--limit-modules") || //NOI18N
                     option.startsWith("--add-exports") ||   //NOI18N
                     option.startsWith("--add-reads")  ||
-                    option.startsWith(OPTION_PATCH_MODULE)) {
+                    option.startsWith(OPTION_PATCH_MODULE)) &&
+                    allowModularOptions) {
                 int idx = option.indexOf('=');
                 if (idx > 0) {
                    res.add(option);
diff --git a/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java b/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java
index 2f9b75b..b159166 100644
--- a/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java
+++ b/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java
@@ -30,7 +30,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
@@ -393,7 +395,18 @@ public class JavacParserTest extends NbTestCase {
         
         return JavacParser.validateSourceLevel("1.7", info, false);
     }
-    
+
+    public void testValidateCompilerOptions() {
+        List<String> input = Arrays.asList("--add-exports", "foo/bar=foobar",
+                                           "--add-exports=foo2/bar=foobar",
+                                           "--limit-modules", "foo",
+                                           "--add-modules", "foo",
+                                           "--add-reads", "foo=foo2");
+        assertEquals(Collections.emptyList(), JavacParser.validateCompilerOptions(input, com.sun.tools.javac.code.Source.lookup("1.8")));
+        assertEquals(input, JavacParser.validateCompilerOptions(input, com.sun.tools.javac.code.Source.lookup("9")));
+        assertEquals(input, JavacParser.validateCompilerOptions(input, com.sun.tools.javac.code.Source.lookup("10")));
+    }
+
     private FileObject createFile(String path, String content) throws Exception {
         FileObject file = FileUtil.createData(sourceRoot, path);
         TestUtilities.copyStringToFile(file, content);

-- 
To stop receiving notification emails like this one, please contact
geertjan@apache.org.

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists