Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -771,82 +771,24 @@ public void finished(boolean success) {
boolean recentProjectsChanged = false;
int maxWork = 1000;
double workForSubprojects = maxWork / (openSubprojects ? 2.0 : 10.0);
double currentWork = 0;
Collection<Project> projectsToOpen = new LinkedHashSet<Project>();

if (handle != null) {
handle.switchToDeterminate(maxWork);
handle.progress(0);
}

Map<Project,Set<? extends Project>> subprojectsCache = new HashMap<Project,Set<? extends Project>>(); // #59098
while (!toHandle.isEmpty()) {
if (canceled != null && canceled.get()) {
break;
}

Project p = toHandle.remove(0);
assert p != null;
if (!p.getProjectDirectory().isValid()) {
StatusDisplayer.getDefault().setStatusText(OpenProjectList_deleted_project(FileUtil.getFileDisplayName(p.getProjectDirectory())));
continue;
}
Set<? extends Project> subprojects = openSubprojects ? subprojectsCache.get(p) : Collections.<Project>emptySet();
boolean recurse = true;
if (subprojects == null) {
ProjectContainerProvider pcp = p.getLookup().lookup(ProjectContainerProvider.class);
if (pcp != null) {
if (handle != null) {
handle.progress(OpenProjectList_finding_subprojects(ProjectUtils.getInformation(p).getDisplayName()));
}
ProjectContainerProvider.Result res = pcp.getContainedProjects();
subprojects = res.getProjects();
if (res.isRecursive()) {
recurse = false;
}
} else {
SubprojectProvider spp = p.getLookup().lookup(SubprojectProvider.class);
if (spp != null) {
if (handle != null) {
handle.progress(OpenProjectList_finding_subprojects(ProjectUtils.getInformation(p).getDisplayName()));
}
subprojects = spp.getSubprojects();
} else {
subprojects = Collections.emptySet();
}
}
subprojectsCache.put(p, subprojects);
}

projectsToOpen.add(p);

for (Project sub : subprojects) {
assert sub != null;
if (sub != null && /** #224592 we need to test for null sub as some subprojectProvider implementations could be faulty and return null and with final releases assert won't fire */
!projectsToOpen.contains(sub) && !toHandle.contains(sub)) {
if (recurse) {
toHandle.add(sub);
} else {
projectsToOpen.add(sub);
}
}
}

double workPerOneProject = (workForSubprojects - currentWork) / (toHandle.size() + 1);
int lastState = (int) currentWork;

currentWork += workPerOneProject;

if (handle != null && lastState < (int) currentWork) {
handle.progress((int) currentWork);
}
}
var subprojectsResolver = new SubprojectsResolver();
Collection<Project> projectsToOpen = subprojectsResolver.getSubprojectsClosure(
toHandle, canceled, openSubprojects, handle,
workForSubprojects, 0
);

if (projectsToOpen.isEmpty()) {
return;
}

double workPerProject = (maxWork - workForSubprojects) / projectsToOpen.size();
double currentWork = workForSubprojects;

final List<Project> oldprjs = new ArrayList<Project>();
final List<Project> newprjs = new ArrayList<Project>();
Expand Down Expand Up @@ -920,7 +862,7 @@ public Void run() {
LOAD.exit();
}
}

public void close( Project someProjects[], boolean notifyUI) {
Group act = Group.getActiveGroup();
close(someProjects, notifyUI, act != null ? act.getName() : null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* 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.project.ui;

import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import static org.netbeans.modules.project.ui.Bundle.OpenProjectList_deleted_project;
import static org.netbeans.modules.project.ui.Bundle.OpenProjectList_finding_subprojects;
import org.netbeans.spi.project.ProjectContainerProvider;
import org.netbeans.spi.project.SubprojectProvider;
import org.openide.awt.StatusDisplayer;
import org.openide.filesystems.FileUtil;

/**
* Helper class to handle computation of subprojects for projects. Instantiate
* once and then query via {@link #getSubprojects} as many times as necessary.
* The computed results are cached.
*/
public final class SubprojectsResolver {
private final Map<Project, Set<? extends Project>> subprojectsCache = new HashMap<>(); // #59098

final Set<? extends Project> getSubprojects(Project p, ProgressHandle handle, boolean[] recurseOut) {
boolean recurse = true;
Set<? extends Project> subprojects = subprojectsCache.get(p);
if (subprojects == null) {
ProjectContainerProvider pcp = p.getLookup().lookup(ProjectContainerProvider.class);
if (pcp != null) {
if (handle != null) {
handle.progress(OpenProjectList_finding_subprojects(ProjectUtils.getInformation(p).getDisplayName()));
}
ProjectContainerProvider.Result res = pcp.getContainedProjects();
subprojects = res.getProjects();
if (res.isRecursive()) {
recurse = false;
}
} else {
SubprojectProvider spp = p.getLookup().lookup(SubprojectProvider.class);
if (spp != null) {
if (handle != null) {
handle.progress(OpenProjectList_finding_subprojects(ProjectUtils.getInformation(p).getDisplayName()));
}
subprojects = spp.getSubprojects();
} else {
subprojects = Collections.emptySet();
}
}
subprojectsCache.put(p, subprojects);
}
recurseOut[0] = recurse;
return subprojects;
}

/** Processes given set of projects. When {@code includeSubprojects} is
* {@code true}, then it computes also transitive closure of all projects.
*
* @param core the core set of projects to process
* @param canceled either {@code null} or an instance of boolean holder to cancel
* the computation
* @param includeSubprojects should subprojects be counted
* @param handle {@code null} or handle to report progress to
* @param maxWork maximum work to report - only needed when {@code handle} is specified
* @param currentWork the current work in the {@code handle}
* @return the projects to process
*/
public final Set<Project> getSubprojectsClosure(
Collection<Project> core,
AtomicBoolean canceled,
boolean includeSubprojects,
ProgressHandle handle,
double maxWork,
double currentWork
) {
Queue<Project> toHandle = new ArrayDeque<>(core);
Set<Project> projectsToOpen = new LinkedHashSet<>();
while (!toHandle.isEmpty()) {
if (canceled != null && canceled.get()) {
break;
}

Project p = toHandle.remove();
assert p != null;
if (!p.getProjectDirectory().isValid()) {
StatusDisplayer.getDefault().setStatusText(OpenProjectList_deleted_project(FileUtil.getFileDisplayName(p.getProjectDirectory())));
continue;
}
boolean[] recurse = new boolean[1];
Set<? extends Project> subprojects = includeSubprojects ?
getSubprojects(p, handle, recurse)
:
Collections.<Project>emptySet();
projectsToOpen.add(p);

for (Project sub : subprojects) {
assert sub != null;
if (sub != null && /** #224592 we need to test for null sub as some subprojectProvider implementations could be faulty and return null and with final releases assert won't fire */
!projectsToOpen.contains(sub) && !toHandle.contains(sub)) {
if (recurse[0]) {
toHandle.add(sub);
} else {
projectsToOpen.add(sub);
}
}
}

double workPerOneProject = (maxWork - currentWork) / (toHandle.size() + 1);
int lastState = (int) currentWork;

currentWork += workPerOneProject;

if (handle != null && lastState < (int) currentWork) {
handle.progress((int) currentWork);
}
}
return projectsToOpen;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.netbeans.modules.project.ui.groups;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -157,4 +156,8 @@ protected String toString(boolean scrubPersonalInfo) {
return super.toString(scrubPersonalInfo) + (isAutoSynch() ? "" : "[!autoSynch]");
}

@Override
boolean isSubprojects() {
return false;
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re. @neilcsmith-net's comment:

  • these issues are deep architecture and UX changes issues ...
    • that influence the core of how NetBeans behave ...
    • and require time to gain enough understanding
    • it is important to make sure the direction is right before changing the course
  • on a contrary this issue is a small, targeted and isolated optimization
  • hence I plan to continue to address the technical review comments
    • as Tracy notes: Promises for days to come are as good as none...
    • why wait?

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static DirectoryGroup create(String name, FileObject dir) {

@Override
protected void findProjects(Set<Project> projects, ProgressHandle h, int start, int end) {
assert !SwingUtilities.isEventDispatchThread();
assert !SwingUtilities.isEventDispatchThread();
String dir = prefs().get(KEY_PATH, null);
FileObject fo = null;
try {
Expand Down Expand Up @@ -111,6 +111,11 @@ public GroupEditPanel createPropertiesPanel() {
return new DirectoryGroupEditPanel(this);
}

@Override
boolean isSubprojects() {
return true;
}

private void processFolderChildren(FileObject fo, Set<Project> projects, ProgressHandle h, Counter counter, boolean checkShare) throws IllegalArgumentException {
Enumeration<? extends FileObject> e = fo.getFolders(false);
while (e.hasMoreElements()) {
Expand All @@ -129,10 +134,11 @@ private void processFolderChildren(FileObject fo, Set<Project> projects, Progres
if (h != null) {
h.progress(progressMessage(p), Math.min(++counter.start, counter.end));
}
continue;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As soon as a project is found, skip its further directory processing...

}
checkShare = share != SharabilityQuery.Sharability.SHARABLE;
// don't need to check the sharability if the current folder is marked as recursively sharable

processFolderChildren(em, projects, h, counter, checkShare);

@jtulach jtulach Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • this PR changes the behavior when a valid "root" Project is found
    • previously the scan continued inside of project structure
    • now it stops and relies on open subprojects behavior of OpenProjects.open
  • such a behavior is way faster
    • scanning NetBeans repository now takes just 4s
    • while previously it took 16s
  • the new behavior is more predictable
  • some Git repositories (like NetBeans or Maven) contain a lot of "testing projects" hidden in test data
    • the previous behavior found those projects ...
    • ... which was greatly unexpected ...
    • ... and also dangerous as those projects are often (intentionally) corrupted, misconfigured or otherwise broken
  • in case of NetBeans there was more than three hundred of such modules
  • lowering the number might even improve the Matthias warning a bit
    • although tons of time is then going to be spend while parsing the projects anyway ...
    • ... to be addressed in some future PR
  • the speed gain and the matched expectations are the major UX improvements of this PR

} catch (IOException x) {
Exceptions.printStackTrace(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.netbeans.modules.project.ui.OpenProjectListSettings;
import org.netbeans.modules.project.ui.ProjectTab;
import org.netbeans.modules.project.ui.ProjectUtilities;
import org.netbeans.modules.project.ui.SubprojectsResolver;
import static org.netbeans.modules.project.ui.groups.Bundle.*;
import org.openide.cookies.CloseCookie;
import org.openide.filesystems.FileObject;
Expand Down Expand Up @@ -471,10 +472,17 @@ protected static Project projectForPath(String path) {
}

/**
* The projects (currently) contained in the group.
* The projects (currently) contained in the group. This includes all specified
* projects as well as <em>required projects</em> when {@link #isSubprojects()}
* of the group is on.
*/
public Set<Project> getProjects() {
return getProjects(null, 0, 0);
public final Set<Project> getProjects() {
Set<Project> core = getProjects(null, 0, 0);
if (isSubprojects()) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eppleton, this addresses your concern with respect to Main Project action, e.g. the following shall be fine:

Common root of the Group.java inline comment and the next point: Group.getProjects() used to include the subprojects and now returns only the roots, but Group.open and setMainProject still treat getProjects() as the full set of projects in the group.

setMainProject (not in the diff) keeps the main project only if getProjects().contains(mainProject). If the user picks a subproject as main project (e.g. a module of the master in a SubprojectsGroup, or a nested module in a DirectoryGroup), KEY_MAIN is now removed with a warning, so the main project is lost the next time the group is opened.

var resolver = new SubprojectsResolver();
core = resolver.getSubprojectsClosure(core, null, true, null, 0, 0);
}
return core;
}

private Set<Project> getProjects(ProgressHandle h, int start, int end) {
Expand Down Expand Up @@ -578,7 +586,10 @@ static void open(final Group g, String oldGroupName, boolean isNewGroup, Prefere
oldOpen.add(real != null ? real : open);
}

long then = System.currentTimeMillis();
Set<Project> newOpen = g != null ? g.getProjects(h, 10, 100) : Collections.emptySet();
long now = System.currentTimeMillis();
LOG.log(Level.FINE, "Found {0} projects of group {1} in {2} ms", new Object[] { newOpen.size(), g, now - then });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I am using netbeans -J-Dorg.netbeans.modules.project.ui.groups.Group.level=FINE to enabled this log message
  • and then grep Group nbbuild/testuserdir/var/log/messages.log for it
  • previously it would print Found 1 206 projects of group DirectoryGroup[id=netbeans,|projects|=1203] in 16 779 ms
  • now it prints Found 857 projects of group DirectoryGroup[id=netbeans,|projects|=857] in 3 665 ms

final Set<Project> toClose = new HashSet<Project>(oldOpen);
toClose.removeAll(newOpen);
final Set<Project> toOpen = new HashSet<Project>(newOpen);
Expand All @@ -591,12 +602,14 @@ static void open(final Group g, String oldGroupName, boolean isNewGroup, Prefere
IndexingBridge.Lock lock = IndexingBridge.getDefault().protectedMode();
try {
h.progress(Group_progress_closing(toClose.size()), 110);
Project[] arrClose = toClose.toArray(new Project[0]);
//close and remember the last opened files in the old group
opl.close(toClose.toArray(new Project[0]), false, oldGroupName);
opl.close(arrClose, false, oldGroupName);
h.switchToIndeterminate();
h.progress(Group_progress_opening(toOpen.size()));
//open the projects with current group
opl.open(toOpen.toArray(new Project[0]), false, false, h, null);
Project[] arrOpen = toOpen.toArray(new Project[0]);
opl.open(arrOpen, false, g != null && g.isSubprojects(), h, null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since newOpen now contains only the root projects, subprojects can get closed without being reopened. Example: group A is a SubprojectsGroup of X, so X and its subprojects are open. Switching to group B, a DirectoryGroup that also contains X, gives toClose = oldOpen - newOpen = the subprojects of X. X itself stays open, so it is not in toOpen, and openSubprojects never runs for it, so the subprojects stay closed. For the same reason the subprojects drop out of stayOpened, so their open documents are no longer persisted or restored per group.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this one is tough!

  • if we want to defer computing subprojects for later
  • then they cannot be known here and newOpen is clearly not containing all the projects
  • the best I can think of is to move this logic into open logic itself
  • have OpenProjectList.open(... boolean replacePrevious ...) and let it return added/remove projects

I have to think about it.


if(!isNewGroup) {
//for old and new group project intersection, save the old files list,
Expand Down Expand Up @@ -753,4 +766,6 @@ private void notifyListeners(Object object, String property, String oldValue, St
listener.propertyChange(new PropertyChangeEvent(object, property, oldValue, newValue));
}
}

abstract boolean isSubprojects();
}
Loading
Loading