lahodaj commented on code in PR #6329: URL: https://github.com/apache/netbeans/pull/6329#discussion_r1311256901
########## java/java.file.launcher/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProvider.java: ########## @@ -0,0 +1,169 @@ +/* + * 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.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.WeakHashMap; +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.spi.java.classpath.ClassPathProvider; +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=100_000), + @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<>(); + + @Override + public ClassPath findClassPath(FileObject file, String type) { + if (DISABLE_MULTI_SOURCE_ROOT) return null; + switch (type) { + case ClassPath.SOURCE: return getSourcePath(file); + case ClassPath.BOOT: + case JavaClassPathConstants.MODULE_BOOT_PATH: + return getBootPath(file); + } + return null; + } + + private ClassPath getSourcePath(FileObject file) { + 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) { Review Comment: Yes, until we see a file, we will not know about it. I don't quite see a way around this, I'm afraid. ########## java/java.file.launcher/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProvider.java: ########## @@ -0,0 +1,169 @@ +/* + * 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.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.WeakHashMap; +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.spi.java.classpath.ClassPathProvider; +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=100_000), + @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<>(); + + @Override + public ClassPath findClassPath(FileObject file, String type) { + if (DISABLE_MULTI_SOURCE_ROOT) return null; + switch (type) { + case ClassPath.SOURCE: return getSourcePath(file); + case ClassPath.BOOT: + case JavaClassPathConstants.MODULE_BOOT_PATH: + return getBootPath(file); + } + return null; + } + + private ClassPath getSourcePath(FileObject file) { + 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; + } + } + } + + public synchronized boolean isSourceLauncher(FileObject file) { + for (FileObject root : root2SourceCP.keySet()) { + if (FileUtil.isParentOf(root, file) || root.equals(file)) { + return true; + } + } + + return false; Review Comment: As above - until we see a file, we will not know about it. I don't quite see a way around this, I'm afraid. -- 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
