diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/AbstractResourceSnapshot.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/AbstractResourceSnapshot.java index 1e6a4f171c1..0e00f2fdc24 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/AbstractResourceSnapshot.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/AbstractResourceSnapshot.java @@ -34,7 +34,7 @@ * @since 3.20 * */ -abstract class AbstractResourceSnapshot implements IResourceSnapshot { +abstract class AbstractResourceSnapshot implements IResourceSnapshot { IContainer parent; long modificationStamp = IResource.NULL_STAMP; @@ -57,7 +57,7 @@ protected AbstractResourceSnapshot() { * * @param resource the resource to be described */ - protected AbstractResourceSnapshot(IResource resource) { + protected AbstractResourceSnapshot(T resource) { super(); parent = resource.getParent(); if (resource.isAccessible()) { @@ -81,9 +81,8 @@ protected AbstractResourceSnapshot(IResource resource) { } @Override - public IResource createResource(IProgressMonitor monitor) - throws CoreException { - IResource resource = createResourceHandle(); + public T createResource(IProgressMonitor monitor) throws CoreException { + T resource = createResourceHandle(); createExistentResourceFromHandle(resource, monitor); restoreResourceAttributes(resource); return resource; diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/ContainerSnapshot.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/ContainerSnapshot.java index eb0be2a2bd9..9b2bc6d18b4 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/ContainerSnapshot.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/ContainerSnapshot.java @@ -15,7 +15,11 @@ package org.eclipse.core.internal.resources.undo.snapshot; +import java.lang.reflect.Array; import java.net.URI; +import java.util.ArrayList; +import java.util.List; + import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; @@ -40,7 +44,7 @@ * * @since 3.20 */ -public abstract class ContainerSnapshot extends AbstractResourceSnapshot implements IContainerSnapshot { +public abstract class ContainerSnapshot extends AbstractResourceSnapshot implements IContainerSnapshot { String name; @@ -50,7 +54,7 @@ public abstract class ContainerSnapshot extends AbstractResourceSnapshot impleme String defaultCharSet; - IResourceSnapshot[] members; + final List> members = new ArrayList<>(); /** * Create a container snapshot from the specified container handle that can be @@ -62,7 +66,7 @@ public abstract class ContainerSnapshot extends AbstractResourceSnapshot impleme * parents. */ - public static ContainerSnapshot fromContainer(IContainer container) { + public static ContainerSnapshot fromContainer(R container) { return fromContainer(container, false); } @@ -76,22 +80,22 @@ public static ContainerSnapshot fromContainer(IContainer container) { * parents. */ - public static ContainerSnapshot fromVirtualFolderContainer(IContainer container) { + public static ContainerSnapshot fromVirtualFolderContainer(R container) { return fromContainer(container, true); } - public static ContainerSnapshot fromContainer(IContainer container, boolean usingVirtualFolder) { + @SuppressWarnings("unchecked") + public static ContainerSnapshot fromContainer(R container, boolean usingVirtualFolder) { IPath fullPath = container.getFullPath(); - ContainerSnapshot firstCreatedParent = null; - ContainerSnapshot currentContainerDescription = null; + ContainerSnapshot firstCreatedParent = null; + ContainerSnapshot currentContainerDescription = null; // Does the container exist already? If so, then the parent exists and // we use the normal creation constructor. IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IContainer currentContainer = (IContainer) root.findMember(fullPath); if (currentContainer != null) { - return (ContainerSnapshot) ResourceSnapshotFactory - .fromResource(container); + return (ContainerSnapshot) ResourceSnapshotFactory.fromResource(container); } // Create container descriptions for any uncreated parents in the given @@ -105,12 +109,11 @@ public static ContainerSnapshot fromContainer(IContainer container, boolean usin currentContainer = (IContainer) resource; } else if (i == 0) { // parent does not exist and it is a project - firstCreatedParent = new ProjectSnapshot(root - .getProject(currentSegment)); + firstCreatedParent = new ProjectSnapshot(root.getProject(currentSegment)); currentContainerDescription = firstCreatedParent; } else { IFolder folderHandle = currentContainer.getFolder(IPath.fromOSString(currentSegment)); - ContainerSnapshot currentFolder; + FolderSnapshot currentFolder; currentFolder = new FolderSnapshot(folderHandle, usingVirtualFolder); currentContainer = folderHandle; if (currentContainerDescription != null) { @@ -122,7 +125,7 @@ public static ContainerSnapshot fromContainer(IContainer container, boolean usin } } } - return firstCreatedParent; + return (ContainerSnapshot)firstCreatedParent; } /** @@ -139,7 +142,7 @@ public ContainerSnapshot() { * * @param container the container to be described */ - public ContainerSnapshot(IContainer container) { + public ContainerSnapshot(T container) { super(container); this.name = container.getName(); if (container.isLinked()) { @@ -149,10 +152,8 @@ public ContainerSnapshot(IContainer container) { if (container.isAccessible()) { defaultCharSet = container.getDefaultCharset(false); IResource[] resourceMembers = container.members(); - members = new AbstractResourceSnapshot[resourceMembers.length]; - for (int i = 0; i < resourceMembers.length; i++) { - members[i] = (AbstractResourceSnapshot) ResourceSnapshotFactory - .fromResource(resourceMembers[i]); + for (IResource resourceMember : resourceMembers) { + members.add(ResourceSnapshotFactory.fromResource(resourceMember)); } } } catch (CoreException e) { @@ -173,31 +174,29 @@ public ContainerSnapshot(IContainer container) { protected final void createChildResources(IContainer parentHandle, IProgressMonitor monitor) throws CoreException { // restore any children - if (members != null && members.length > 0) { - SubMonitor subMonitor = SubMonitor.convert(monitor, members.length); - for (IResourceSnapshot member : members) { - if (member instanceof AbstractResourceSnapshot) - ((AbstractResourceSnapshot) member).parent = parentHandle; - member.createResource(subMonitor.split(1)); - } + SubMonitor subMonitor = SubMonitor.convert(monitor, members.size()); + for (IResourceSnapshot member : members) { + if (member instanceof AbstractResourceSnapshot) + ((AbstractResourceSnapshot) member).parent = parentHandle; + member.createResource(subMonitor.split(1)); } } @Override - public void recordStateFromHistory(IResource resource, IProgressMonitor mon) throws CoreException { + public void recordStateFromHistory(T resource, IProgressMonitor mon) throws CoreException { if (members != null) { SubMonitor subMonitor = SubMonitor.convert(mon, ResourceSnapshotMessages.FolderDescription_SavingUndoInfoProgress, - members.length); - for (IResourceSnapshot member : members) { + members.size()); + for (IResourceSnapshot member : members) { SubMonitor iterationMonitor = subMonitor.split(1); - if (member instanceof FileSnapshot) { - IPath path = resource.getFullPath().append(((FileSnapshot) member).name); + if (member instanceof FileSnapshot fileSnapshot) { + IPath path = resource.getFullPath().append(fileSnapshot.name); IFile fileHandle = resource.getWorkspace().getRoot().getFile(path); - member.recordStateFromHistory(fileHandle, iterationMonitor); - } else if (member instanceof FolderSnapshot) { - IPath path = resource.getFullPath().append(((FolderSnapshot) member).name); + fileSnapshot.recordStateFromHistory(fileHandle, iterationMonitor); + } else if (member instanceof FolderSnapshot folderSnapshot) { + IPath path = resource.getFullPath().append(folderSnapshot.name); IFolder folderHandle = resource.getWorkspace().getRoot().getFolder(path); - member.recordStateFromHistory(folderHandle, iterationMonitor); + folderSnapshot.recordStateFromHistory(folderHandle, iterationMonitor); } } } @@ -213,11 +212,10 @@ public String getName() { return name; } + @SuppressWarnings("unchecked") @Override - public IResourceSnapshot[] getMembers() { - AbstractResourceSnapshot[] clone = new AbstractResourceSnapshot[members.length]; - System.arraycopy(members, 0, clone, 0, members.length); - return clone; + public IResourceSnapshot[] getMembers() { + return members.toArray((IResourceSnapshot[]) Array.newInstance(IResourceSnapshot.class, members.size())); } /** @@ -226,15 +224,8 @@ public IResourceSnapshot[] getMembers() { * @param member the resource snapshot considered a member of this container. */ @Override - public void addMember(IResourceSnapshot member) { - if (members == null) { - members = new IResourceSnapshot[] { member }; - } else { - IResourceSnapshot[] expandedMembers = new AbstractResourceSnapshot[members.length + 1]; - System.arraycopy(members, 0, expandedMembers, 0, members.length); - expandedMembers[members.length] = member; - members = expandedMembers; - } + public void addMember(IResourceSnapshot member) { + members.add(member); } @Override @@ -274,11 +265,9 @@ public boolean verifyExistence(boolean checkMembers) { if (existence) { if (checkMembers) { // restore any children - if (members != null && members.length > 0) { - for (IResourceSnapshot member : members) { - if (!member.verifyExistence(checkMembers)) { - return false; - } + for (IResourceSnapshot member : members) { + if (!member.verifyExistence(checkMembers)) { + return false; } } } diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/FileSnapshot.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/FileSnapshot.java index 4e60c7b65e8..7ada4b15001 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/FileSnapshot.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/FileSnapshot.java @@ -39,7 +39,7 @@ * @since 3.20 * */ -public class FileSnapshot extends AbstractResourceSnapshot { +public class FileSnapshot extends AbstractResourceSnapshot { String name; @@ -94,15 +94,14 @@ public FileSnapshot(IFile file, URI linkLocation, } @Override - public void recordStateFromHistory(IResource resource, + public void recordStateFromHistory(IFile resource, IProgressMonitor monitor) throws CoreException { Assert.isLegal(resource.getType() == IResource.FILE); - if (location != null) { // file is linked, no need to record any history return; } - IFileState[] states = ((IFile) resource).getHistory(monitor); + IFileState[] states = resource.getHistory(monitor); if (states.length > 0) { final IFileState state = getMatchingFileState(states); this.fileContentDescription = new IFileContentSnapshot() { @@ -125,20 +124,17 @@ public String getCharset() throws CoreException { } @Override - public IResource createResourceHandle() { + public IFile createResourceHandle() { IWorkspaceRoot workspaceRoot = parent.getWorkspace().getRoot(); IPath fullPath = parent.getFullPath().append(name); return workspaceRoot.getFile(fullPath); } @Override - public void createExistentResourceFromHandle(IResource resource, IProgressMonitor mon) throws CoreException { - - Assert.isLegal(resource instanceof IFile); - if (resource.exists()) { + public void createExistentResourceFromHandle(IFile fileHandle, IProgressMonitor mon) throws CoreException { + if (fileHandle.exists()) { return; } - IFile fileHandle = (IFile) resource; SubMonitor subMonitor = SubMonitor.convert(mon, 200); subMonitor.setTaskName(ResourceSnapshotMessages.FileDescription_NewFileProgress); try { diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/FolderSnapshot.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/FolderSnapshot.java index 2b094bbd956..bb06251a2a6 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/FolderSnapshot.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/FolderSnapshot.java @@ -16,11 +16,11 @@ package org.eclipse.core.internal.resources.undo.snapshot; import java.net.URI; + import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceFilterDescription; import org.eclipse.core.resources.IWorkspaceRoot; -import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; @@ -35,7 +35,7 @@ * @since 3.20 * */ -public class FolderSnapshot extends ContainerSnapshot { +public class FolderSnapshot extends ContainerSnapshot { private boolean virtual = false; @@ -67,19 +67,17 @@ public FolderSnapshot(IFolder folder, URI linkLocation) { } @Override - public IResource createResourceHandle() { + public IFolder createResourceHandle() { IWorkspaceRoot workspaceRoot = getWorkspace().getRoot(); IPath folderPath = parent.getFullPath().append(name); return workspaceRoot.getFolder(folderPath); } @Override - public void createExistentResourceFromHandle(IResource resource, IProgressMonitor mon) throws CoreException { - Assert.isLegal(resource instanceof IFolder); - if (resource.exists()) { + public void createExistentResourceFromHandle(final IFolder folderHandle, IProgressMonitor mon) throws CoreException { + if (folderHandle.exists()) { return; } - IFolder folderHandle = (IFolder) resource; SubMonitor subMonitor = SubMonitor.convert(mon, 300); subMonitor.setTaskName(ResourceSnapshotMessages.FolderDescription_NewFolderProgress); if (filters != null) { diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/ProjectSnapshot.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/ProjectSnapshot.java index 59623f9180a..65e01f8410b 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/ProjectSnapshot.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/undo/snapshot/ProjectSnapshot.java @@ -33,7 +33,7 @@ * @since 3.20 * */ -public class ProjectSnapshot extends ContainerSnapshot { +public class ProjectSnapshot extends ContainerSnapshot { private IProjectDescription projectDescription; private boolean openOnCreate = true; @@ -73,19 +73,16 @@ public ProjectSnapshot(IProjectDescription projectDescription) { } @Override - public IResource createResourceHandle() { + public IProject createResourceHandle() { return ResourcesPlugin.getWorkspace().getRoot().getProject(getName()); } @Override - public void createExistentResourceFromHandle(IResource resource, - IProgressMonitor monitor) throws CoreException { + public void createExistentResourceFromHandle(IProject projectHandle, IProgressMonitor monitor) throws CoreException { SubMonitor subMonitor = SubMonitor.convert(monitor, 200); - Assert.isLegal(resource instanceof IProject); - if (resource.exists()) { + if (projectHandle.exists()) { return; } - IProject projectHandle = (IProject) resource; subMonitor.setTaskName(ResourceSnapshotMessages.FolderDescription_NewFolderProgress); if (projectDescription == null) { projectHandle.create(subMonitor.split(100)); @@ -109,7 +106,7 @@ public String getName() { @Override public boolean verifyExistence(boolean checkMembers) { // We can only check members if the project is open. - IProject projectHandle = (IProject) createResourceHandle(); + IProject projectHandle = createResourceHandle(); if (projectHandle.isAccessible()) { return super.verifyExistence(checkMembers); } diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/IContainerSnapshot.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/IContainerSnapshot.java index 4843f338893..18cea8e7a37 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/IContainerSnapshot.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/IContainerSnapshot.java @@ -15,6 +15,9 @@ package org.eclipse.core.resources.undo.snapshot; import java.net.URI; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceFilterDescription; /** @@ -25,14 +28,14 @@ * @noextend This interface is not intended to be extended by clients. * @since 3.20 */ -public interface IContainerSnapshot extends IResourceSnapshot { +public interface IContainerSnapshot extends IResourceSnapshot { /** * Get a list of snapshots of members of this container * * @return a list of snapshots */ - public IResourceSnapshot[] getMembers(); + public IResourceSnapshot[] getMembers(); /** * Add the specified resource description as a member of this resource @@ -40,7 +43,7 @@ public interface IContainerSnapshot extends IResourceSnapshot { * * @param member the resource description considered a member of this container. */ - public void addMember(IResourceSnapshot member); + public void addMember(IResourceSnapshot member); /** * Set the location to which this container is linked. diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/IResourceSnapshot.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/IResourceSnapshot.java index b95ad260a32..0ba394767d9 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/IResourceSnapshot.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/IResourceSnapshot.java @@ -28,7 +28,7 @@ * @since 3.20 * */ -public interface IResourceSnapshot { +public interface IResourceSnapshot { /** * Create a resource handle that can be used to create a resource from this @@ -38,7 +38,7 @@ public interface IResourceSnapshot { * @return the resource handle that can be used to create a resource from * this description */ - public IResource createResourceHandle(); + public T createResourceHandle(); /** * Get the name of this resource. @@ -55,7 +55,7 @@ public interface IResourceSnapshot { * @return a resource that has the attributes of this resource description * @throws CoreException if creation failed */ - public IResource createResource(IProgressMonitor monitor) throws CoreException; + public T createResource(IProgressMonitor monitor) throws CoreException; /** * Given a resource handle, create an actual resource with the attributes of @@ -67,8 +67,7 @@ public interface IResourceSnapshot { * the progress monitor to be used when creating the resource * @throws CoreException if creation failed */ - public void createExistentResourceFromHandle(IResource resource, - IProgressMonitor monitor) throws CoreException; + public void createExistentResourceFromHandle(T resource, IProgressMonitor monitor) throws CoreException; /** * Return a boolean indicating whether this resource description has enough @@ -89,8 +88,7 @@ public void createExistentResourceFromHandle(IResource resource, * the progress monitor to be used * @throws CoreException in case of error */ - public void recordStateFromHistory(IResource resource, - IProgressMonitor monitor) throws CoreException; + public void recordStateFromHistory(T resource, IProgressMonitor monitor) throws CoreException; /** * Return a boolean indicating whether this description represents an diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/ResourceSnapshotFactory.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/ResourceSnapshotFactory.java index 7ffe66d9cc7..e280e060cb7 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/ResourceSnapshotFactory.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/undo/snapshot/ResourceSnapshotFactory.java @@ -47,13 +47,14 @@ public class ResourceSnapshotFactory { * @param resource the resource from which a description should be created * @return the resource description */ - public static IResourceSnapshot fromResource(IResource resource) { + @SuppressWarnings("unchecked") + public static IResourceSnapshot fromResource(T resource) { if (resource.getType() == IResource.PROJECT) { - return new ProjectSnapshot((IProject) resource); + return (IResourceSnapshot) new ProjectSnapshot((IProject) resource); } else if (resource.getType() == IResource.FOLDER) { - return new FolderSnapshot((IFolder) resource, resource.isVirtual()); + return (IResourceSnapshot) new FolderSnapshot((IFolder) resource, resource.isVirtual()); } else if (resource.getType() == IResource.FILE) { - return new FileSnapshot((IFile) resource); + return (IResourceSnapshot) new FileSnapshot((IFile) resource); } else { throw new IllegalArgumentException(); } @@ -65,7 +66,7 @@ public static IResourceSnapshot fromResource(IResource resource) { * * @param projectDescription the project description for the future project */ - public static IContainerSnapshot fromProjectDescription(IProjectDescription projectDescription) { + public static IContainerSnapshot fromProjectDescription(IProjectDescription projectDescription) { return new ProjectSnapshot(projectDescription); } @@ -78,7 +79,7 @@ public static IContainerSnapshot fromProjectDescription(IProjectDescription proj * @return a container description describing the container and any non-existing * parents. */ - public static IContainerSnapshot fromContainer(IContainer container) { + public static IContainerSnapshot fromContainer(IContainer container) { return ContainerSnapshot.fromContainer(container); } @@ -91,7 +92,7 @@ public static IContainerSnapshot fromContainer(IContainer container) { * @return a container description describing the container and any non-existing * parents. */ - public static IContainerSnapshot fromVirtualFolderContainer(IContainer container) { + public static IContainerSnapshot fromVirtualFolderContainer(IContainer container) { return ContainerSnapshot.fromContainer(container, true); } @@ -107,7 +108,7 @@ public static IContainerSnapshot fromVirtualFolderContainer(IContainer container * the file is not linked * @param contents an input stream representing the contents of the file */ - public static IResourceSnapshot fromFileDetails(IFile file, URI linkLocation, InputStream contents) { + public static IResourceSnapshot fromFileDetails(IFile file, URI linkLocation, InputStream contents) { return new FileSnapshot(file, linkLocation, createFileContentDescription(file, contents)); }