1 11 12 package org.eclipse.jdt.internal.debug.ui.launcher; 13 14 import org.eclipse.core.expressions.PropertyTester; 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.core.runtime.NullProgressMonitor; 17 import org.eclipse.jdt.core.IClassFile; 18 import org.eclipse.jdt.core.ICompilationUnit; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IJavaProject; 21 import org.eclipse.jdt.core.IMember; 22 import org.eclipse.jdt.core.IMethod; 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.core.JavaModelException; 27 import org.eclipse.jdt.core.Signature; 28 29 33 public class JavaElementPropertyTester extends PropertyTester { 34 35 private static final String PROPERTY_IS_APPLET= "isApplet"; private static final String PROPERTY_HAS_MAIN_TYPE= "hasMainType"; private final String APPLET_TYPE = "java.applet.Applet"; 39 42 public boolean test(Object receiver, String method, Object [] args, Object expectedValue) { 43 IJavaElement javaElement = null; 44 if (receiver instanceof IAdaptable) { 45 javaElement = (IJavaElement) ((IAdaptable)receiver).getAdapter(IJavaElement.class); 46 } 47 if (javaElement != null) { 48 if (!javaElement.exists()) { 49 return false; 50 } 51 } 52 if (javaElement instanceof IJavaProject || 53 javaElement instanceof IPackageFragmentRoot || 54 javaElement instanceof IPackageFragment) { 55 return true; 57 } 58 if (javaElement != null) { 59 if (PROPERTY_IS_APPLET.equals(method)) { 60 return isApplet(javaElement); 61 } else if (PROPERTY_HAS_MAIN_TYPE.equals(method)) { 62 return hasMain(javaElement); 63 } 64 } 65 return false; 66 } 67 68 73 private boolean isApplet(IJavaElement element) { 74 try { 75 IType type = getType(element); 76 if(type != null) { 77 IType[] stypes = type.newSupertypeHierarchy(new NullProgressMonitor()).getAllSuperclasses(type); 79 for (int i = 0; i < stypes.length; i++) { 80 if (stypes[i].getFullyQualifiedName().equals(APPLET_TYPE)) { 81 return true; 82 } } } } catch (JavaModelException e) {} 87 return false; 88 } 89 90 95 private boolean hasMain(IJavaElement element) { 96 try { 97 IType mainType = getType(element); 98 if (mainType != null && mainType.exists()) { 99 IMethod[] methods = mainType.getMethods(); 100 for (int i= 0; i < methods.length; i++) { 101 if (methods[i].isMainMethod()) { 102 return true; 103 } } } } catch (JavaModelException e) {} 108 return false; 109 } 110 111 private IType getType(IJavaElement element) throws JavaModelException { 112 IType type = null; 113 if (element instanceof ICompilationUnit) { 114 ICompilationUnit cu = (ICompilationUnit) element; 115 type= cu.getType(Signature.getQualifier(cu.getElementName())); 116 } else if (element instanceof IClassFile) { 117 type = ((IClassFile)element).getType(); 118 } else if (element instanceof IType) { 119 type = (IType) element; 120 } else if (element instanceof IMember) { 121 type = ((IMember)element).getDeclaringType(); 122 } 123 return type; 124 } 125 } 126 | Popular Tags |