KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > logicalstructures > LogicalObjectStructureInterfaceType


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 package org.eclipse.jdt.internal.debug.core.logicalstructures;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.debug.core.DebugEvent;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.IStatusHandler;
20 import org.eclipse.debug.core.model.IDebugTarget;
21 import org.eclipse.debug.core.model.ILogicalStructureTypeDelegate;
22 import org.eclipse.debug.core.model.IThread;
23 import org.eclipse.debug.core.model.IValue;
24 import org.eclipse.jdt.debug.core.IEvaluationRunnable;
25 import org.eclipse.jdt.debug.core.IJavaClassType;
26 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
27 import org.eclipse.jdt.debug.core.IJavaInterfaceType;
28 import org.eclipse.jdt.debug.core.IJavaObject;
29 import org.eclipse.jdt.debug.core.IJavaThread;
30 import org.eclipse.jdt.debug.core.IJavaType;
31 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
32
33 /**
34  * Common facilities for logical structure types for instances of an interface
35  */

36 public abstract class LogicalObjectStructureInterfaceType implements ILogicalStructureTypeDelegate {
37     
38     private IJavaObject fObject; // the map to provide structure for
39

40     private IValue fResult; // the resulting structure
41

42     private boolean fDone = false; // done the evaluation
43

44     private static IStatus fgNeedThread = new Status(IStatus.INFO, JDIDebugPlugin.getUniqueIdentifier(), LogicalObjectStructureInterfaceType.INFO_EVALUATION_THREAD, "Provides thread context for an evaluation", null); //$NON-NLS-1$
45
private static IStatusHandler fgThreadProvider;
46
47     /**
48      * Status code used by the debug model to retrieve a thread to use
49      * for evalutaions, via a status handler. A status handler is contributed by
50      * the Java debug UI. When not present, the debug model uses any suspended thread.
51      *
52      * @since 3.0
53      */

54     public static final int INFO_EVALUATION_THREAD = 110;
55
56     /* (non-Javadoc)
57      * @see org.eclipse.debug.core.model.ILogicalStructureType#providesLogicalStructure(org.eclipse.debug.core.model.IValue)
58      */

59     public boolean providesLogicalStructure(IValue value) {
60         if (value instanceof IJavaObject) {
61             IJavaObject object = (IJavaObject) value;
62             try {
63                 IJavaType type = object.getJavaType();
64                 if (type instanceof IJavaClassType) {
65                     IJavaClassType classType = (IJavaClassType) type;
66                     IJavaInterfaceType[] interfaceTypes = classType.getAllInterfaces();
67                     String JavaDoc targetInterface = getTargetInterfaceName();
68                     for (int i = 0; i < interfaceTypes.length; i++) {
69                         IJavaInterfaceType inter = interfaceTypes[i];
70                         if (inter.getName().equals(targetInterface)) {
71                             return true;
72                         }
73                     }
74                 }
75             } catch (DebugException e) {
76             }
77         }
78         return false;
79     }
80     
81     /**
82      * Returns the name of an interface that an object must implement for this
83      * structure type to be appropriate.
84      *
85      * @return the name of an interface that an object must implement for this
86      * structure type to be appropriate
87      */

88     protected abstract String JavaDoc getTargetInterfaceName();
89     
90     /**
91      * Returns the evaluation that computes the logical object structure for this
92      * strucutre type.
93      *
94      * @return the evaluation that computes the logical object structure for this
95      * strucutre type
96      */

97     protected abstract IEvaluationRunnable getEvaluation();
98
99     /* (non-Javadoc)
100      * @see org.eclipse.debug.core.model.ILogicalStructureType#getLogicalStructure(org.eclipse.debug.core.model.IValue)
101      */

102     public synchronized IValue getLogicalStructure(IValue value) throws CoreException {
103         final IJavaThread thread = getThread(value);
104         if (thread == null) {
105             // can't do it
106
throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.INTERNAL_ERROR, LogicalStructuresMessages.LogicalObjectStructureType_1, null));
107         }
108         setObject((IJavaObject)value);
109         final IEvaluationRunnable evaluation = getEvaluation();
110         final CoreException[] ex = new CoreException[1];
111         final Object JavaDoc lock = this;
112         fDone = false;
113         if (thread.isPerformingEvaluation() && thread.isSuspended()) {
114             return value;
115         }
116         thread.queueRunnable(new Runnable JavaDoc() {
117             public void run() {
118                 try {
119                     thread.runEvaluation(evaluation, null, DebugEvent.EVALUATION_IMPLICIT, false);
120                 } catch (DebugException e) {
121                     ex[0] = e;
122                 }
123                 synchronized (lock) {
124                     fDone = true;
125                     lock.notifyAll();
126                 }
127             }
128         });
129         try {
130             synchronized (lock) {
131                 if (!fDone) {
132                     lock.wait();
133                 }
134             }
135         } catch (InterruptedException JavaDoc e) {
136         }
137         if (ex[0] != null) {
138             throw ex[0];
139         }
140         return fResult;
141     }
142
143     private IJavaThread getThread(IValue value) throws CoreException {
144         IStatusHandler handler = getThreadProvider();
145         if (handler != null) {
146             IJavaThread thread = (IJavaThread)handler.handleStatus(fgNeedThread, value);
147             if (thread != null) {
148                 return thread;
149             }
150         }
151         IDebugTarget target = value.getDebugTarget();
152         IJavaDebugTarget javaTarget = (IJavaDebugTarget) target.getAdapter(IJavaDebugTarget.class);
153         if (javaTarget != null) {
154             IThread[] threads = javaTarget.getThreads();
155             for (int i = 0; i < threads.length; i++) {
156                 IThread thread = threads[i];
157                 if (thread.isSuspended()) {
158                     return (IJavaThread)thread;
159                 }
160             }
161         }
162         return null;
163     }
164     
165     private static IStatusHandler getThreadProvider() {
166         if (fgThreadProvider == null) {
167             fgThreadProvider = DebugPlugin.getDefault().getStatusHandler(fgNeedThread);
168         }
169         return fgThreadProvider;
170     }
171
172     /**
173      * Sets the object for which a logical structure is to be provided.
174      *
175      * @param object the object for which a logical structure is to be provided
176      */

177     private void setObject(IJavaObject object) {
178         fObject = object;
179     }
180     
181     /**
182      * Returns the object for which a logical structure is to be provided
183      *
184      * @return the object for which a logical structure is to be provided
185      */

186     protected IJavaObject getObject() {
187         return fObject;
188     }
189     
190     /**
191      * Sets the object representing the logical structure.
192      *
193      * @param result the object representing the logical structure
194      */

195     protected void setLogicalStructure(IValue result) {
196         fResult = result;
197     }
198     
199 }
200
Popular Tags