Question

Is it possible to create a breakpoint where the condition is that its the start of the recursive process? In other words, the stack should only have one call to the function.

IE consider this workflow:

Main func -> call recursive func -> hit breakpoint -> continue -> recursively call self -> DONT hit breakpoint -> continue recursion.

Was it helpful?

Solution

Try this:

if(!Thread.currentThread().getStackTrace()[2].getMethodName().equals("this method's name"))
     // breakpoint

The 2 is because getStackTrace will return the actual getStackTrace method as part of the stack, your method name, and THEN the caller method, which we want.

OTHER TIPS

I don't this why you want to corrupt your code to achieve this unless you are creating break point programmatically.

Keep the break point inside the recursive function then right click on the break point and select break point properties the enter hit count 1. After hitting the break point first time it going to be disabled.

You will want to add a parameter to your recursive function who's job is to track the depth of the recursion. When you do the recursive call, pass in the current depth value plus one, so it increments on each call. Then set a breakpoint based on depth == 1. Something like this (factorial example):

public static int fact(int n, int depth)
{
  if(n <= 1)
    return 1;
  return n* fact(n-1, depth +1);
}

Alternatively, some IDE's allow you to set breakpoints based on the number of times a line of code is hit. You could try using that instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top