Skip to content

Commit 73319a5

Browse files
committed
Various minor code clean-ups and modernizations in Resources plugin
- Use Arrays.sort() instead of custom quick-sort implementation - Rely on monitor accepting methods that a null-monitor can be handled - Platform.getOS() can now be used in non-OSGi environments
1 parent 824a163 commit 73319a5

File tree

7 files changed

+84
-107
lines changed

7 files changed

+84
-107
lines changed

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/CopyVisitor.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,25 @@
1818
import java.util.LinkedList;
1919
import org.eclipse.core.filesystem.EFS;
2020
import org.eclipse.core.filesystem.IFileStore;
21-
import org.eclipse.core.internal.resources.*;
21+
import org.eclipse.core.internal.resources.Container;
22+
import org.eclipse.core.internal.resources.File;
23+
import org.eclipse.core.internal.resources.FilterDescription;
24+
import org.eclipse.core.internal.resources.Folder;
25+
import org.eclipse.core.internal.resources.Project;
26+
import org.eclipse.core.internal.resources.Resource;
27+
import org.eclipse.core.internal.resources.ResourceInfo;
28+
import org.eclipse.core.internal.resources.ResourceStatus;
29+
import org.eclipse.core.internal.resources.Workspace;
2230
import org.eclipse.core.internal.utils.Messages;
23-
import org.eclipse.core.resources.*;
24-
import org.eclipse.core.runtime.*;
31+
import org.eclipse.core.resources.IResource;
32+
import org.eclipse.core.resources.IResourceStatus;
33+
import org.eclipse.core.resources.ResourcesPlugin;
34+
import org.eclipse.core.runtime.CoreException;
35+
import org.eclipse.core.runtime.IPath;
36+
import org.eclipse.core.runtime.IProgressMonitor;
37+
import org.eclipse.core.runtime.IStatus;
38+
import org.eclipse.core.runtime.MultiStatus;
39+
import org.eclipse.core.runtime.SubMonitor;
2540
import org.eclipse.osgi.util.NLS;
2641

2742
//
@@ -135,7 +150,7 @@ protected Resource getDestinationResource(Resource source, IPath suffix) {
135150
*/
136151
protected RefreshLocalVisitor getRefreshLocalVisitor() {
137152
if (refreshLocalVisitor == null)
138-
refreshLocalVisitor = new RefreshLocalVisitor(SubMonitor.convert(null));
153+
refreshLocalVisitor = new RefreshLocalVisitor(null);
139154
return refreshLocalVisitor;
140155
}
141156

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ public boolean internalWrite(IProject target, IProjectDescription description, i
722722
}
723723

724724
//write the project description file (don't use API because scheduling rule might not match)
725-
write(descriptionFile, newContents, fileInfo, IResource.FORCE, false, SubMonitor.convert(null));
726-
workspace.getAliasManager().updateAliases(descriptionFile, getStore(descriptionFile), IResource.DEPTH_ZERO, SubMonitor.convert(null));
725+
write(descriptionFile, newContents, fileInfo, IResource.FORCE, false, null);
726+
workspace.getAliasManager().updateAliases(descriptionFile, getStore(descriptionFile), IResource.DEPTH_ZERO, null);
727727

728728
//update the timestamp on the project as well so we know when it has
729729
//been changed from the outside
@@ -788,7 +788,7 @@ public boolean isSynchronized(IResource target, int depth) {
788788
return true;
789789
break;
790790
}
791-
IsSynchronizedVisitor visitor = new IsSynchronizedVisitor(SubMonitor.convert(null));
791+
IsSynchronizedVisitor visitor = new IsSynchronizedVisitor(null);
792792
UnifiedTree tree = new UnifiedTree(target);
793793
try {
794794
tree.accept(visitor, depth);
@@ -954,9 +954,7 @@ public ProjectDescription read(IProject target, boolean creation) throws CoreExc
954954
ProjectDescription description = null;
955955
//hold onto any exceptions until after sync info is updated, then throw it
956956
ResourceException error = null;
957-
try (
958-
InputStream in = new BufferedInputStream(descriptionStore.openInputStream(EFS.NONE, SubMonitor.convert(null)));
959-
) {
957+
try (InputStream in = new BufferedInputStream(descriptionStore.openInputStream(EFS.NONE, null));) {
960958
// IFileStore#openInputStream may cancel the monitor, thus the monitor state is checked
961959
description = new ProjectDescriptionReader(target).read(new InputSource(in));
962960
} catch (OperationCanceledException e) {

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/UnifiedTree.java

Lines changed: 50 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,28 @@
2222

2323
import java.io.IOException;
2424
import java.nio.file.Path;
25-
import java.util.*;
25+
import java.util.ArrayList;
26+
import java.util.Arrays;
27+
import java.util.Collections;
28+
import java.util.Iterator;
29+
import java.util.LinkedList;
30+
import java.util.List;
2631
import java.util.regex.Pattern;
27-
import org.eclipse.core.filesystem.*;
32+
import org.eclipse.core.filesystem.EFS;
33+
import org.eclipse.core.filesystem.IFileInfo;
34+
import org.eclipse.core.filesystem.IFileStore;
35+
import org.eclipse.core.filesystem.IFileTree;
2836
import org.eclipse.core.internal.refresh.RefreshJob;
29-
import org.eclipse.core.internal.resources.*;
37+
import org.eclipse.core.internal.resources.ICoreConstants;
38+
import org.eclipse.core.internal.resources.Resource;
39+
import org.eclipse.core.internal.resources.ResourceInfo;
40+
import org.eclipse.core.internal.resources.Workspace;
3041
import org.eclipse.core.resources.IContainer;
3142
import org.eclipse.core.resources.IResource;
32-
import org.eclipse.core.runtime.*;
43+
import org.eclipse.core.runtime.Assert;
44+
import org.eclipse.core.runtime.CoreException;
45+
import org.eclipse.core.runtime.IPath;
46+
import org.eclipse.core.runtime.Platform;
3347
import org.eclipse.core.runtime.jobs.Job;
3448

3549
/**
@@ -43,16 +57,9 @@ public class UnifiedTree {
4357
/** special node to mark the separation of a node's children */
4458
protected static final UnifiedTreeNode childrenMarker = new UnifiedTreeNode(null, null, null, null, false);
4559

46-
private static final Iterator<UnifiedTreeNode> EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator();
47-
4860
/** special node to mark the beginning of a level in the tree */
4961
protected static final UnifiedTreeNode levelMarker = new UnifiedTreeNode(null, null, null, null, false);
5062

51-
private static final IFileInfo[] NO_CHILDREN = {};
52-
53-
/** Singleton to indicate no local children */
54-
private static final IResource[] NO_RESOURCES = {};
55-
5663
/**
5764
* True if the level of the children of the current node are valid according
5865
* to the requested refresh depth, false otherwise
@@ -145,7 +152,7 @@ protected void addChildren(UnifiedTreeNode node) {
145152

146153
// get the list of resources in the file system
147154
// don't ask for local children if we know it doesn't exist locally
148-
IFileInfo[] list = node.existsInFileSystem() ? getLocalList(node) : NO_CHILDREN;
155+
List<IFileInfo> list = node.existsInFileSystem() ? getLocalList(node) : List.of();
149156
int localIndex = 0;
150157

151158
// See if the children of this resource have been computed before
@@ -155,21 +162,21 @@ protected void addChildren(UnifiedTreeNode node) {
155162

156163
// get the list of resources in the workspace
157164
if (!unknown && (parentType == IResource.FOLDER || parentType == IResource.PROJECT) && parent.exists(flags, true)) {
158-
IResource target = null;
159-
UnifiedTreeNode child = null;
160-
IResource[] members;
165+
List<IResource> members;
161166
try {
162-
members = ((IContainer) parent).members(IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS | IContainer.INCLUDE_HIDDEN);
167+
IContainer container = (IContainer) parent;
168+
members = Arrays.asList(container.members(IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS | IContainer.INCLUDE_HIDDEN));
163169
} catch (CoreException e) {
164-
members = NO_RESOURCES;
170+
members = List.of();
165171
}
166172
int workspaceIndex = 0;
167173
//iterate simultaneously over file system and workspace members
168-
while (workspaceIndex < members.length) {
169-
target = members[workspaceIndex];
174+
while (workspaceIndex < members.size()) {
175+
IResource target = members.get(workspaceIndex);
170176
String name = target.getName();
171-
IFileInfo localInfo = localIndex < list.length ? list[localIndex] : null;
177+
IFileInfo localInfo = localIndex < list.size() ? list.get(localIndex) : null;
172178
int comp = localInfo != null ? name.compareTo(localInfo.getName()) : -1;
179+
UnifiedTreeNode child = null;
173180
//special handling for linked resources
174181
if (target.isLinked()) {
175182
//child will be null if location is undefined
@@ -221,11 +228,11 @@ protected void addChildren(UnifiedTreeNode node) {
221228
addChildrenMarker();
222229
}
223230

224-
protected void addChildrenFromFileSystem(UnifiedTreeNode node, IFileInfo[] childInfos, int index) {
225-
if (childInfos == null)
231+
protected void addChildrenFromFileSystem(UnifiedTreeNode node, List<IFileInfo> childInfos, int index) {
232+
if (childInfos == null) {
226233
return;
227-
for (int i = index; i < childInfos.length; i++) {
228-
IFileInfo info = childInfos[i];
234+
}
235+
for (IFileInfo info : childInfos.subList(index, childInfos.size())) {
229236
//don't create a node for symbolic links that create a cycle
230237
if (!info.getAttribute(EFS.ATTRIBUTE_SYMLINK) || !info.isDirectory() || !isRecursiveLink(node.getStore(), info))
231238
addChildToTree(node, createChildNodeFromFileSystem(node, info));
@@ -321,14 +328,14 @@ protected Iterator<UnifiedTreeNode> getChildren(UnifiedTreeNode node) {
321328

322329
/* if the first child is still null, the node does not have any children */
323330
if (node.getFirstChild() == null)
324-
return EMPTY_ITERATOR;
331+
return Collections.emptyIterator();
325332

326333
/* get the index of the first child */
327334
int index = queue.indexOf(node.getFirstChild());
328335

329336
/* if we do not have children, just return an empty enumeration */
330337
if (index == -1)
331-
return EMPTY_ITERATOR;
338+
return Collections.emptyIterator();
332339

333340
/* create an enumeration with node's children */
334341
List<UnifiedTreeNode> result = new ArrayList<>(10);
@@ -350,7 +357,7 @@ protected int getLevel() {
350357
return level;
351358
}
352359

353-
protected IFileInfo[] getLocalList(UnifiedTreeNode node) {
360+
protected List<IFileInfo> getLocalList(UnifiedTreeNode node) {
354361
try {
355362
final IFileStore store = node.getStore();
356363
IFileInfo[] list;
@@ -360,15 +367,15 @@ protected IFileInfo[] getLocalList(UnifiedTreeNode node) {
360367
list = store.childInfos(EFS.NONE, null);
361368

362369
if (list == null || list.length == 0)
363-
return NO_CHILDREN;
370+
return List.of();
364371
list = ((Resource) node.getResource()).filterChildren(list, false);
365-
int size = list.length;
366-
if (size > 1)
367-
quickSort(list, 0, size - 1);
368-
return list;
372+
if (list.length > 1) {
373+
Arrays.sort(list);
374+
}
375+
return Arrays.asList(list);
369376
} catch (CoreException e) {
370377
//treat failure to access the directory as a non-existent directory
371-
return NO_CHILDREN;
378+
return List.of();
372379
}
373380
}
374381

@@ -402,11 +409,11 @@ protected boolean isLevelMarker(UnifiedTreeNode node) {
402409
private static class PatternHolder {
403410
//Initialize-on-demand Holder class to avoid compiling Pattern if never needed
404411
//Pattern: A UNIX or Windows relative path that just points backward
405-
private static final String REGEX = Platform.getOS().equals(Platform.OS_WIN32) ? "\\.[.\\\\]*" : "\\.[./]*"; //$NON-NLS-1$ //$NON-NLS-2$
406-
public static final Pattern TRIVIAL_SYMLINK_PATTERN = Pattern.compile(REGEX);
412+
static final Pattern TRIVIAL_SYMLINK_PATTERN = Pattern.compile( //
413+
Platform.OS.isWindows() ? "\\.[.\\\\]*" : "\\.[./]*"); //$NON-NLS-1$//$NON-NLS-2$
407414

408-
private static final String REGEX_BACK_REPEATING = Platform.getOS().equals(Platform.OS_WIN32) ? "(\\.\\.\\\\)+.*" : "(\\.\\./)+.*"; //$NON-NLS-1$ //$NON-NLS-2$
409-
public static final Pattern REPEATING_BACKWARDS_PATTERN = Pattern.compile(REGEX_BACK_REPEATING);
415+
static final Pattern REPEATING_BACKWARDS_PATTERN = Pattern.compile( //
416+
Platform.OS.isWindows() ? "(\\.\\.\\\\)+.*" : "(\\.\\./)+.*"); //$NON-NLS-1$//$NON-NLS-2$
410417
}
411418

412419
/**
@@ -541,44 +548,12 @@ private boolean isRecursiveLink(IFileStore parentStore, IFileInfo localInfo) {
541548
}
542549

543550
protected boolean isValidLevel(int currentLevel, int depth) {
544-
switch (depth) {
545-
case IResource.DEPTH_INFINITE :
546-
return true;
547-
case IResource.DEPTH_ONE :
548-
return currentLevel <= 1;
549-
case IResource.DEPTH_ZERO :
550-
return currentLevel == 0;
551-
default :
552-
return currentLevel + 1000 <= depth;
553-
}
554-
}
555-
556-
/**
557-
* Sorts the given array of strings in place. This is
558-
* not using the sorting framework to avoid casting overhead.
559-
*/
560-
protected void quickSort(IFileInfo[] infos, int left, int right) {
561-
int originalLeft = left;
562-
int originalRight = right;
563-
IFileInfo mid = infos[(left + right) / 2];
564-
do {
565-
while (mid.compareTo(infos[left]) > 0)
566-
left++;
567-
while (infos[right].compareTo(mid) > 0)
568-
right--;
569-
if (left <= right) {
570-
IFileInfo tmp = infos[left];
571-
infos[left] = infos[right];
572-
infos[right] = tmp;
573-
left++;
574-
right--;
575-
}
576-
} while (left <= right);
577-
if (originalLeft < right)
578-
quickSort(infos, originalLeft, right);
579-
if (left < originalRight)
580-
quickSort(infos, left, originalRight);
581-
return;
551+
return switch (depth) {
552+
case IResource.DEPTH_INFINITE -> true;
553+
case IResource.DEPTH_ONE -> currentLevel <= 1;
554+
case IResource.DEPTH_ZERO -> currentLevel == 0;
555+
default -> currentLevel + 1000 <= depth;
556+
};
582557
}
583558

584559
/**

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/AliasManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ public void startup(IProgressMonitor monitor) {
608608
* @param depth whether to search for aliases on all children of the given
609609
* resource. Only depth ZERO and INFINITE are used.
610610
*/
611-
@SuppressWarnings({"unchecked"})
612611
public void updateAliases(IResource resource, IFileStore location, int depth, IProgressMonitor monitor) throws CoreException {
612+
monitor = IProgressMonitor.nullSafe(monitor);
613613
if (hasNoAliases(resource))
614614
return;
615615
aliases.clear();
@@ -620,8 +620,7 @@ public void updateAliases(IResource resource, IFileStore location, int depth, IP
620620
if (aliases.isEmpty())
621621
return;
622622
FileSystemResourceManager localManager = workspace.getFileSystemManager();
623-
HashSet<IResource> aliasesCopy = (HashSet<IResource>) aliases.clone();
624-
for (IResource alias : aliasesCopy) {
623+
for (IResource alias : new ArrayList<>(aliases)) {
625624
monitor.subTask(NLS.bind(Messages.links_updatingDuplicate, alias.getFullPath()));
626625
if (alias.getType() == IResource.PROJECT) {
627626
if (checkDeletion((Project) alias, location))

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,8 +2316,7 @@ public IStatus open(IProgressMonitor monitor) throws CoreException {
23162316
// create root location
23172317
localMetaArea.locationFor(getRoot()).toFile().mkdirs();
23182318

2319-
SubMonitor subMonitor = SubMonitor.convert(null);
2320-
startup(subMonitor);
2319+
startup(new NullProgressMonitor());
23212320
// restart the notification manager so it is initialized with the right tree
23222321
notificationManager.startup(null);
23232322
openFlag = true;

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/FileUtil.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@
3939
import org.eclipse.core.runtime.CoreException;
4040
import org.eclipse.core.runtime.IPath;
4141
import org.eclipse.core.runtime.IProgressMonitor;
42+
import org.eclipse.core.runtime.Platform;
4243
import org.eclipse.core.runtime.SubMonitor;
4344
import org.eclipse.core.runtime.content.IContentDescription;
44-
import org.eclipse.osgi.service.environment.Constants;
4545
import org.eclipse.osgi.util.NLS;
4646

4747
/**
4848
* Static utility methods for manipulating Files and URIs.
4949
*/
5050
public class FileUtil {
51-
static final boolean MACOSX = Constants.OS_MACOSX.equals(getOS());
51+
static final boolean MACOSX = Platform.OS.isMac();
5252

5353
/**
5454
* Converts a ResourceAttributes object into an IFileInfo object.
@@ -157,14 +157,6 @@ public static IPath realPath(IPath path) {
157157
return realPath.equals(path) ? path : realPath;
158158
}
159159

160-
/**
161-
* Returns the current OS. Equivalent to Platform.getOS(), but tolerant of the platform runtime
162-
* not being present.
163-
*/
164-
private static String getOS() {
165-
return System.getProperty("osgi.os", ""); //$NON-NLS-1$ //$NON-NLS-2$
166-
}
167-
168160
/**
169161
* Converts a URI into its canonical form.
170162
*/

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IFileTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
import org.eclipse.core.runtime.Platform.OS;
6868
import org.eclipse.core.runtime.QualifiedName;
6969
import org.eclipse.core.tests.harness.FussyProgressMonitor;
70-
import org.eclipse.osgi.service.environment.Constants;
7170
import org.junit.Before;
7271
import org.junit.Rule;
7372
import org.junit.Test;
@@ -249,8 +248,8 @@ public void refreshFile(IFile file) throws CoreException, IOException {
249248
if (file.getProject().exists()) {
250249
file.getRawLocation().toFile().delete();
251250
createInFileSystem(file);
252-
if (Constants.OS_WIN32.equals(Platform.getOS())) {
253-
Files.setAttribute(file.getRawLocation().toFile().toPath(), "dos:hidden", Boolean.TRUE);
251+
if (Platform.OS.isWindows()) {
252+
Files.setAttribute(file.getRawLocation().toPath(), "dos:hidden", Boolean.TRUE);
254253
}
255254
}
256255
return;
@@ -263,8 +262,8 @@ public void refreshFile(IFile file) throws CoreException, IOException {
263262
if (file.getName().equals(WORKSPACE_ONLY_HIDDEN)) {
264263
file.getRawLocation().toFile().delete();
265264
createInFileSystem(file);
266-
if (Constants.OS_WIN32.equals(Platform.getOS())) {
267-
Files.setAttribute(file.getRawLocation().toFile().toPath(), "dos:hidden", Boolean.TRUE);
265+
if (Platform.OS.isWindows()) {
266+
Files.setAttribute(file.getRawLocation().toPath(), "dos:hidden", Boolean.TRUE);
268267
}
269268
file.refreshLocal(1, null);
270269
file.getRawLocation().toFile().delete();
@@ -284,8 +283,8 @@ public void refreshFile(IFile file) throws CoreException, IOException {
284283
}
285284
if (file.getName().equals(EXISTING_HIDDEN)) {
286285
createInWorkspace(file);
287-
if (Constants.OS_WIN32.equals(Platform.getOS())) {
288-
Files.setAttribute(file.getRawLocation().toFile().toPath(), "dos:hidden", Boolean.TRUE);
286+
if (Platform.OS.isWindows()) {
287+
Files.setAttribute(file.getRawLocation().toPath(), "dos:hidden", Boolean.TRUE);
289288
}
290289
file.refreshLocal(1, null);
291290
return;

0 commit comments

Comments
 (0)