Bug 574831 - Computation of module name in Java launch use wrong module

Change-Id: I23c54de4217364ccc8586609241a0747f32fdfd7
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.debug/+/187080
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Tested-by: Releng Bot <releng-bot@eclipse.org>
Reviewed-by: Sarika Sinha <sarika.sinha@in.ibm.com>
diff --git a/org.eclipse.jdt.debug.ui/plugin.properties b/org.eclipse.jdt.debug.ui/plugin.properties
index 62e444f..7a3f817 100644
--- a/org.eclipse.jdt.debug.ui/plugin.properties
+++ b/org.eclipse.jdt.debug.ui/plugin.properties
@@ -1,5 +1,5 @@
 ###############################################################################
-# Copyright (c) 2000, 2019 IBM Corporation and others.
+# Copyright (c) 2000, 2021 IBM Corporation and others.
 #
 # This program and the accompanying materials
 # are made available under the terms of the Eclipse Public License 2.0
@@ -265,6 +265,7 @@
 LaunchConfigurationITypeRenameParticipant.name=Launch configuration participant
 LaunchConfigurationITypeMoveParticipant.name=Launch configuration participant
 LaunchConfigurationIPackageFragmentMoveParticipant.name=Launch configuration participant
+LaunchConfigurationIModuleDescriptionRenameParticipant.name=Launch configuration participant
 JavaBreakpointITypeRenameParticipant.name=Breakpoint participant
 JavaBreakpointIJavaProjectRenameParticipant.name=Breakpoint participant
 JavaBreakpointIPackageFragmentRenameParticipant.name=Breakpoint participant
diff --git a/org.eclipse.jdt.debug.ui/plugin.xml b/org.eclipse.jdt.debug.ui/plugin.xml
index 5021241..6e88711 100644
--- a/org.eclipse.jdt.debug.ui/plugin.xml
+++ b/org.eclipse.jdt.debug.ui/plugin.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?eclipse version="3.0"?>
 <!--
-     Copyright (c) 2005, 2019 IBM Corporation and others.
+     Copyright (c) 2005, 2021 IBM Corporation and others.
 
      This program and the accompanying materials
      are made available under the terms of the Eclipse Public License 2.0
@@ -3152,6 +3152,21 @@
            </with>
          </enablement>
       </renameParticipant>
+      <renameParticipant
+            class="org.eclipse.jdt.internal.debug.core.refactoring.LaunchConfigurationIModuleDescriptionRenameParticipant"
+            name="%LaunchConfigurationIModuleDescriptionRenameParticipant.name"
+            id="org.eclipse.jdt.debug.refactoring.launchConfiguration.IModuleDescriptionRename">
+         <enablement>
+           <with variable="affectedNatures">
+             <iterate operator="or">
+               <equals value="org.eclipse.jdt.core.javanature"/>
+             </iterate>
+           </with>
+           <with variable="element">
+   	         <instanceof value="org.eclipse.jdt.core.IModuleDescription"/>
+           </with>
+         </enablement>
+      </renameParticipant>
    </extension>
    <extension point="org.eclipse.ltk.core.refactoring.moveParticipants">
       <moveParticipant
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/JDTDebugRefactoringUtil.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/JDTDebugRefactoringUtil.java
index 2e2e672..1baad98 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/JDTDebugRefactoringUtil.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/JDTDebugRefactoringUtil.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2021 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -22,6 +22,7 @@
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IModuleDescription;
 import org.eclipse.jdt.core.IPackageFragment;
 import org.eclipse.jdt.core.IPackageFragmentRoot;
 import org.eclipse.jdt.core.IType;
@@ -176,6 +177,31 @@
 	}
 
 	/**
+	 * Provides a public mechanism for creating the <code>Change</code> for renaming a module
+	 *
+	 * @param module
+	 *            the module to rename
+	 * @param newname
+	 *            the new name for the module
+	 * @return the Change for the module rename
+	 * @throws CoreException
+	 */
+	public static Change createChangesForModuleRename(IModuleDescription module, String newname) throws CoreException {
+		List<Change> changes = new ArrayList<>();
+		ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurationsForModule(module.getElementName());
+		LaunchConfigurationProjectMainTypeChange change = null;
+		for (int i = 0; i < configs.length; i++) {
+			change = new LaunchConfigurationProjectMainTypeChange(configs[i], null, newname);
+			String newcname = computeNewContainerName(configs[i]);
+			if (newcname != null) {
+				change.setNewContainerName(newcname);
+			}
+			changes.add(change);
+		}
+		return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7);
+	}
+
+	/**
 	 * Creates a <code>Change</code> for a type change
 	 * @param type the type that is changing
 	 * @param newfqname the new fully qualified name
@@ -278,4 +304,31 @@
 		return new ILaunchConfiguration[0];
 	}
 
+	/**
+	 * Returns a listing of configurations that have a specific module name attribute in them
+	 *
+	 * @param pname
+	 *            the module attribute to compare against
+	 * @return the list of java type launch configurations that have the specified module attribute
+	 */
+	protected static ILaunchConfiguration[] getJavaTypeLaunchConfigurationsForModule(String pname) {
+		try {
+			ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
+			ArrayList<ILaunchConfiguration> list = new ArrayList<>();
+			String attrib;
+			for (int i = 0; i < configs.length; i++) {
+				attrib = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_MODULE_NAME, (String) null);
+				if (attrib != null) {
+					if (attrib.equals(pname)) {
+						list.add(configs[i]);
+					}
+				}
+			}
+			return list.toArray(new ILaunchConfiguration[list.size()]);
+		} catch (CoreException e) {
+			JDIDebugPlugin.log(e);
+		}
+		return new ILaunchConfiguration[0];
+	}
+
 }
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationIModuleDescriptionRenameParticipant.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationIModuleDescriptionRenameParticipant.java
new file mode 100644
index 0000000..830425b
--- /dev/null
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/core/refactoring/LaunchConfigurationIModuleDescriptionRenameParticipant.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 2021 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.jdt.internal.debug.core.refactoring;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.core.IModuleDescription;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
+import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
+
+/**
+ * Provides a rename participant for module descriptions with respect to launch configurations
+ */
+public class LaunchConfigurationIModuleDescriptionRenameParticipant extends RenameParticipant {
+
+	/**
+	 * the module to rename
+	 */
+	private IModuleDescription fModuleDescription;
+
+	@Override
+	protected boolean initialize(Object element) {
+		fModuleDescription = (IModuleDescription) element;
+		return true;
+	}
+
+	@Override
+	public String getName() {
+		return RefactoringMessages.LaunchConfigurationParticipant_0;
+	}
+
+	@Override
+	public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context) {
+		return new RefactoringStatus();
+	}
+
+	@Override
+	public Change createChange(IProgressMonitor pm) throws CoreException {
+		return JDTDebugRefactoringUtil.createChangesForModuleRename(fModuleDescription, getArguments().getNewName());
+	}
+}