KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > TypeNameResolver


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.debug.ui;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.variables.IDynamicVariable;
16 import org.eclipse.debug.internal.ui.stringsubstitution.ResourceResolver;
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.IType;
21 import org.eclipse.jdt.core.JavaCore;
22
23 /**
24  * Variable resolver which returns the fully qualified name of the
25  * primary type in the selected resource.
26  */

27 public class TypeNameResolver extends ResourceResolver {
28     /* (non-Javadoc)
29      * @see org.eclipse.core.variables.IDynamicVariableResolver#resolveValue(org.eclipse.core.variables.IDynamicVariable, java.lang.String)
30      */

31     public String JavaDoc resolveValue(IDynamicVariable variable, String JavaDoc argument) throws CoreException {
32         IResource resource = getSelectedResource(variable);
33         IJavaElement javaElement = JavaCore.create(resource);
34         if (javaElement != null) {
35             IType type= getType(javaElement);
36             if (type != null) {
37                 return type.getFullyQualifiedName();
38             }
39         }
40         abort(DebugUIMessages.TypeNameResolver_0, null);
41         return null;
42     }
43     
44     /**
45      * Returns the primary type in the given Java element
46      * or <code>null</code> if none.
47      *
48      * @param element the Java element
49      * @return the primary type in the given Java element
50      */

51     public static IType getType(IJavaElement element) {
52         IType type= null;
53         int elementType= element.getElementType();
54         switch (elementType) {
55             case IJavaElement.CLASS_FILE :
56                 type= ((IClassFile) element).getType();
57                 break;
58             case IJavaElement.COMPILATION_UNIT :
59                 type= ((ICompilationUnit) element).findPrimaryType();
60                 break;
61         }
62         return type;
63     }
64 }
65
Popular Tags