|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 IBM Corporation and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.pde.internal.ui.refactoring; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.eclipse.core.resources.IFile; |
| 24 | +import org.eclipse.core.resources.IFolder; |
| 25 | +import org.eclipse.core.resources.IProject; |
| 26 | +import org.eclipse.core.resources.IResource; |
| 27 | +import org.eclipse.core.runtime.CoreException; |
| 28 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 29 | +import org.eclipse.jdt.core.IJavaElement; |
| 30 | +import org.eclipse.jdt.core.IJavaProject; |
| 31 | +import org.eclipse.jdt.core.IPackageFragment; |
| 32 | +import org.eclipse.jdt.core.IType; |
| 33 | +import org.eclipse.ltk.core.refactoring.Change; |
| 34 | +import org.eclipse.ltk.core.refactoring.CompositeChange; |
| 35 | +import org.eclipse.pde.internal.core.WorkspaceModelManager; |
| 36 | +import org.eclipse.pde.internal.core.project.PDEProject; |
| 37 | +import org.eclipse.pde.internal.ui.PDEUIMessages; |
| 38 | + |
| 39 | +public class ManifestTypeDeleteParticipant extends PDEDeleteParticipant { |
| 40 | + |
| 41 | + @Override |
| 42 | + protected boolean initialize(Object element) { |
| 43 | + if (element instanceof IType type) { |
| 44 | + IJavaProject javaProject = (IJavaProject) type.getAncestor(IJavaElement.JAVA_PROJECT); |
| 45 | + IProject project = javaProject.getProject(); |
| 46 | + if (WorkspaceModelManager.isPluginProject(project)) { |
| 47 | + fProject = javaProject.getProject(); |
| 48 | + fElements = new HashMap<>(); |
| 49 | + fElements.put(element, type.getFullyQualifiedName()); |
| 50 | + return true; |
| 51 | + } |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getName() { |
| 58 | + return PDEUIMessages.ManifestTypeDeleteParticipant_composite; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void addChange(CompositeChange result, IProgressMonitor pm) throws CoreException { |
| 63 | + IFile file = PDEProject.getManifest(fProject); |
| 64 | + if (!file.exists()) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + Map<IPackageFragment, List<IType>> deletedByPackage = new HashMap<>(); |
| 69 | + fElements.forEach((element, fullyQualifiedName) -> { |
| 70 | + if (element instanceof IType type) { |
| 71 | + IPackageFragment pkg = type.getPackageFragment(); |
| 72 | + deletedByPackage.computeIfAbsent(pkg, k -> new ArrayList<>()).add(type); |
| 73 | + } |
| 74 | + }); |
| 75 | + // Check each package to see if it becomes empty after deletion |
| 76 | + for (Map.Entry<IPackageFragment, List<IType>> entry : deletedByPackage.entrySet()) { |
| 77 | + IPackageFragment pkg = entry.getKey(); |
| 78 | + List<IType> deletedTypes = entry.getValue(); |
| 79 | + |
| 80 | + try { |
| 81 | + if (willPackageBeEmpty(pkg, deletedTypes)) { |
| 82 | + Change change = BundleManifestChange.createEmptyPackageChange(file, pkg.getElementName(), pm); |
| 83 | + if (change != null) { |
| 84 | + result.add(change); |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (CoreException e) { |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Checks if a package will be empty after deleting the specified types. |
| 95 | + * @param pkg the package to check |
| 96 | + * @param deletedTypes the types being deleted |
| 97 | + * @return true if the package will be empty after deletion |
| 98 | + * @throws CoreException if an error occurs accessing package contents |
| 99 | + */ |
| 100 | + private boolean willPackageBeEmpty(IPackageFragment pkg, List<IType> deletedTypes) throws CoreException { |
| 101 | + IJavaElement[] javaChildren = pkg.getChildren(); |
| 102 | + if (javaChildren.length > deletedTypes.size()) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + // Check for non-Java resources (properties files, XML files, etc.) |
| 106 | + Object[] nonJavaResources = pkg.getNonJavaResources(); |
| 107 | + if (nonJavaResources != null && nonJavaResources.length > 0) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + Set<String> deletedJavaFileNames = new HashSet<>(); |
| 112 | + for (IType type : deletedTypes) { |
| 113 | + // Get the compilation unit (the .java file) containing this type |
| 114 | + if (type.getCompilationUnit() != null) { |
| 115 | + deletedJavaFileNames.add(type.getCompilationUnit().getElementName()); |
| 116 | + } |
| 117 | + } |
| 118 | + // Check the underlying folder for any OTHER files |
| 119 | + IResource resource = pkg.getCorrespondingResource(); |
| 120 | + if (resource instanceof IFolder folder) { |
| 121 | + IResource[] members = folder.members(); |
| 122 | + for (IResource member : members) { |
| 123 | + if (member instanceof IFile memberFile) { |
| 124 | + String fileName = memberFile.getName(); |
| 125 | + String extension = memberFile.getFileExtension(); |
| 126 | + if ("class".equals(extension)) { //$NON-NLS-1$ |
| 127 | + continue; |
| 128 | + } |
| 129 | + if ("java".equals(extension) && deletedJavaFileNames.contains(fileName)) { //$NON-NLS-1$ |
| 130 | + continue; |
| 131 | + } |
| 132 | + return false; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments