1 11 package org.eclipse.jdt.internal.debug.ui.launcher; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import com.ibm.icu.text.MessageFormat; 16 import java.util.ArrayList ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IAdaptable; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 import org.eclipse.jdt.core.IClassFile; 30 import org.eclipse.jdt.core.ICompilationUnit; 31 import org.eclipse.jdt.core.IJavaElement; 32 import org.eclipse.jdt.core.IJavaProject; 33 import org.eclipse.jdt.core.IMember; 34 import org.eclipse.jdt.core.IType; 35 import org.eclipse.jdt.core.ITypeHierarchy; 36 import org.eclipse.jdt.core.JavaCore; 37 import org.eclipse.jdt.core.JavaModelException; 38 import org.eclipse.jdt.core.Signature; 39 import org.eclipse.jdt.internal.debug.core.JavaDebugUtils; 40 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 41 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 42 import org.eclipse.jface.operation.IRunnableContext; 43 import org.eclipse.jface.operation.IRunnableWithProgress; 44 45 public class AppletLaunchConfigurationUtils { 46 47 56 public static void abort(String message, Throwable exception, int code) 57 throws CoreException { 58 throw new CoreException( 59 new Status( 60 IStatus.ERROR, 61 JDIDebugUIPlugin.getUniqueIdentifier(), 62 code, 63 message, 64 exception)); 65 } 66 67 72 public static IType getMainType(String mainTypeName, IJavaProject javaProject) throws CoreException { 73 if ((mainTypeName == null) || (mainTypeName.trim().length() < 1)) { 74 abort(LauncherMessages.appletlauncher_utils_error_main_type_not_specified, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); 75 } 76 IType mainType = null; 77 try { 78 mainType = findType(javaProject, mainTypeName); 79 } catch (JavaModelException jme) { 80 } 81 if (mainType == null) { 82 abort(MessageFormat.format(LauncherMessages.appletlauncher_utils_error_main_type_does_not_exist, new String [] {mainTypeName, javaProject.getElementName()}), null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); 83 } 84 return mainType; 85 } 86 87 88 91 public static IType findType(IJavaProject javaProject, String mainTypeName) throws CoreException { 92 IJavaElement javaElement= JavaDebugUtils.findElement(mainTypeName, javaProject); 93 if (javaElement == null) { 94 return null; 95 } else if (javaElement instanceof IType) { 96 return (IType)javaElement; 97 } else if (javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) { 98 String simpleName= Signature.getSimpleName(mainTypeName); 99 return ((ICompilationUnit) javaElement).getType(simpleName); 100 } else if (javaElement.getElementType() == IJavaElement.CLASS_FILE) { 101 return ((IClassFile) javaElement).getType(); 102 } 103 return null; 104 } 105 106 109 public static Set collectAppletTypesInProject(IProgressMonitor monitor, IJavaProject project) { 110 IType[] types; 111 HashSet result = new HashSet (5); 112 try { 113 IType javaLangApplet = AppletLaunchConfigurationUtils.getMainType("java.applet.Applet", project); ITypeHierarchy hierarchy = javaLangApplet.newTypeHierarchy(project, new SubProgressMonitor(monitor, 1)); 115 types = hierarchy.getAllSubtypes(javaLangApplet); 116 int length = types.length; 117 if (length != 0) { 118 for (int i = 0; i < length; i++) { 119 if (!types[i].isBinary()) { 120 result.add(types[i]); 121 } 122 } 123 } 124 } catch(JavaModelException jme) { 125 } catch(CoreException e) { 126 } 127 monitor.done(); 128 return result; 129 } 130 131 public static void collectTypes(Object element, IProgressMonitor monitor, Set result) throws JavaModelException { 132 element= computeScope(element); 133 while(element instanceof IMember) { 134 if(element instanceof IType) { 135 if (isSubclassOfApplet(monitor, (IType)element)) { 136 result.add(element); 137 monitor.done(); 138 return; 139 } 140 } 141 element= ((IJavaElement)element).getParent(); 142 } 143 if (element instanceof ICompilationUnit) { 144 ICompilationUnit cu= (ICompilationUnit)element; 145 IType[] types= cu.getAllTypes(); 146 for (int i= 0; i < types.length; i++) { 147 if (isSubclassOfApplet(monitor, types[i])) { 148 result.add(types[i]); 149 } 150 } 151 } else if (element instanceof IClassFile) { 152 IType type = ((IClassFile)element).getType(); 153 if (isSubclassOfApplet(monitor, type)) { 154 result.add(type); 155 } 156 } else if (element instanceof IJavaElement) { 157 IJavaElement parent = (IJavaElement) element; 158 List found= searchSubclassesOfApplet(monitor, (IJavaElement)element); 159 Iterator iterator = found.iterator(); 161 while (iterator.hasNext()) { 162 IJavaElement target = (IJavaElement) iterator.next(); 163 IJavaElement child = target; 164 while (child != null) { 165 if (child.equals(parent)) { 166 result.add(target); 167 break; 168 } 169 child = child.getParent(); 170 } 171 } 172 } 173 monitor.done(); 174 } 175 176 private static List searchSubclassesOfApplet(IProgressMonitor pm, IJavaElement javaElement) { 177 return new ArrayList (collectAppletTypesInProject(pm, javaElement.getJavaProject())); 178 } 179 180 private static boolean isSubclassOfApplet(IProgressMonitor pm, IType type) { 181 return collectAppletTypesInProject(pm, type.getJavaProject()).contains(type); 182 } 183 184 private static Object computeScope(Object element) { 185 if (element instanceof IJavaElement) { 186 return element; 187 } 188 if (element instanceof IAdaptable) { 189 element = ((IAdaptable)element).getAdapter(IResource.class); 190 } 191 if (element instanceof IResource) { 192 IJavaElement javaElement = JavaCore.create((IResource)element); 193 if (javaElement != null && !javaElement.exists()) { 194 element = null; 196 } else { 197 element= javaElement; 198 } 199 200 } 201 return element; 202 } 203 204 212 public static IType[] findApplets(IRunnableContext context, final Object [] elements) throws InvocationTargetException , InterruptedException { 213 final Set result= new HashSet (); 214 215 if (elements.length > 0) { 216 IRunnableWithProgress runnable= new IRunnableWithProgress() { 217 public void run(IProgressMonitor pm) throws InterruptedException { 218 int nElements= elements.length; 219 pm.beginTask(LauncherMessages.appletlauncher_search_task_inprogress, nElements); 220 try { 221 for (int i= 0; i < nElements; i++) { 222 try { 223 collectTypes(elements[i], new SubProgressMonitor(pm, 1), result); 224 } catch (JavaModelException jme) { 225 JDIDebugUIPlugin.log(jme.getStatus()); 226 } 227 if (pm.isCanceled()) { 228 throw new InterruptedException (); 229 } 230 } 231 } finally { 232 pm.done(); 233 } 234 } 235 }; 236 context.run(true, true, runnable); 237 } 238 return (IType[]) result.toArray(new IType[result.size()]) ; 239 } 240 } 241 242 | Popular Tags |