KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > SerialVersionComputationHelper


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.text.correction;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21
22 import org.eclipse.debug.core.DebugPlugin;
23 import org.eclipse.debug.core.ILaunchConfiguration;
24 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
25 import org.eclipse.debug.core.ILaunchManager;
26 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
27
28 import org.eclipse.debug.ui.IDebugUIConstants;
29
30 import org.eclipse.jdt.core.IJavaProject;
31
32 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
33 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
34
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
36
37 /**
38  * Helper class to compute a batch of serial version IDs.
39  *
40  * @since 3.2
41  */

42 public final class SerialVersionComputationHelper {
43
44     /** The launch configuration type */
45     private static final String JavaDoc LAUNCH_CONFIG_TYPE= "org.eclipse.jdt.ui.serial.support"; //$NON-NLS-1$
46

47     /** The serial support class */
48     private static final String JavaDoc SERIAL_SUPPORT_CLASS= "org.eclipse.jdt.internal.ui.text.correction.SerialVersionComputer"; //$NON-NLS-1$
49

50     /**
51      * Computes the serial IDs for the specified fully qualified class names.
52      *
53      * @param classPath
54      * the class path to use
55      * @param project
56      * the project used to associated the private launch
57      * configuration
58      * @param classNames
59      * the fully qualified class names
60      * @param monitor
61      * the progress monitor to use
62      * @return the array of serial version ids corresponding
63      * @throws CoreException
64      * if any error occurs during serial version ID computation
65      */

66     public static long[] computeSerialIDs(final IRuntimeClasspathEntry[] classPath, final IJavaProject project, final String JavaDoc[] classNames, final IProgressMonitor monitor) throws CoreException {
67         Assert.isNotNull(classPath);
68         Assert.isNotNull(project);
69         Assert.isNotNull(classNames);
70         Assert.isNotNull(monitor);
71         long[] result= {};
72         final ILaunchConfigurationWorkingCopy copy= DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(LAUNCH_CONFIG_TYPE).newInstance(null, LAUNCH_CONFIG_TYPE + System.currentTimeMillis());
73         copy.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true);
74         copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);
75         copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, SERIAL_SUPPORT_CLASS);
76         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
77         for (int index= 0; index < classNames.length; index++) {
78             buffer.append(classNames[index]);
79             buffer.append(' ');
80         }
81         copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, buffer.toString());
82         copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, project.getElementName());
83         final List JavaDoc mementos= new ArrayList JavaDoc(classPath.length);
84         IRuntimeClasspathEntry entry= null;
85         for (int index= 0; index < classPath.length; index++) {
86             entry= classPath[index];
87             mementos.add(entry.getMemento());
88         }
89         copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, mementos);
90         final ILaunchConfiguration configuration= copy.doSave();
91         try {
92             final ILaunchConfigurationDelegate delegate= configuration.getType().getDelegate(ILaunchManager.RUN_MODE);
93             if (delegate instanceof SerialVersionLaunchConfigurationDelegate) {
94                 final SerialVersionLaunchConfigurationDelegate extension= (SerialVersionLaunchConfigurationDelegate) delegate;
95                 configuration.launch(ILaunchManager.RUN_MODE, monitor, true, false);
96                 result= extension.getSerialVersionIDs();
97                 final String JavaDoc message= extension.getErrorMessage();
98                 if (message != null)
99                     throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, message, null));
100             } else
101                 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, CorrectionMessages.SerialVersionHashProposal_wrong_launch_delegate, null));
102         } finally {
103             configuration.delete();
104         }
105         return result;
106     }
107
108     /**
109      * Creates a new serial version computation helper.
110      */

111     private SerialVersionComputationHelper() {
112         // Not for instantiation
113
}
114 }
115
Popular Tags