KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > eval > ast > engine > AbstractRuntimeContext


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.eval.ast.engine;
12
13 import com.ibm.icu.text.MessageFormat;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jdt.core.IJavaProject;
19 import org.eclipse.jdt.debug.core.IJavaClassObject;
20 import org.eclipse.jdt.debug.core.IJavaClassType;
21 import org.eclipse.jdt.debug.core.IJavaObject;
22 import org.eclipse.jdt.debug.core.IJavaType;
23 import org.eclipse.jdt.debug.core.IJavaValue;
24 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
25 import org.eclipse.jdt.internal.debug.eval.ast.instructions.InstructionsEvaluationMessages;
26
27 import com.sun.jdi.InvocationException;
28
29 /**
30  * Common runtime context code for class loading and cache of
31  * class loader/java.lang.Class.
32  *
33  * @since 3.2
34  */

35
36 public abstract class AbstractRuntimeContext implements IRuntimeContext {
37     
38     /**
39      * Cache of class loader for this runtime context
40      */

41     private IJavaObject fClassLoader;
42     
43     /**
44      * Cache of java.lang.Class type
45      */

46     private IJavaClassType fJavaLangClass;
47
48     /**
49      * Java project context
50      */

51     protected IJavaProject fProject;
52     
53     public static final String JavaDoc CLASS= "java.lang.Class"; //$NON-NLS-1$
54
public static final String JavaDoc FOR_NAME= "forName"; //$NON-NLS-1$
55
public static final String JavaDoc FOR_NAME_SIGNATURE= "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"; //$NON-NLS-1$
56

57
58     public AbstractRuntimeContext(IJavaProject project) {
59         fProject = project;
60     }
61     
62     /**
63      * Returns the class loader used to load classes for this runtime context
64      * or <code>null</code> when loaded by the bootstrap loader
65      *
66      * @return the class loader used to load classes for this runtime context or
67      * <code>null</code> when loaded by the bootstrap loader
68      * @throws CoreException if unable to resolve a class loader
69      */

70     protected IJavaObject getClassLoaderObject() throws CoreException {
71         if (fClassLoader == null) {
72             fClassLoader = getReceivingType().getClassLoaderObject();
73         }
74         return fClassLoader;
75     }
76     
77     /**
78      * Return the java.lang.Class type.
79      *
80      * @return the java.lang.Class type
81      * @throws CoreException if unable to retrive the type
82      */

83     protected IJavaClassType getJavaLangClass() throws CoreException {
84         if (fJavaLangClass == null) {
85             IJavaType[] types= getVM().getJavaTypes(CLASS);
86             if (types == null || types.length != 1) {
87                 throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, MessageFormat.format(InstructionsEvaluationMessages.Instruction_No_type, new String JavaDoc[]{CLASS}), null));
88             }
89             fJavaLangClass = (IJavaClassType) types[0];
90         }
91         return fJavaLangClass;
92     }
93     
94     /**
95      * Invokes Class.classForName(String, boolean, ClassLoader) on the target
96      * to force load the specified class.
97      *
98      * @param qualifiedName name of class to load
99      * @param loader the class loader to use or <code>null</code> if the bootstrap loader
100      * @return the loaded class
101      * @throws CoreException if loading fails
102      */

103     protected IJavaClassObject classForName(String JavaDoc qualifiedName, IJavaObject loader) throws CoreException {
104         IJavaValue loaderArg = loader;
105         if (loader == null) {
106             loaderArg = getVM().nullValue();
107         }
108         IJavaValue[] args = new IJavaValue[] {getVM().newValue(qualifiedName), getVM().newValue(true), loaderArg};
109         try {
110             return (IJavaClassObject) getJavaLangClass().sendMessage(FOR_NAME, FOR_NAME_SIGNATURE, args, getThread());
111         } catch (CoreException e) {
112             if (e.getStatus().getException() instanceof InvocationException) {
113                 // Don't throw ClassNotFoundException
114
if (((InvocationException)e.getStatus().getException()).exception().referenceType().name().equals("java.lang.ClassNotFoundException")) { //$NON-NLS-1$
115
return null;
116                 }
117             }
118             throw e;
119         }
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.jdt.internal.debug.eval.ast.engine.IRuntimeContext#classForName(java.lang.String)
124      */

125     public IJavaClassObject classForName(String JavaDoc name) throws CoreException {
126         return classForName(name, getClassLoaderObject());
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.jdt.internal.debug.eval.ast.engine.IRuntimeContext#getProject()
131      */

132     public IJavaProject getProject() {
133         return fProject;
134     }
135 }
136
Popular Tags