-
Notifications
You must be signed in to change notification settings - Fork 128
Handle deletion scenario for cleaning up empty exported packages #2292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
....pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestPackageDeleteParticipant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation and others. | ||
| * | ||
| * This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License 2.0 | ||
| * which accompanies this distribution, and is available at | ||
| * https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.pde.internal.ui.refactoring; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.core.resources.IProject; | ||
| import org.eclipse.core.runtime.CoreException; | ||
| import org.eclipse.core.runtime.IProgressMonitor; | ||
| import org.eclipse.jdt.core.IPackageFragment; | ||
| import org.eclipse.jdt.core.JavaModelException; | ||
| import org.eclipse.ltk.core.refactoring.Change; | ||
| import org.eclipse.ltk.core.refactoring.CompositeChange; | ||
| import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments; | ||
| import org.eclipse.pde.internal.core.WorkspaceModelManager; | ||
| import org.eclipse.pde.internal.core.project.PDEProject; | ||
| import org.eclipse.pde.internal.ui.PDEPlugin; | ||
| import org.eclipse.pde.internal.ui.PDEUIMessages; | ||
|
|
||
| /** | ||
| * Handles package deletion in PDE plugin projects. Updates the bundle's | ||
| * MANIFEST.MF after deleting packages. | ||
| */ | ||
| public class ManifestPackageDeleteParticipant extends PDEDeleteParticipant { | ||
|
|
||
| private IProject fProject; | ||
| private Set<IPackageFragment> fPackages = new HashSet<>(); | ||
|
|
||
| @Override | ||
| protected boolean initialize(Object element) { | ||
| IPackageFragment fragment = (IPackageFragment) element; | ||
| try { | ||
| if (!fragment.containsJavaResources()) { | ||
| return false; | ||
| } | ||
| } catch (JavaModelException e) { | ||
| PDEPlugin.logException(e); | ||
| } | ||
| fProject = fragment.getJavaProject().getProject(); | ||
| if (WorkspaceModelManager.isPluginProject(fProject)) { | ||
| fPackages.add(fragment); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void addElement(Object element, RefactoringArguments arguments) { | ||
| fPackages.add((IPackageFragment) element); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return PDEUIMessages.ManifestPackageDeleteParticipant_packageDelete; | ||
| } | ||
|
|
||
| @Override | ||
| protected void addChanges(CompositeChange result, IProgressMonitor pm) throws CoreException { | ||
| IFile file = PDEProject.getManifest(fProject); | ||
| if (file.exists()) { | ||
| Change change = BundleManifestChange.createDeletePackagesChange(file, fPackages, pm); | ||
| if (change != null) { | ||
| result.add(change); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
131 changes: 131 additions & 0 deletions
131
...pse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestTypeDeleteParticipant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation and others. | ||
| * | ||
| * This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License 2.0 | ||
| * which accompanies this distribution, and is available at | ||
| * https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.pde.internal.ui.refactoring; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.core.resources.IProject; | ||
| import org.eclipse.core.runtime.CoreException; | ||
| import org.eclipse.core.runtime.IProgressMonitor; | ||
| import org.eclipse.jdt.core.ICompilationUnit; | ||
| import org.eclipse.jdt.core.IJavaProject; | ||
| import org.eclipse.jdt.core.IPackageFragment; | ||
| import org.eclipse.jdt.core.IType; | ||
| import org.eclipse.ltk.core.refactoring.Change; | ||
| import org.eclipse.ltk.core.refactoring.CompositeChange; | ||
| import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments; | ||
| import org.eclipse.pde.internal.core.WorkspaceModelManager; | ||
| import org.eclipse.pde.internal.core.project.PDEProject; | ||
| import org.eclipse.pde.internal.ui.PDEUIMessages; | ||
|
|
||
| /** | ||
| * Handles class deletion in PDE plugin projects. Updates the bundle's MANIFEST.MF | ||
| * when a package becomes empty after deleting types. | ||
| */ | ||
| public class ManifestTypeDeleteParticipant extends PDEDeleteParticipant { | ||
|
HannesWell marked this conversation as resolved.
|
||
|
|
||
| private IProject fProject; | ||
| private Set<IType> fTypes = new LinkedHashSet<>(); | ||
|
|
||
| @Override | ||
| protected boolean initialize(Object element) { | ||
| IType type = (IType) element; | ||
| IJavaProject javaProject = type.getJavaProject(); | ||
| if (javaProject != null) { | ||
| fProject = javaProject.getProject(); | ||
| if (WorkspaceModelManager.isPluginProject(fProject)) { | ||
| fTypes.add(type); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void addElement(Object element, RefactoringArguments arguments) { | ||
| IType type = (IType) element; | ||
| fTypes.add(type); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return PDEUIMessages.ManifestTypeDeleteParticipant_composite; | ||
| } | ||
|
|
||
| @Override | ||
| protected void addChanges(CompositeChange result, IProgressMonitor pm) throws CoreException { | ||
| IFile file = PDEProject.getManifest(fProject); | ||
| if (!file.exists()) { | ||
| return; | ||
| } | ||
| // Group deleted types by package and collect their compilation units | ||
| Map<IPackageFragment, Set<ICompilationUnit>> deletedCUsByPackage = new HashMap<>(); | ||
| for (IType type : fTypes) { | ||
| IPackageFragment pkg = type.getPackageFragment(); | ||
| ICompilationUnit cu = type.getCompilationUnit(); | ||
| if (cu != null) { | ||
| deletedCUsByPackage.computeIfAbsent(pkg, k -> new HashSet<>()).add(cu); | ||
| } | ||
| } | ||
| // Check each package to see if it becomes empty after deletion | ||
| List<IPackageFragment> emptiedPackages = deletedCUsByPackage.entrySet().stream().filter(e -> { | ||
| IPackageFragment pkg = e.getKey(); | ||
| Set<ICompilationUnit> deletedCUs = e.getValue(); | ||
| try { | ||
| return willPackageBeEmpty(pkg, deletedCUs); | ||
| } catch (CoreException ex) { | ||
| return false; | ||
| } | ||
| }).map(Map.Entry::getKey).toList(); | ||
|
|
||
| Change change = BundleManifestChange.createDeletePackagesChange(file, emptiedPackages, pm); | ||
| if (change != null) { | ||
| result.add(change); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Checks if a package will be empty after deleting the specified compilation units. | ||
| * | ||
| * @param pkg the package to check | ||
| * @param deletedCUs the compilation units being deleted | ||
| * @return true if the package will be empty after deletion | ||
| * @throws CoreException if an error occurs accessing package contents | ||
| */ | ||
| private boolean willPackageBeEmpty(IPackageFragment pkg, Set<ICompilationUnit> deletedCUs) throws CoreException { | ||
| // Check for non-Java resources (properties files, XML files, etc.) | ||
| Object[] nonJavaResources = pkg.getNonJavaResources(); | ||
| if (nonJavaResources != null && nonJavaResources.length > 0) { | ||
| return false; | ||
| } | ||
| // Check if any compilation unit in the package is NOT being deleted | ||
| ICompilationUnit[] compilationUnits = pkg.getCompilationUnits(); | ||
| for (ICompilationUnit cu : compilationUnits) { | ||
| if (!deletedCUs.contains(cu)) { | ||
| // This compilation unit is not being deleted, so package is not | ||
| // empty | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/PDEDeleteParticipant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation and others. | ||
| * | ||
| * This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License 2.0 | ||
| * which accompanies this distribution, and is available at | ||
| * https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.pde.internal.ui.refactoring; | ||
|
|
||
| import org.eclipse.core.runtime.CoreException; | ||
| import org.eclipse.core.runtime.IProgressMonitor; | ||
| import org.eclipse.core.runtime.OperationCanceledException; | ||
| import org.eclipse.ltk.core.refactoring.Change; | ||
| import org.eclipse.ltk.core.refactoring.CompositeChange; | ||
| import org.eclipse.ltk.core.refactoring.RefactoringStatus; | ||
| import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; | ||
| import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant; | ||
| import org.eclipse.ltk.core.refactoring.participants.ISharableParticipant; | ||
|
|
||
| public abstract class PDEDeleteParticipant extends DeleteParticipant implements ISharableParticipant { | ||
|
|
||
| @Override | ||
| public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context) | ||
| throws OperationCanceledException { | ||
| return new RefactoringStatus(); | ||
| } | ||
|
|
||
| @Override | ||
| public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { | ||
| CompositeChange result = new CompositeChange(getName()); | ||
| addChanges(result, pm); | ||
| return result.getChildren().length == 0 ? null : result; | ||
| } | ||
|
|
||
| protected abstract void addChanges(CompositeChange result, IProgressMonitor pm) throws CoreException; | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.