[jboss-svn-commits] JBL Code SVN: r6133 - in labs/jbossrules/trunk/drools-ide: . META-INF src/main/java/org/drools/ide src/main/java/org/drools/ide/debug/core src/main/java/org/drools/ide/launching

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Sep 10 19:45:05 EDT 2006


Author: KrisVerlaenen
Date: 2006-09-10 19:44:58 -0400 (Sun, 10 Sep 2006)
New Revision: 6133

Added:
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/RuleInfo.java
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsLocalVariable.java
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsStackFrame.java
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsThisVariable.java
Removed:
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsStackFrame.java
Modified:
   labs/jbossrules/trunk/drools-ide/META-INF/MANIFEST.MF
   labs/jbossrules/trunk/drools-ide/plugin.xml
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/DroolsIDEPlugin.java
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsLineBreakpoint.java
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsThread.java
   labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/launching/DroolsSourceLookupParticipant.java
Log:
JBRULES-342: interactive rule debugger
 - additional work to get debugger working

Modified: labs/jbossrules/trunk/drools-ide/META-INF/MANIFEST.MF
===================================================================
--- labs/jbossrules/trunk/drools-ide/META-INF/MANIFEST.MF	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/META-INF/MANIFEST.MF	2006-09-10 23:44:58 UTC (rev 6133)
@@ -30,7 +30,8 @@
  org.eclipse.ui.presentations.r21,
  org.eclipse.ui.views,
  org.eclipse.ui.workbench.texteditor,
- org.eclipse.pde.ui
+ org.eclipse.pde.ui,
+ com.ibm.icu
 Eclipse-LazyStart: true
 Bundle-ClassPath: ide.jar,
  lib/antlr-2.7.6.jar,

Modified: labs/jbossrules/trunk/drools-ide/plugin.xml
===================================================================
--- labs/jbossrules/trunk/drools-ide/plugin.xml	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/plugin.xml	2006-09-10 23:44:58 UTC (rev 6133)
@@ -179,7 +179,7 @@
       </perspective>
    </extension>
    
-   <!-- Extensions for the interactive debugger
+   <!-- Extensions for the interactive debugger -->
    
    <extension point = "org.eclipse.debug.core.launchConfigurationTypes">
       <launchConfigurationType
@@ -242,8 +242,6 @@
       </debugModelPresentation> 
    </extension>
    
-   -->
-   
    <extension point = "org.eclipse.ui.popupMenus">
      <objectContribution
         	id="org.drools.ide.action.ConvertToDroolsProject"

Modified: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/DroolsIDEPlugin.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/DroolsIDEPlugin.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/DroolsIDEPlugin.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -15,16 +15,30 @@
  * limitations under the License.
  */
 
+import java.io.Reader;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 
+import org.drools.compiler.DrlParser;
+import org.drools.compiler.PackageBuilder;
+import org.drools.compiler.PackageBuilderConfiguration;
+import org.drools.ide.builder.DroolsBuilder;
+import org.drools.ide.builder.Util;
+import org.drools.ide.editors.DSLAdapter;
 import org.drools.ide.preferences.IDroolsConstants;
+import org.drools.ide.util.ProjectClassLoader;
+import org.drools.lang.descr.PackageDescr;
+import org.drools.lang.descr.RuleDescr;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.debug.core.DebugException;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.ImageRegistry;
@@ -41,12 +55,17 @@
     
     public static final int INTERNAL_ERROR = 120;
     public static final String PLUGIN_ID = "org.drools.ide";
+    public static final String BUILD_RESULT_PACKAGE = "Package";
+    public static final String BUILD_RESULT_PACKAGE_DESCR = "PackageDescr";
     
 	//The shared instance.
 	private static DroolsIDEPlugin plugin;
 	//Resource bundle.
 	private ResourceBundle resourceBundle;
 	private Map colors = new HashMap();
+	private Map parsedRules = new HashMap();
+	private Map ruleInfoByRuleNameMap = new HashMap();
+	private Map ruleInfoByClassNameMap = new HashMap();
 	
 	/**
 	 * The constructor.
@@ -70,6 +89,7 @@
 		super.stop(context);
 		plugin = null;
 		resourceBundle = null;
+		parsedRules = null;
 		Iterator iterator = colors.values().iterator();
 		while (iterator.hasNext()) {
 			((Color) iterator.next()).dispose();
@@ -165,5 +185,81 @@
 		store.setDefault(IDroolsConstants.BUILD_ALL, false);
 		store.setDefault(IDroolsConstants.EDITOR_FOLDING, true);
 	}
+	
+	public PackageDescr parseResource(IResource resource, boolean compile) {
+		// TODO cache result and clear cache if necessary, taking properties into account
+		PackageDescr result = (PackageDescr) parsedRules.get(resource);
+		if (result != null) {
+			return result;
+		}
+		result = generateParsedResource(resource, compile);
+		if (result != null) {
+			parsedRules.put(resource, result);
+		}
+		return result;
+	}
+	
+	private PackageDescr generateParsedResource(IResource resource, boolean compile) {
+		if (resource instanceof IFile) {
+			IFile file = (IFile) resource;
+	        DrlParser parser = new DrlParser();
+	        try {
+	        	String content = new String(Util.getResourceContentsAsCharArray(file));
+	            Reader dslReader = DSLAdapter.getDSLContent(content, file);
+	            ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
+	            ClassLoader newLoader = DroolsBuilder.class.getClassLoader();
+	            PackageBuilderConfiguration builder_configuration = new PackageBuilderConfiguration();
+	            if (file.getProject().getNature("org.eclipse.jdt.core.javanature") != null) {
+	                IJavaProject project = JavaCore.create(file.getProject());
+	                newLoader = ProjectClassLoader.getProjectClassLoader(project);
+	                String level = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
+	            	builder_configuration.setJavaLanguageLevel(level);
+	            }
+	            try {
+	            	builder_configuration.setClassLoader(newLoader);
+	                Thread.currentThread().setContextClassLoader(newLoader);
+	                
+	                //First we parse the source
+	                PackageDescr packageDescr = null;
+                	if (dslReader != null) { 
+                		packageDescr = parser.parse(content, dslReader);
+                	} else {
+                		packageDescr = parser.parse(content);
+                	}
+                	if (compile && !parser.hasErrors()) {
+                        PackageBuilder builder = new PackageBuilder(builder_configuration);
+                        builder.addPackage(packageDescr);
+                        Iterator rules = packageDescr.getRules().iterator();
+                    	while (rules.hasNext()) {
+                    		RuleDescr descr = (RuleDescr) rules.next();
+                    		String className = packageDescr.getName() + "." + descr.getClassName();
+                    		RuleInfo ruleInfo = new RuleInfo(descr.getName(),
+                				packageDescr.getName(), resource.getName(),
+                				resource.getFullPath().toString(), className,
+                				descr.getConsequenceLine(),
+                				builder.getPackage().getPackageCompilationData()
+                					.getLineMappings(className).getOffset());
+                    		ruleInfoByRuleNameMap.put(packageDescr.getName() + "." + descr.getName(), ruleInfo);
+                    		ruleInfoByClassNameMap.put(className, ruleInfo);
+                    	}
+                    }
+                	return packageDescr;
+	            } finally {
+	                Thread.currentThread().setContextClassLoader(oldLoader);
+	            }
+	        } catch (Throwable t) {
+	        	log(t);
+	        }
+		}
+		return null;
+	}
+	
+	public RuleInfo getRuleInfoByClass(String ruleClassName) {
+		return (RuleInfo) ruleInfoByClassNameMap.get(ruleClassName);
+	}
 
+	public RuleInfo getRuleInfoByRule(String ruleName) {
+		return (RuleInfo) ruleInfoByClassNameMap.get(ruleName);
+	}
+
 }

Added: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/RuleInfo.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/RuleInfo.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/RuleInfo.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -0,0 +1,52 @@
+package org.drools.ide;
+
+public class RuleInfo {
+	
+	private String ruleName;
+	private String packageName;
+	private String drlName;
+	private String pathName;
+	private String className;
+	private int consequenceDrlLineNumber;
+	private int consequenceJavaLineNumber;
+	
+	public RuleInfo(String ruleName, String packageName, String drlName,
+					String pathName, String className,
+					int consequenceDrlLineNumber, int consequenceJavaLineNumber) {
+		this.ruleName = ruleName;
+		this.packageName = packageName;
+		this.drlName = drlName;
+		this.pathName = pathName;
+		this.className = className;
+		this.consequenceDrlLineNumber = consequenceDrlLineNumber;
+		this.consequenceJavaLineNumber = consequenceJavaLineNumber;
+	}
+
+	public String getClassName() {
+		return className;
+	}
+
+	public int getConsequenceDrlLineNumber() {
+		return consequenceDrlLineNumber;
+	}
+
+	public int getConsequenceJavaLineNumber() {
+		return consequenceJavaLineNumber;
+	}
+
+	public String getDrlName() {
+		return drlName;
+	}
+
+	public String getPackageName() {
+		return packageName;
+	}
+
+	public String getPathName() {
+		return pathName;
+	}
+
+	public String getRuleName() {
+		return ruleName;
+	}
+}

Modified: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsLineBreakpoint.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsLineBreakpoint.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsLineBreakpoint.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -1,10 +1,17 @@
 package org.drools.ide.debug.core;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
+import org.drools.ide.DroolsIDEPlugin;
+import org.drools.ide.RuleInfo;
+import org.drools.lang.descr.PackageDescr;
+import org.drools.lang.descr.RuleDescr;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.internal.debug.core.breakpoints.JavaLineBreakpoint;
 
 public class DroolsLineBreakpoint extends JavaLineBreakpoint {
@@ -47,13 +54,60 @@
 		return IDroolsDebugConstants.ID_DROOLS_DEBUG_MODEL;
 	}
 	
-	private static String getRuleClassName(IResource resource, int lineNumber) {
-		String ruleName = "Hello World";
-		return "Rule_" + ruleName.replaceAll("[^\\w$]", "_") + "_0";
+	private static String getRuleClassName(IResource resource, int lineNumber) throws CoreException {
+		try {
+			PackageDescr packageDescr = DroolsIDEPlugin.getDefault().parseResource(resource, true);
+			if (packageDescr != null) {
+				Iterator rules = packageDescr.getRules().iterator();
+				int ruleLine = -1;
+				RuleDescr resultRule = null;
+				while (rules.hasNext()) {
+					RuleDescr rule = (RuleDescr) rules.next();
+					if (rule.getConsequenceLine() > ruleLine && rule.getConsequenceLine() < lineNumber) {
+						ruleLine = rule.getConsequenceLine();
+						resultRule = rule;
+					}
+				}
+				if (resultRule != null && resultRule.getClassName() != null) {
+					return packageDescr.getName() + "." + resultRule.getClassName();
+				}
+			}
+			throw new CoreException(new Status(IStatus.ERROR, DroolsIDEPlugin.getUniqueIdentifier(), 0,
+				"Cannot determine ruleClassName for " + resource + " " + lineNumber, null));
+		} catch(Throwable t) {
+			throw new CoreException(new Status(IStatus.ERROR, DroolsIDEPlugin.getUniqueIdentifier(), 0,
+				"Cannot determine ruleClassName for " + resource + " " + lineNumber, t));
+		}
 	}
 	
-	private static int getRuleLineNumber(IResource resource, int lineNumber) {
-		// TODO
-		return 7;
+	private static int getRuleLineNumber(IResource resource, int lineNumber) throws CoreException {
+		// TODO remove duplicated code
+		try {
+			PackageDescr packageDescr = DroolsIDEPlugin.getDefault().parseResource(resource, true);
+			if (packageDescr != null) {
+				Iterator rules = packageDescr.getRules().iterator();
+				int ruleLine = -1;
+				RuleDescr resultRule = null;
+				while (rules.hasNext()) {
+					RuleDescr rule = (RuleDescr) rules.next();
+					if (rule.getConsequenceLine() > ruleLine && rule.getConsequenceLine() < lineNumber) {
+						ruleLine = rule.getConsequenceLine();
+						resultRule = rule;
+					}
+				}
+				if (resultRule != null && resultRule.getClassName() != null) {
+					String ruleClassName = packageDescr.getName() + "." + resultRule.getClassName();
+					RuleInfo ruleInfo = DroolsIDEPlugin.getDefault().getRuleInfoByClass(ruleClassName);
+					if (ruleInfo != null) {
+						return ruleInfo.getConsequenceJavaLineNumber() + lineNumber - ruleLine;
+					}
+				}
+			}
+			throw new CoreException(new Status(IStatus.ERROR, DroolsIDEPlugin.getUniqueIdentifier(), 0,
+				"Cannot determine ruleLineNumber for " + resource + " " + lineNumber, null));
+		} catch(Throwable t) {
+			throw new CoreException(new Status(IStatus.ERROR, DroolsIDEPlugin.getUniqueIdentifier(), 0,
+				"Cannot determine ruleLineNumber for " + resource + " " + lineNumber, t));
+		}
 	}
 }
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsLocalVariable.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsLocalVariable.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsLocalVariable.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -0,0 +1,22 @@
+package org.drools.ide.debug.core;
+
+import org.eclipse.jdt.internal.debug.core.model.JDILocalVariable;
+import org.eclipse.jdt.internal.debug.core.model.JDIStackFrame;
+
+import com.sun.jdi.LocalVariable;
+
+public class DroolsLocalVariable extends JDILocalVariable {
+	
+	public DroolsLocalVariable(JDIStackFrame frame, LocalVariable local) {
+		super(frame, local);
+	}
+
+	protected void setLocal(LocalVariable local) {
+		super.setLocal(local);
+	}
+	
+	protected LocalVariable getLocal() {
+		return super.getLocal();
+	}
+	
+}

Deleted: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsStackFrame.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsStackFrame.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsStackFrame.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -1,71 +0,0 @@
-package org.drools.ide.debug.core;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.drools.ide.DroolsIDEPlugin;
-import org.eclipse.debug.core.DebugException;
-import org.eclipse.debug.core.model.IVariable;
-import org.eclipse.jdt.internal.debug.core.model.JDIStackFrame;
-
-import com.sun.jdi.StackFrame;
-
-public class DroolsStackFrame extends JDIStackFrame {
-
-	private static final String CONSEQUENCE_SIGNATURE = "(Lorg/drools/spi/KnowledgeHelper";
-	
-	public DroolsStackFrame(DroolsThread thread, StackFrame frame, int depth) {
-		super(thread, frame, depth);
-	}
-	
-	public boolean isExecutingRule() {
-		try {
-			String methodName = getMethodName();
-			String signature = getSignature();
-			if ("consequence".equals(methodName) && signature.startsWith(CONSEQUENCE_SIGNATURE)) {
-				// TODO return name of drl
-				return true;
-			}
-		} catch (DebugException exc) {
-			DroolsIDEPlugin.log(exc);
-		}
-		return false;
-	}
-	
-	public int getLineNumber() throws DebugException {
-		if (isExecutingRule()) {
-			return convertToDRLLineNumber();
-		}
-		return super.getLineNumber();
-	}
-
-	private int convertToDRLLineNumber() {
-		// TODO
-		return 21;
-	}
-	
-	protected JDIStackFrame bind(StackFrame frame, int depth) {
-		return super.bind(frame, depth);
-	}
-	
-	protected StackFrame getUnderlyingStackFrame() throws DebugException {
-		return super.getUnderlyingStackFrame();
-	}
-	
-	protected void setUnderlyingStackFrame(StackFrame frame) {
-		super.setUnderlyingStackFrame(frame);
-	}
-	
-	public IVariable[] getVariables() throws DebugException {
-		IVariable[] variables = super.getVariables();
-		List result = new ArrayList((variables.length - 1)/2);
-		for (int i = 0; i < variables.length; i++) {
-			String name = variables[i].getName();
-			if (!(name.equals("drools")) && !(name.endsWith("__Handle__"))) {
-				result.add(variables[i]);
-			}
-		}
-		return (IVariable[]) result.toArray(new IVariable[result.size()]);
-	}
-	
-}

Added: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsStackFrame.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsStackFrame.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsStackFrame.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -0,0 +1,561 @@
+package org.drools.ide.debug.core;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+import org.drools.ide.DroolsIDEPlugin;
+import org.drools.ide.RuleInfo;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.debug.core.DebugEvent;
+import org.eclipse.debug.core.DebugException;
+import org.eclipse.debug.core.model.IThread;
+import org.eclipse.debug.core.model.IVariable;
+import org.eclipse.jdt.debug.core.IJavaStackFrame;
+import org.eclipse.jdt.debug.core.IJavaThread;
+import org.eclipse.jdt.debug.core.IJavaVariable;
+import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
+import org.eclipse.jdt.internal.debug.core.model.JDIDebugModelMessages;
+import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
+import org.eclipse.jdt.internal.debug.core.model.JDIFieldVariable;
+import org.eclipse.jdt.internal.debug.core.model.JDILocalVariable;
+import org.eclipse.jdt.internal.debug.core.model.JDIReferenceType;
+import org.eclipse.jdt.internal.debug.core.model.JDIStackFrame;
+import org.eclipse.jdt.internal.debug.core.model.JDIThread;
+
+import com.ibm.icu.text.MessageFormat;
+import com.sun.jdi.AbsentInformationException;
+import com.sun.jdi.Field;
+import com.sun.jdi.LocalVariable;
+import com.sun.jdi.Location;
+import com.sun.jdi.Method;
+import com.sun.jdi.NativeMethodException;
+import com.sun.jdi.ObjectReference;
+import com.sun.jdi.ReferenceType;
+import com.sun.jdi.StackFrame;
+
+public class DroolsStackFrame extends JDIStackFrame {
+
+	private static final String CONSEQUENCE_SIGNATURE = "(Lorg/drools/spi/KnowledgeHelper";
+
+	private DroolsThread fThread;
+	private Location fLocation;
+	private List fVariables;
+	private boolean fRefreshVariables= true;
+	private int fDepth = -2;
+	private boolean initialized = true;
+	private StackFrame fStackFrame;
+	private ObjectReference fThisObject;
+	private String fReceivingTypeName;
+	private boolean fLocalsAvailable = true;
+
+	public DroolsStackFrame(DroolsThread thread, StackFrame frame, int depth) {
+		super(thread, frame, depth);
+		bind(frame, depth);
+	}
+	
+	public boolean isExecutingRule() {
+		try {
+			String methodName = getMethodName();
+			String signature = getSignature();
+			String type = getDeclaringTypeName();
+			if ("consequence".equals(methodName) && signature.startsWith(CONSEQUENCE_SIGNATURE)) {
+				return true;
+			}
+		} catch (DebugException exc) {
+			DroolsIDEPlugin.log(exc);
+		}
+		return false;
+	}
+	
+	public RuleInfo getExecutingRuleInfo() {
+		try {
+			String methodName = getMethodName();
+			String signature = getSignature();
+			String type = getDeclaringTypeName();
+			if ("consequence".equals(methodName) && signature.startsWith(CONSEQUENCE_SIGNATURE)) {
+				return DroolsIDEPlugin.getDefault().getRuleInfoByClass(type);
+			}
+		} catch (DebugException exc) {
+			DroolsIDEPlugin.log(exc);
+		}
+		return null;
+	}
+	
+	public int getLineNumber() throws DebugException {
+		synchronized (fThread) {
+			RuleInfo ruleInfo = getExecutingRuleInfo();
+			if (ruleInfo != null) {
+				return ruleInfo.getConsequenceDrlLineNumber() + (getInternalLineNumber() - ruleInfo.getConsequenceJavaLineNumber());
+			}
+			
+		}
+		return getInternalLineNumber();
+	}
+	
+	private int getInternalLineNumber() throws DebugException {
+		try {
+			return fLocation.lineNumber();
+		} catch (RuntimeException e) {
+			if (getThread().isSuspended()) {
+				targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_line_number, new String[] {e.toString()}), e); 
+			}
+		}
+		return -1;
+	}
+
+	public IVariable[] getVariables() throws DebugException {
+		IVariable[] variables = super.getVariables();
+		List result = new ArrayList((variables.length - 1)/2);
+		for (int i = 0; i < variables.length; i++) {
+			String name = variables[i].getName();
+			if (!(name.equals("drools")) && !(name.endsWith("__Handle__"))) {
+				result.add(variables[i]);
+			}
+		}
+		return (IVariable[]) result.toArray(new IVariable[result.size()]);
+	}
+	
+	protected List getVariables0() throws DebugException {
+		synchronized (fThread) {
+			if (fVariables == null) {
+				
+				// throw exception if native method, so variable view will update
+				// with information message
+				if (isNative()) {
+					requestFailed(JDIDebugModelMessages.JDIStackFrame_Variable_information_unavailable_for_native_methods, null); 
+				}
+				
+				Method method= getUnderlyingMethod();
+				fVariables= new ArrayList();
+				// #isStatic() does not claim to throw any exceptions - so it is not try/catch coded
+				if (method.isStatic()) {
+					// add statics
+					List allFields= null;
+					ReferenceType declaringType = method.declaringType();
+					try {
+						allFields= declaringType.allFields();
+					} catch (RuntimeException e) {
+						targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_fields,new String[] {e.toString()}), e); 
+						// execution will not reach this line, as 
+						// #targetRequestFailed will throw an exception					
+						return Collections.EMPTY_LIST;
+					}
+					if (allFields != null) {
+						Iterator fields= allFields.iterator();
+						while (fields.hasNext()) {
+							Field field= (Field) fields.next();
+							if (field.isStatic()) {
+								fVariables.add(new JDIFieldVariable((JDIDebugTarget)getDebugTarget(), field, declaringType));
+							}
+						}
+						Collections.sort(fVariables, new Comparator() {
+							public int compare(Object a, Object b) {
+								JDIFieldVariable v1= (JDIFieldVariable)a;
+								JDIFieldVariable v2= (JDIFieldVariable)b;
+								try {
+									return v1.getName().compareToIgnoreCase(v2.getName());
+								} catch (DebugException de) {
+									logError(de);
+									return -1;
+								}
+							}
+						});
+					}
+				} else {
+					// add "this"
+					ObjectReference t= getUnderlyingThisObject();
+					if (t != null) {
+						fVariables.add(new DroolsThisVariable((JDIDebugTarget)getDebugTarget(), t));
+					}
+				}
+				// add locals
+				Iterator variables= getUnderlyingVisibleVariables().iterator();
+				while (variables.hasNext()) {
+					LocalVariable var= (LocalVariable) variables.next();
+					fVariables.add(new DroolsLocalVariable(this, var));
+				}
+			} else if (fRefreshVariables) {
+				updateVariables();
+			}
+			fRefreshVariables = false;
+			return fVariables;
+		}
+	}
+	
+	protected JDIStackFrame bind(StackFrame frame, int depth) {
+		if (initialized) {
+			synchronized (fThread) {
+				if (fDepth == -2) {
+					// first initialization
+					fStackFrame = frame;
+					fDepth = depth;
+					fLocation = frame.location();
+					return this;
+				} else if (depth == -1) {
+					// mark as invalid
+					fDepth = -1;
+					fStackFrame = null;
+					return null;
+				} else if (fDepth == depth) {
+					Location location = frame.location();
+					Method method = location.method();
+					if (method.equals(fLocation.method())) {
+						try {
+							if (method.declaringType().defaultStratum().equals("Java") || //$NON-NLS-1$
+							    equals(getSourceName(location), getSourceName(fLocation))) {
+								// TODO: what about receiving type being the same?
+								fStackFrame = frame;
+								fLocation = location;
+								clearCachedData();
+								return this;
+							}
+						} catch (DebugException e) {
+						}
+					}
+				}
+				// invalidate this franme
+				bind(null, -1);
+				// return a new frame
+				return new DroolsStackFrame(fThread, frame, depth);
+			}
+		} else {
+			return null;
+		}
+	}
+	
+	public IThread getThread() {
+		return fThread;
+	}
+
+	public Method getUnderlyingMethod() {
+		synchronized (fThread) {
+			return fLocation.method();
+		}
+	}
+	
+	protected List getUnderlyingVisibleVariables() throws DebugException {
+		synchronized (fThread) {
+			List variables= Collections.EMPTY_LIST;
+			try {
+				variables= getUnderlyingStackFrame().visibleVariables();
+			} catch (AbsentInformationException e) {
+				setLocalsAvailable(false);
+			} catch (NativeMethodException e) {
+				setLocalsAvailable(false);
+			} catch (RuntimeException e) {
+				targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_visible_variables_2,new String[] {e.toString()}), e); 
+			}
+			return variables;
+		}
+	}
+
+	protected ObjectReference getUnderlyingThisObject() throws DebugException {
+		synchronized (fThread) {
+			if ((fStackFrame == null || fThisObject == null) && !isStatic()) {
+				try {
+					fThisObject = getUnderlyingStackFrame().thisObject();
+				} catch (RuntimeException e) {
+					targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_this,new String[] {e.toString()}), e); 
+					// execution will not reach this line, as 
+					// #targetRequestFailed will throw an exception			
+					return null;
+				}
+			}
+			return fThisObject;
+		}
+	}
+	
+	public String getDeclaringTypeName() throws DebugException {
+		synchronized (fThread) {
+			try {
+				if (isObsolete()) {
+					return  JDIDebugModelMessages.JDIStackFrame__unknown_declaring_type__1; 
+				}
+				return JDIReferenceType.getGenericName(getUnderlyingMethod().declaringType());
+			} catch (RuntimeException e) {
+				if (getThread().isSuspended()) {
+					targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_declaring_type, new String[] {e.toString()}), e); 
+				}
+				return JDIDebugModelMessages.JDIStackFrame__unknown_declaring_type__1; 
+			}
+		}
+	}
+	
+	public String getSourceName() throws DebugException {
+		synchronized (fThread) {
+			return getSourceName(fLocation); 
+		}
+	}
+
+	public boolean isObsolete() {
+		if (!JDIDebugPlugin.isJdiVersionGreaterThanOrEqual(new int[] {1,4}) || !((JDIDebugTarget)getDebugTarget()).hasHCROccurred()) {
+			// If no hot code replace has occurred, this frame
+			// cannot be obsolete.
+			return false;
+		}
+		// if this frame's thread is not suspended, the obsolete status cannot
+		// change until it suspends again
+		synchronized (fThread) {
+			if (getThread().isSuspended()) {
+				return getUnderlyingMethod().isObsolete();
+			}
+			return false;
+		}
+	}
+	
+	protected boolean exists() {
+		synchronized (fThread) {
+			return fDepth != -1;
+		}
+	}
+	
+	protected StackFrame getUnderlyingStackFrame() throws DebugException {
+		synchronized (fThread) {
+			if (fStackFrame == null) {
+				if (fDepth == -1) {
+					throw new DebugException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IJavaStackFrame.ERR_INVALID_STACK_FRAME, JDIDebugModelMessages.JDIStackFrame_25, null)); 
+				}
+				if (fThread.isSuspended()) {
+					// re-index stack frames - See Bug 47198
+					fThread.computeStackFrames();
+					if (fDepth == -1) {
+						// If depth is -1, then this is an invalid frame
+						throw new DebugException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IJavaStackFrame.ERR_INVALID_STACK_FRAME, JDIDebugModelMessages.JDIStackFrame_25, null)); 
+					}
+				} else {
+					throw new DebugException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IJavaThread.ERR_THREAD_NOT_SUSPENDED, JDIDebugModelMessages.JDIStackFrame_25, null)); 
+				}
+			}
+			return fStackFrame;
+		}
+	}
+
+	protected void setUnderlyingStackFrame(StackFrame frame) {
+		synchronized (fThread) {
+			fStackFrame = frame;
+			if (frame == null) {
+				fRefreshVariables = true;
+			}
+		}
+	}
+	
+	protected void setThread(JDIThread thread) {
+		fThread = (DroolsThread) thread;
+	}
+
+	public String getSourcePath(String stratum) throws DebugException {
+		synchronized (fThread) {
+			try {
+				return fLocation.sourcePath(stratum);
+			} catch (AbsentInformationException e) {
+			} catch (RuntimeException e) {
+				targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_source_path, new String[] {e.toString()}), e); 
+			}
+		}
+		return null;
+	}
+
+	public String getSourcePath() throws DebugException {
+		synchronized (fThread) {
+			try {
+				return fLocation.sourcePath();
+			} catch (AbsentInformationException e) {
+			} catch (RuntimeException e) {
+				targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_source_path, new String[] {e.toString()}), e); 
+			}
+		}
+		return null;
+	}	
+
+	public int getLineNumber(String stratum) throws DebugException {
+		synchronized (fThread) {
+			try {
+				return fLocation.lineNumber(stratum);
+			} catch (RuntimeException e) {
+				if (getThread().isSuspended()) {
+					targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_line_number, new String[] {e.toString()}), e); 
+				}
+			}
+		}
+		return -1;
+	}
+
+	public String getSourceName(String stratum) throws DebugException {
+		synchronized (fThread) {
+			try {
+				return fLocation.sourceName(stratum);
+			} catch (AbsentInformationException e) {
+			} catch (NativeMethodException e) {
+			} catch (RuntimeException e) {
+				targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_source_name, new String[] {e.toString()}), e); 
+			}
+		}
+		return null;
+	}
+
+	protected void updateVariables() throws DebugException {
+		if (fVariables == null) {
+			return;
+		}
+
+		Method method= getUnderlyingMethod();
+		int index= 0;
+		if (!method.isStatic()) {
+			// update "this"
+			ObjectReference thisObject;
+			try {
+				thisObject= getUnderlyingThisObject();
+			} catch (DebugException exception) {
+				if (!getThread().isSuspended()) {
+					thisObject= null;
+				} else {
+					throw exception;
+				}
+			}
+			DroolsThisVariable oldThisObject= null;
+			if (!fVariables.isEmpty() && fVariables.get(0) instanceof DroolsThisVariable) {
+				oldThisObject= (DroolsThisVariable) fVariables.get(0);
+			}
+			if (thisObject == null && oldThisObject != null) {
+				// removal of 'this'
+				fVariables.remove(0);
+				index= 0;
+			} else {
+				if (oldThisObject == null && thisObject != null) {
+					// creation of 'this'
+					oldThisObject= new DroolsThisVariable((JDIDebugTarget)getDebugTarget(),thisObject);
+					fVariables.add(0, oldThisObject);
+					index= 1;
+				} else {
+					if (oldThisObject != null) {
+						// 'this' still exists, replace with new 'this' if a different receiver
+						if (!oldThisObject.retrieveValue().equals(thisObject)) {
+							fVariables.remove(0);
+							fVariables.add(0, new DroolsThisVariable((JDIDebugTarget)getDebugTarget(),thisObject));
+						}
+						index= 1;
+					}
+				}
+			}
+		}
+
+		List locals= null;
+		try {
+			locals= getUnderlyingStackFrame().visibleVariables();
+		} catch (AbsentInformationException e) {
+			locals= Collections.EMPTY_LIST;
+		} catch (NativeMethodException e) {
+			locals= Collections.EMPTY_LIST;
+		} catch (RuntimeException e) {
+			targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_visible_variables,new String[] {e.toString()}), e); 
+			// execution will not reach this line, as 
+			// #targetRequestFailed will throw an exception			
+			return;
+		}
+		int localIndex= -1;
+		while (index < fVariables.size()) {
+			Object var= fVariables.get(index);
+			if (var instanceof JDILocalVariable) {
+				DroolsLocalVariable local= (DroolsLocalVariable) fVariables.get(index);
+				localIndex= locals.indexOf(local.getLocal());
+				if (localIndex >= 0) {
+					// update variable with new underling JDI LocalVariable
+					local.setLocal((LocalVariable) locals.get(localIndex));
+					locals.remove(localIndex);
+					index++;
+				} else {
+					// remove variable
+					fVariables.remove(index);
+				}
+			} else {
+				//field variable of a static frame
+				index++;
+			}
+		}
+
+		// add any new locals
+		Iterator newOnes= locals.iterator();
+		while (newOnes.hasNext()) {
+			DroolsLocalVariable local= new DroolsLocalVariable(this, (LocalVariable) newOnes.next());
+			fVariables.add(local);
+		}
+	}
+	
+	protected void setVariables(List variables) {
+		fVariables = variables;
+	}
+	
+	public String getReceivingTypeName() throws DebugException {
+		if (fStackFrame == null || fReceivingTypeName == null) {
+			try {
+				if (isObsolete()) {
+					fReceivingTypeName=JDIDebugModelMessages.JDIStackFrame__unknown_receiving_type__2; 
+				} else {
+					ObjectReference thisObject = getUnderlyingThisObject();
+					if (thisObject == null) {
+						fReceivingTypeName = getDeclaringTypeName();
+					} else {
+						fReceivingTypeName = JDIReferenceType.getGenericName(thisObject.referenceType());
+					}
+				}
+			} catch (RuntimeException e) {
+				if (getThread().isSuspended()) {
+					targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_receiving_type, new String[] {e.toString()}), e); 
+				}
+				return JDIDebugModelMessages.JDIStackFrame__unknown_receiving_type__2; 
+			}
+		}
+		return fReceivingTypeName;
+	}
+
+	private String getSourceName(Location location) throws DebugException {
+		try {
+			return location.sourceName();
+		} catch (AbsentInformationException e) {
+			return null;
+		} catch (NativeMethodException e) {
+			return null;
+		} catch (RuntimeException e) {
+			targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIStackFrame_exception_retrieving_source_name, new String[] {e.toString()}), e); 
+		}
+		return null;
+	}	
+	
+	private boolean equals(Object o1, Object o2) {
+		if (o1 == null) {
+			return o2 == null;
+		} else {
+			return o1.equals(o2);
+		}
+	}
+	
+	private void clearCachedData() {
+		fThisObject= null;
+		fReceivingTypeName= null;	
+	}
+	
+	private void setLocalsAvailable(boolean available) {
+		if (available != fLocalsAvailable) {
+			fLocalsAvailable = available;
+			fireChangeEvent(DebugEvent.STATE);
+		}
+	}	
+
+	public boolean wereLocalsAvailable() {
+		return fLocalsAvailable;
+	}
+	
+	public IJavaVariable[] getLocalVariables() throws DebugException {
+		List list = getUnderlyingVisibleVariables();
+		IJavaVariable[] locals = new IJavaVariable[list.size()];
+		for (int i = 0; i < list.size(); i++) {
+			locals[i] = new DroolsLocalVariable(this, (LocalVariable)list.get(i));
+		}
+		return locals;
+	}
+	
+}

Added: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsThisVariable.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsThisVariable.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsThisVariable.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -0,0 +1,18 @@
+package org.drools.ide.debug.core;
+
+import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
+import org.eclipse.jdt.internal.debug.core.model.JDIThisVariable;
+
+import com.sun.jdi.ObjectReference;
+import com.sun.jdi.Value;
+
+public class DroolsThisVariable extends JDIThisVariable {
+
+	public DroolsThisVariable(JDIDebugTarget target, ObjectReference object) {
+		super(target, object);
+	}
+	
+	protected Value retrieveValue() {
+		return super.retrieveValue();
+	}
+}

Modified: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsThread.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsThread.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/debug/core/DroolsThread.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -166,5 +166,13 @@
 	protected void setRunning(boolean running) {
 		super.setRunning(running);
 	}
+	
+	protected void dropToFrame(IStackFrame frame) throws DebugException {
+		super.dropToFrame(frame);
+	}
+	
+	protected synchronized void stepToFrame(IStackFrame frame) throws DebugException {
+		super.stepToFrame(frame);
+	}
 
 }

Modified: labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/launching/DroolsSourceLookupParticipant.java
===================================================================
--- labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/launching/DroolsSourceLookupParticipant.java	2006-09-09 11:25:30 UTC (rev 6132)
+++ labs/jbossrules/trunk/drools-ide/src/main/java/org/drools/ide/launching/DroolsSourceLookupParticipant.java	2006-09-10 23:44:58 UTC (rev 6133)
@@ -1,5 +1,6 @@
 package org.drools.ide.launching;
 
+import org.drools.ide.RuleInfo;
 import org.drools.ide.debug.core.DroolsStackFrame;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jdt.launching.sourcelookup.containers.JavaSourceLookupParticipant;
@@ -8,9 +9,9 @@
 
 	public String getSourceName(Object object) throws CoreException {
 		if (object instanceof DroolsStackFrame) {
-			if (((DroolsStackFrame) object).isExecutingRule()) {
-				// TODO return name of drl
-				return "Sample.drl";
+			RuleInfo ruleInfo = ((DroolsStackFrame) object).getExecutingRuleInfo();
+			if (ruleInfo != null) {
+				return ruleInfo.getDrlName();
 			}
 		}
 		return super.getSourceName(object);




More information about the jboss-svn-commits mailing list