• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1413
  • Last Modified:

Java Indirection

Still getting use to Java (Moved over from C/C++ and C#)

Basically I have a trace mechanism here that checks when a method is entered and exited, it allows me to actively debug my code, especially when problems are found. (Extremely helpful since I am new to java)

Basically I have a problem with Indirection, where a we do a check for the calling class (and we get the trace class).


public static void traceEn(Object myObject, String myMethod)
  {

   String obj = myObject.getClass().getName();
      if (obj.length()>PACKAGE_LENGTH)
          {
              obj = obj.substring(PACKAGE_LENGTH);
          }
               String myString = obj+ ";" +myMethod+ ";" +Thread.currentThread();
         //Calls tracer method to log the trace
          Tracer.trace(Trace.ENTER_METHOD, myString);
  }

Towards the bottom of the class file I have

 final public static void enter( Object inObject )
{
  traceEn(inObject,getMethodNameWithParams(""));
}

I think I am getting muddled somewhere in this lot, and loosing the plot. As I know a certain method is calling this, and it merely picks up its own class and methods.

Any ideas?
0
JimmyHackNSlash
Asked:
JimmyHackNSlash
  • 8
  • 4
1 Solution
 
aozarovCommented:
StackTraceElement[] stackElements - new Exception().getStackTrace();
// current class should be at
stackElements[stackElements.length - 1];
// caller should be at
stackElements[stackElements.length - 2];
0
 
aozarovCommented:
replace (= instead of -):
StackTraceElement[] stackElements - new Exception().getStackTrace();
with
StackTraceElement[] stackElements = new Exception().getStackTrace();
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
JimmyHackNSlashAuthor Commented:
Cheers, I will take a stab at that. I am assumming that all we are doing there is creating a stack and throwing all the methods in, then going back up as far as needed?
0
 
aozarovCommented:
>> creating a stack and throwing all the methods in, then going back up as far as needed?
Right, java populates this stack with all the methods (and their class information).
0
 
JimmyHackNSlashAuthor Commented:
Would you have an example of what

stackElements[stackElements.length = 2];

Would Give?
0
 
JimmyHackNSlashAuthor Commented:
Sorry that should be

stackElements[stackElements.length - 2];

0
 
aozarovCommented:
stackElements[stackElements.length - 2];  gives the StackElement (classname, methodname, line number) of the method that calls the method that is calling this line.
0
 
aozarovCommented:
lets say the flow: main -> C.method1 -> B.method2 (which calls stackElements[stackElements.length - 2] and gets C.method1)
0
 
JimmyHackNSlashAuthor Commented:
Anyway just to pull out the classname, or method?
0
 
aozarovCommented:
pull out what ever you need. that StackTraceElements contains all the information.
0
 
aozarovCommented:
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now