1 11 package org.eclipse.jdt.internal.debug.ui.launcher; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IAdaptable; 21 import org.eclipse.debug.core.ILaunchConfiguration; 22 import org.eclipse.debug.core.ILaunchConfigurationType; 23 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 24 import org.eclipse.jdt.core.IJavaElement; 25 import org.eclipse.jdt.core.IMember; 26 import org.eclipse.jdt.core.IMethod; 27 import org.eclipse.jdt.core.IType; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.core.search.IJavaSearchScope; 30 import org.eclipse.jdt.core.search.SearchEngine; 31 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 32 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 33 import org.eclipse.jface.operation.IRunnableContext; 34 35 38 public class JavaApplicationLaunchShortcut extends JavaLaunchShortcut { 39 40 46 private IJavaElement[] getJavaElements(Object [] objects) { 47 List list= new ArrayList (objects.length); 48 for (int i = 0; i < objects.length; i++) { 49 Object object = objects[i]; 50 if (object instanceof IAdaptable) { 51 IJavaElement element = (IJavaElement) ((IAdaptable)object).getAdapter(IJavaElement.class); 52 if (element != null) { 53 if (element instanceof IMember) { 54 IJavaElement type= ((IMember)element).getDeclaringType(); 56 if (type != null) { 57 element= type; 58 } 59 } 60 list.add(element); 61 } 62 } 63 } 64 return (IJavaElement[]) list.toArray(new IJavaElement[list.size()]); 65 } 66 67 70 protected ILaunchConfiguration createConfiguration(IType type) { 71 ILaunchConfiguration config = null; 72 ILaunchConfigurationWorkingCopy wc = null; 73 try { 74 ILaunchConfigurationType configType = getConfigurationType(); 75 wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(type.getElementName())); 76 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, type.getFullyQualifiedName()); 77 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, type.getJavaProject().getElementName()); 78 wc.setMappedResources(new IResource[] {type.getUnderlyingResource()}); 80 config = wc.doSave(); 81 } catch (CoreException exception) { 82 reportErorr(exception); 83 } 84 return config; 85 } 86 87 90 protected ILaunchConfigurationType getConfigurationType() { 91 return getLaunchManager().getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION); 92 } 93 94 97 protected IType[] findTypes(Object [] elements, IRunnableContext context) throws InterruptedException , CoreException { 98 try { 99 if(elements.length == 1) { 100 IType type = isMainMethod(elements[0]); 101 if(type != null) { 102 return new IType[] {type}; 103 } 104 } 105 IJavaElement[] javaElements = getJavaElements(elements); 106 MainMethodSearchEngine engine = new MainMethodSearchEngine(); 107 int constraints = IJavaSearchScope.SOURCES; 108 constraints |= IJavaSearchScope.APPLICATION_LIBRARIES; 109 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(javaElements, constraints); 110 return engine.searchMainMethods(context, scope, true); 111 } catch (InvocationTargetException e) { 112 throw (CoreException)e.getTargetException(); 113 } 114 } 115 116 122 protected IType isMainMethod(Object o) { 123 if(o instanceof IAdaptable) { 124 IAdaptable adapt = (IAdaptable) o; 125 IJavaElement element = (IJavaElement) adapt.getAdapter(IJavaElement.class); 126 if(element != null && element.getElementType() == IJavaElement.METHOD) { 127 try { 128 IMethod method = (IMethod) element; 129 if(method.isMainMethod()) { 130 return method.getDeclaringType(); 131 } 132 } 133 catch (JavaModelException jme) {JDIDebugUIPlugin.log(jme);} 134 } 135 } 136 return null; 137 } 138 139 142 protected String getTypeSelectionTitle() { 143 return LauncherMessages.JavaApplicationLaunchShortcut_0; 144 } 145 146 149 protected String getEditorEmptyMessage() { 150 return LauncherMessages.JavaApplicationLaunchShortcut_1; 151 } 152 153 156 protected String getSelectionEmptyMessage() { 157 return LauncherMessages.JavaApplicationLaunchShortcut_2; 158 } 159 160 } 161 | Popular Tags |