KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > JavaElementPropertyTester


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
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 /**
30  * ResourceExtender provides propertyTester(s) for IResource types
31  * for use in XML Expression Language syntax.
32  */

33 public class JavaElementPropertyTester extends PropertyTester {
34
35     private static final String JavaDoc PROPERTY_IS_APPLET= "isApplet"; //$NON-NLS-1$
36
private static final String JavaDoc PROPERTY_HAS_MAIN_TYPE= "hasMainType"; //$NON-NLS-1$
37
private final String JavaDoc APPLET_TYPE = "java.applet.Applet"; //$NON-NLS-1$
38

39     /* (non-Javadoc)
40      * @see org.eclipse.jdt.internal.corext.refactoring.participants.properties.IPropertyEvaluator#test(java.lang.Object, java.lang.String, java.lang.String)
41      */

42     public boolean test(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args, Object JavaDoc 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                 // optomistic
56
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     /**
69      * Check if the specified resource is an Applet.
70      * @return <code>true</code> if the target resource is an Applet,
71      * <code>false</code> otherwise.
72      */

73     private boolean isApplet(IJavaElement element) {
74         try {
75             IType type = getType(element);
76             if(type != null) {
77                 //bug fix: 112455
78
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                     }//end if
83
}//end for
84
}//end if
85
}//end try
86
catch (JavaModelException e) {}
87         return false;
88     }
89     
90     /**
91      * Look for a Java main() method in the specified resource.
92      * @return true if the target resource has a <code>main</code> method,
93      * <code>false</code> otherwise.
94      */

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                     }//end if
104
}//end for
105
}//end if
106
}//end try
107
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