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


##########
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....

Review Comment:
   This is unfortunately quite tricky. The CP must be registered in the 
`GlobalPathRegistry` (so that refactoring would work), so it will never be 
freed anyway. The current state is that these objects is basically perpetual 
(until the VM is closed, of course). I am afraid I don't have a good idea how 
to improve that. Probably the most realistic thing that comes to my mind is to 
free roots that are "unused" for an extended period of time. Which has its own 
problems, of course.



-- 
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