KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > refactoring > JDTDebugRefactoringUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.core.refactoring;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.ILaunchConfiguration;
21 import org.eclipse.jdt.core.IJavaElement;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
27 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
28 import org.eclipse.ltk.core.refactoring.Change;
29 import org.eclipse.ltk.core.refactoring.CompositeChange;
30
31 /**
32  *
33  * provides methods to create refactoring changes
34  */

35 public class JDTDebugRefactoringUtil {
36     
37     /**
38      * Take a list of Changes, and return a unique Change, a CompositeChange, or null.
39      */

40     public static Change createChangeFromList(List JavaDoc changes, String JavaDoc changeLabel) {
41         int nbChanges= changes.size();
42         if (nbChanges == 0) {
43             return null;
44         }
45         else if (nbChanges == 1) {
46             return (Change) changes.get(0);
47         }
48         else {
49             return new CompositeChange(changeLabel, (Change[])changes.toArray(new Change[changes.size()]));
50         }
51     }
52     
53     /**
54      * Returns the new container name for the given project and launch configuration
55      * @param javaProject the java to get the new container name for
56      * @param launchConfiguration the associated launch configuration
57      * @return the new container name
58      * @since 3.2
59      */

60     protected static String JavaDoc computeNewContainerName(IJavaProject javaProject, ILaunchConfiguration launchConfiguration) {
61         IPath currentLocation = launchConfiguration.getLocation();
62         IPath projectLocation = javaProject.getProject().getLocation();
63         if (projectLocation.isPrefixOf(currentLocation)) {
64             String JavaDoc projectFile = new File JavaDoc(projectLocation.toOSString()).getAbsolutePath();
65             String JavaDoc configDir = new File JavaDoc(currentLocation.toOSString()).getParent();
66             return new String JavaDoc(configDir.substring(projectFile.length()));
67         }
68         return null;
69     }
70     
71     /**
72      * Returns a change for the given launch configuration if the launch configuration needs to
73      * be updated for this IType change. It specifically looks to see if the main type of the launch configuration
74      * is an inner type of the given IType.
75      * @param config the launch configuration
76      * @param type the type to check for
77      * @param newfqname the new fully qualified name
78      * @param pname the new project name
79      * @return the <code>Change</code> for this outer type
80      * @throws CoreException
81      * @since 3.2
82      */

83     protected static Change createChangesForOuterTypeChange(ILaunchConfiguration config, IType type, String JavaDoc newfqname, String JavaDoc pname) throws CoreException {
84         IType[] innerTypes = type.getTypes();
85         String JavaDoc mtname = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
86         for (int i= 0; i < innerTypes.length; i++) {
87             String JavaDoc newTypeName = newfqname + '$' + innerTypes[i].getElementName();
88             // if it matches, check the type
89
if (innerTypes[i].getFullyQualifiedName().equals(mtname)) {
90                 return new LaunchConfigurationProjectMainTypeChange(config, newTypeName, pname);
91             }
92             // if it's not the type, check the inner types
93
return createChangesForOuterTypeChange(config, innerTypes[i], newTypeName, pname);
94         }
95         return null;
96     }
97     
98     /**
99      * Provides a public mechanism for creating the <code>Change</code> for moving a package
100      * @param packageFragment the fragment to move
101      * @param destination the destination to move it to
102      * @return the <code>Change</code> for moving the package
103      * @throws CoreException
104      * @since 3.2
105      */

106     public static Change createChangesForPackageMove(IPackageFragment pfragment, IPackageFragmentRoot destination) throws CoreException {
107         List JavaDoc changes = new ArrayList JavaDoc();
108         ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurations(pfragment.getJavaProject().getElementName());
109         String JavaDoc mtname = null;
110         for (int i= 0; i < configs.length; i++) {
111             mtname = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
112             if(mtname != null) {
113                 if(mtname.lastIndexOf(pfragment.getElementName()) > -1) {
114                     changes.add(new LaunchConfigurationProjectMainTypeChange(configs[i], null, destination.getJavaProject().getElementName()));
115                 }
116             }
117         }
118         return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7);
119     }
120     
121     /**
122      * Provides a public mechanism for creating the <code>Change</code> for renaming a package
123      * @param packageFragment the fragment to rename
124      * @param newName the new name for the fragment
125      * @return the Change for the renaming
126      * @throws CoreException
127      * @since 3.2
128      */

129     public static Change createChangesForPackageRename(IPackageFragment pfragment, String JavaDoc newname) throws CoreException {
130         List JavaDoc changes = new ArrayList JavaDoc();
131         ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurations(pfragment.getJavaProject().getElementName());
132         String JavaDoc mtname;
133         for (int i= 0; i < configs.length; i++) {
134             mtname = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
135             if(mtname != null) {
136                 String JavaDoc pkname = ""; //$NON-NLS-1$
137
int index = mtname.lastIndexOf('.');
138                 if(index > 0) {
139                     pkname = mtname.substring(0, index);
140                 }
141                 if (pfragment.getElementName().equals(pkname)) {
142                     String JavaDoc ntname = newname + '.' + mtname.substring(index + 1);
143                     changes.add(new LaunchConfigurationProjectMainTypeChange(configs[i], ntname, null));
144                 }
145             }
146             else {
147                 changes.add(new LaunchConfigurationProjectMainTypeChange(configs[i], null, null));
148             }
149         }
150         return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7);
151     }
152     
153     /**
154      * Provides a public mechanism for creating the <code>Change</code> for renaming a project
155      * @param javaProject the project to rename
156      * @param newProjectName the new name for the project
157      * @return the Change for the project rename
158      * @throws CoreException
159      * @since 3.2
160      */

161     public static Change createChangesForProjectRename(IJavaProject project, String JavaDoc newname) throws CoreException {
162         List JavaDoc changes = new ArrayList JavaDoc();
163         ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurations(project.getElementName());
164         LaunchConfigurationProjectMainTypeChange change = null;
165         for (int i= 0; i < configs.length; i++) {
166             change = new LaunchConfigurationProjectMainTypeChange(configs[i], null, newname);
167             String JavaDoc newcname = computeNewContainerName(project, configs[i]);
168             if (newcname != null) {
169                 change.setNewContainerName(newcname);
170             }
171             changes.add(change);
172         }
173         return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7);
174     }
175     
176     /**
177      * Creates a <code>Change</code> for a type change
178      * @param type the type that is changing
179      * @param newfqname the new fully qualified name
180      * @param pname the project name
181      * @return the <code>Change</code> for changing the specified type
182      * @throws CoreException
183      * @since 3.2
184      */

185     protected static Change createChangesForTypeChange(IType type, String JavaDoc newfqname, String JavaDoc pname) throws CoreException {
186         List JavaDoc changes = new ArrayList JavaDoc();
187         String JavaDoc typename = type.getFullyQualifiedName();
188         ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurations(type.getJavaProject().getElementName());
189         String JavaDoc mtname;
190         for (int i= 0; i < configs.length; i++) {
191             mtname = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
192             if (typename.equals(mtname)) {
193                 changes.add(new LaunchConfigurationProjectMainTypeChange(configs[i], newfqname, pname));
194             }
195             else {
196                 Change change = createChangesForOuterTypeChange(configs[i], type, newfqname, pname);
197                 if (change != null) {
198                     changes.add(change);
199                 }
200             }
201         }
202         return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7);
203     }
204     
205     /**
206      * Provides a public mechanism for creating the <code>Change</code> for moving a type
207      * @param type the type being moved
208      * @param destination the destination to move the type to
209      * @return the <code>Change</code> for the type move
210      * @throws CoreException
211      * @since 3.2
212      */

213     public static Change createChangesForTypeMove(IType type, IJavaElement destination) throws CoreException {
214         IJavaProject pdestination = destination.getJavaProject();
215         String JavaDoc newpname = null;
216         if (!type.getJavaProject().equals(pdestination)) {
217             newpname = pdestination.getElementName();
218         }
219         String JavaDoc newfqname = type.getElementName();
220         if (destination instanceof IType) {
221             newfqname = ((IType)destination).getFullyQualifiedName() + '$' + type.getElementName();
222         }
223         else if (destination instanceof IPackageFragment) {
224             if (!((IPackageFragment) destination).isDefaultPackage()) {
225                 newfqname = destination.getElementName() + '.' + type.getElementName();
226             }
227         }
228         return createChangesForTypeChange(type, newfqname, newpname);
229     }
230     
231     /**
232      * Provides a public mechanism for creating the <code>Change</code> for renaming a type
233      * @param type the type to rename
234      * @param newname the new name for the type
235      * @return the <code>Change</code> for the type rename
236      * @throws CoreException
237      * @since 3.2
238      */

239     public static Change createChangesForTypeRename(IType type, String JavaDoc newname) throws CoreException {
240         IType dtype = type.getDeclaringType();
241         String JavaDoc newfqname = newname;
242         if (dtype == null) {
243             IPackageFragment packageFragment = type.getPackageFragment();
244             if (!packageFragment.isDefaultPackage()) {
245                 newfqname = packageFragment.getElementName() + '.' + newname;
246             }
247         }
248         else {
249             newfqname = dtype.getFullyQualifiedName() + '$' + newname;
250         }
251         return createChangesForTypeChange(type, newfqname, null);
252     }
253     
254     /**
255      * Returns a listing of configurations that have a specific project name attribute in them
256      * @param pname the project attribute to compare against
257      * @return the list of java type launch configurations that have the specified project attribute
258      * @since 3.2
259      */

260     protected static ILaunchConfiguration[] getJavaTypeLaunchConfigurations(String JavaDoc pname) {
261         try {
262             ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
263             ArrayList JavaDoc list = new ArrayList JavaDoc();
264             String JavaDoc attrib;
265             for(int i = 0; i < configs.length; i++) {
266                 attrib = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc)null);
267                 if(attrib != null) {
268                     if(attrib.equals(pname)) {
269                         list.add(configs[i]);
270                     }
271                 }
272             }
273             return (ILaunchConfiguration[])list.toArray(new ILaunchConfiguration[list.size()]);
274         }
275         catch(CoreException e) {JDIDebugPlugin.log(e);}
276         return new ILaunchConfiguration[0];
277     }
278
279 }
280
Popular Tags