Home » Language IDEs » Java Development Tools (JDT) » JDT Debug Tools Help
JDT Debug Tools Help [message #894873] |
Tue, 10 July 2012 20:56 |
Matt R Messages: 6 Registered: July 2012 |
Junior Member |
|
|
Hi,
I'm new to eclipse plugin development/debugger development, and I'm currently working on a project with this scenario:
What I'm trying to do is I have this framework/API built on Java, and I would like to write an eclipse plugin debugger that is customized to my framework. Here is a simple example:
I have two classes, one called scope and one called variable. The scope holds a map of variables. The code is all in java, but I'm using this scope-variable relationship almost like a new language, and would like a variable debug tab that gives me a list of currently active scopes with the variables that are currently stored inside. Here is some code:
import java.util.Hashtable;
public class Scope {
private Hashtable<String, Variable> variableList = new Hashtable<String, Variable>();
// constructor
public Scope(){
}
public void put(String key, Variable v){
variableList.put(key, v);
}
public Variable get(String key){
return variableList.get(key);
}
}
public class Variable {
private String value;
private String name;
public Variable(String aName, String aValue){
name = aName;
value = aValue;
}
public String getValue(){
return value;
}
public String getName(){
return name;
}
public void setValue(String aValue){
value = aValue;
}
}
This example is simplified; the actual scope-variable relation I am working with is more complex, such that you have a scope which is a container of something, which is a container of something...some container holds variables; basically the hierarchy is complicated and is not easily navigable with the variables view.
Goal: I want to be able to create a view that is similar to a variables view, but only shows scope-variable relationships ignoring all the stuff in between that would make up the complex java hierarchy behind it.
Current Solution: In order to perform what I am currently doing, I'm using the static methods of the org.eclipse.debug.ui.DebugUITools; class: the getDebugContext(). Basically this listens to debug context changes and gives me IStackFrame/IVariable/IValue interfaces that give me enough information to build the complicated hierarchy, which I later parse into the simplified view of scope-variable. Here is some code:
public class DebugContextListener implements IDebugContextListener
{
IStackFrameConsumer consumer;
/**
* Creates a new DebugContextListener.
*
* @param consumer
* an IStackFrameConsumer to which the extracted stack frame
* objects will be given.
*/
public DebugContextListener(IStackFrameConsumer consumer)
{
this.consumer = consumer;
}
/**
* Notification that the debug context has changed (a new debug event has happened)
*/
public void debugContextChanged(DebugContextEvent event)
{
if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0)
{
contextActivated(event.getContext());
}
}
// Grab the data if it is a new
private void contextActivated(ISelection context)
{
if (context instanceof StructuredSelection)
{
Object data = ((StructuredSelection) context).getFirstElement();
if (data instanceof IJavaStackFrame)
{
consumer.setStackFrame((IStackFrame) data);
} else
{
consumer.setStackFrame(null);
}
}
}
}
public void createPartControl(Composite parent)
{
// Create a new debug listener object to detect
// debug actions
debugListener = new DebugContextListener(this);
DebugUITools.getDebugContextManager().addDebugContextListener(debugListener);
// Check if there is an already started debug context
IAdaptable dc = DebugUITools.getDebugContext();
if (dc != null)
{
Object o = dc.getAdapter(IStackFrame.class);
if (o instanceof IStackFrame)
setStackFrame((IStackFrame) o);
}
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
...//more view building stuff
Ok so there are a few more classes, but this is the basic gist and most of the important code, and it works as it is intended.
Problem: The issue with this method is the only information I get from the listener are these IVariable/IValue objects, merely language independent representations of data on the stackframes, meaning my plugin cannot actually get the java objects. This means I have to write a billion loops, looping through my giant hierarchy to grab the variable that is deep within a scope objects. If I had the real java object, i could just call a get method that grabs me the variables.
Question: So as I understand it, the JDT provides another layer of java language specific tools that could possibly help me do what I want (that is, grab real java objects from the stackframe and be able to evaluate them, similar to the expressions view). This is what I am referring to: http://git.eclipse.org/c/jdt/eclipse.jdt.debug.git/tree/org.eclipse.jdt.debug/model/org/eclipse/jdt/debug/core
I am really eager for some advice on whether I am on the right track or not, and perhaps what I should be specifically looking into, as I am not very experienced with the JDT, and I can't find any good examples of anyone attempting anything similar to my project.
Thank you for reading all this, and thanks for any help.
|
|
| | | | | | | | | |
Re: JDT Debug Tools Help [message #1368846 is a reply to message #1367002] |
Tue, 20 May 2014 09:51 |
diego loro Messages: 2 Registered: May 2014 |
Junior Member |
|
|
Hi Matt,
we solved the problem too.
For next programmers that will have the same problem here is our solution:
IValue value = javaVariable.getValue();
if(value instanceof IJavaObject) {
IJavaObject object = (IJavaObject) value;
IJavaThread thread = getThread();
if(thread == null)
throw new DebugException(null);
// STRING VALUE
IJavaValue result = object.sendMessage("toString", "()Ljava/lang/String;", new IJavaValue[] { }, thread, false);
// BYTE VALUE
// IJavaValue resultBytes = object.sendMessage("getBytes", "()[B", new IJavaValue[] { }, thread, false);
variableValueString = formatString(result.toString());
}
private static IJavaThread getThread() {
IJavaStackFrame stackFrame = getStackFrame();
return (IJavaThread) stackFrame.getThread();
}
private static IJavaStackFrame getStackFrame() {
IAdaptable adaptable = DebugUITools.getDebugContext();
if(adaptable != null)
return (IJavaStackFrame) adaptable.getAdapter(IJavaStackFrame.class);
else
return null;
}
Thanks all,
best regards
|
|
|
Goto Forum:
Current Time: Thu Dec 26 23:30:32 GMT 2024
Powered by FUDForum. Page generated in 0.04567 seconds
|