lahodaj commented on code in PR #6329:
URL: https://github.com/apache/netbeans/pull/6329#discussion_r1409287832


##########
java/java.file.launcher/src/org/netbeans/modules/java/file/launcher/api/SourceLauncher.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.file.launcher.api;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.netbeans.modules.java.file.launcher.SingleSourceFileUtil;
+import org.netbeans.modules.java.file.launcher.queries.MultiSourceRootProvider;
+import org.openide.filesystems.FileObject;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.Lookup;
+
+/**Holds utilities for source file launcher.
+ *
+ */
+public final class SourceLauncher {
+
+    private static final String ENABLE_PREVIEW = "--enable-preview";
+    private static final String SOURCE = "--source";
+    private static final String CLASS_PATH = "--class-path";
+    private static final String CLASSPATH = "-classpath";
+    private static final String CP = "-cp";
+    private static final String MODULE_PATH = "--module-path";
+    private static final String P = "-p";
+
+    /**Returns {@code true} if and only if the given file is known as a
+     * file that is handled by a source file launcher. This, in particular,
+     * typically means the file is outside of a project.
+     *
+     * @param file the file to test
+     * @return {@code true} if and only if the file is known as a file handled 
by the
+     *         source launcher. {@code false} otherwise.
+     */
+    public static boolean isSourceLauncherFile(FileObject file) {
+        MultiSourceRootProvider msrp = 
Lookup.getDefault().lookup(MultiSourceRootProvider.class);
+        return msrp != null && msrp.isSourceLauncher(file);
+    }
+
+    public static String joinCommandLines(Iterable<? extends String> 
inputLines) {
+        Map<String, String> joinedOptions = new HashMap<>();
+
+        for (String value : inputLines) {
+            List<String> args = SingleSourceFileUtil.parseLine(value);
+
+            for (int i = 0; i < args.size(); i++) {
+                switch (args.get(i)) {
+                    case ENABLE_PREVIEW:
+                        joinedOptions.put(ENABLE_PREVIEW, null);
+                        break;
+                    case CLASSPATH: case CLASS_PATH: case CP:
+                        if (i + 1 < args.size()) {
+                            joinedOptions.put(CLASS_PATH, 
mergePaths(joinedOptions.get(CLASS_PATH), args.get(i + 1)));
+                            i++;
+                        }
+                        break;
+                    case MODULE_PATH: case P:
+                        if (i + 1 < args.size()) {
+                            joinedOptions.put(MODULE_PATH, 
mergePaths(joinedOptions.get(MODULE_PATH), args.get(i + 1)));
+                            i++;
+                        }
+                        break;
+                    case SOURCE:
+                        if (i + 1 < args.size()) {
+                            String version = args.get(i + 1);
+                            String testVersion = version;
+                            if (testVersion.startsWith("1.")) {
+                                testVersion = testVersion.substring(2);
+                            }
+                            String existingVersion = 
joinedOptions.get("--source");

Review Comment:
   Fixed.



##########
java/java.file.launcher/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProvider.java:
##########
@@ -0,0 +1,300 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.file.launcher.queries;
+
+import java.beans.PropertyChangeListener;
+import java.beans.PropertyChangeSupport;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.WeakHashMap;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.classpath.GlobalPathRegistry;
+import org.netbeans.api.java.classpath.JavaClassPathConstants;
+import org.netbeans.api.java.lexer.JavaTokenId;
+import org.netbeans.api.java.platform.JavaPlatformManager;
+import org.netbeans.api.lexer.TokenHierarchy;
+import org.netbeans.api.lexer.TokenSequence;
+import org.netbeans.api.queries.FileEncodingQuery;
+import org.netbeans.modules.java.file.launcher.SingleSourceFileUtil;
+import 
org.netbeans.modules.java.file.launcher.spi.SingleFileOptionsQueryImplementation;
+import org.netbeans.spi.java.classpath.ClassPathFactory;
+import org.netbeans.spi.java.classpath.ClassPathImplementation;
+import static 
org.netbeans.spi.java.classpath.ClassPathImplementation.PROP_RESOURCES;
+import org.netbeans.spi.java.classpath.ClassPathProvider;
+import org.netbeans.spi.java.classpath.PathResourceImplementation;
+import org.netbeans.spi.java.classpath.support.ClassPathSupport;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.Exceptions;
+import org.openide.util.lookup.ServiceProvider;
+import org.openide.util.lookup.ServiceProviders;
+
+@ServiceProviders({
+    @ServiceProvider(service=ClassPathProvider.class, position=9_999),
+    @ServiceProvider(service=MultiSourceRootProvider.class)
+})
+public class MultiSourceRootProvider implements ClassPathProvider {
+
+    public static boolean DISABLE_MULTI_SOURCE_ROOT = false;
+
+    //TODO: the cache will probably be never cleared, as the ClassPath/value 
refers to the key(?)
+    private Map<FileObject, ClassPath> file2SourceCP = new WeakHashMap<>();
+    private Map<FileObject, ClassPath> root2SourceCP = new WeakHashMap<>();
+    private Map<FileObject, ClassPath> file2AllPath = new WeakHashMap<>();
+    private Map<FileObject, ClassPath> file2ClassPath = new WeakHashMap<>();
+    private Map<FileObject, ClassPath> file2ModulePath = new WeakHashMap<>();
+
+    @Override
+    public ClassPath findClassPath(FileObject file, String type) {
+        switch (type) {
+            case ClassPath.SOURCE: return getSourcePath(file);
+            case ClassPath.COMPILE:
+                return attributeBasedPath(file, file2AllPath, "-classpath", 
"-cp", "--class-path", "--module-path", "-p");
+            case JavaClassPathConstants.MODULE_CLASS_PATH:
+                return attributeBasedPath(file, file2ClassPath, "-classpath", 
"-cp", "--class-path");
+            case JavaClassPathConstants.MODULE_COMPILE_PATH:
+                return attributeBasedPath(file, file2ModulePath, 
"--module-path", "-p");
+            case ClassPath.BOOT:
+            case JavaClassPathConstants.MODULE_BOOT_PATH:
+                return getBootPath(file);
+        }
+        return null;
+    }
+
+    private ClassPath getSourcePath(FileObject file) {
+        if (DISABLE_MULTI_SOURCE_ROOT) return null;
+        synchronized (this) {
+            //XXX: what happens if there's a Java file in user's home???
+            if (file.isData() && "text/x-java".equals(file.getMIMEType())) {
+                return file2SourceCP.computeIfAbsent(file, f -> {
+                    try {
+                        String content = new String(file.asBytes(), 
FileEncodingQuery.getEncoding(file));
+                        String packName = findPackage(content);
+                        FileObject root = file.getParent();
+
+                        if (packName != null) {
+                            List<String> packageParts = 
Arrays.asList(packName.split("\\."));
+
+                            Collections.reverse(packageParts);
+
+                            for (String packagePart : packageParts) {
+                                if 
(!root.getNameExt().equalsIgnoreCase(packagePart)) {
+                                    //ignore files outside of proper package 
structure,
+                                    //those may too easily lead to using a too 
general
+                                    //directory as a root, leading to too much 
indexing:
+                                    return null;
+                                }
+                                root = root.getParent();
+                            }
+                        }
+
+                        return root2SourceCP.computeIfAbsent(root, r -> { 
//XXX: weak....
+                            ClassPath srcCP = 
ClassPathSupport.createClassPath(r);
+                            
GlobalPathRegistry.getDefault().register(ClassPath.SOURCE, new ClassPath[] 
{srcCP});
+                            return srcCP;
+                        });
+                    } catch (IOException ex) {
+                        Exceptions.printStackTrace(ex);
+                    }
+                    return null;
+                });
+            } else {
+                FileObject folder = file;
+
+                while (!folder.isRoot()) {
+                    ClassPath cp = root2SourceCP.get(folder);
+
+                    if (cp != null) {
+                        return cp;
+                    }
+
+                    folder = folder.getParent();
+                }
+
+                return null;
+            }
+        }
+    }
+
+    private synchronized FileObject getSourceRootImpl(FileObject file) {
+        for (FileObject root : root2SourceCP.keySet()) {
+            if (FileUtil.isParentOf(root, file) || root.equals(file)) {

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to