Excellent point.  When I call beginning-of-defun interactively it works
great, but when I call it from a program it goes to the beginning of the
file.  Is there some trick to getting the "java" version of
beginning-of-defun?
Also, I tried to call mark-defun indent-region after I inserted my
instrumentation code, but that barfs too.  Here is my attempt.  I haven't
tracked donw how to get the class and method name.  




(defun add-timer()
  (interactive)
  "Add Timer calls"
  (beginning-of-defun)
;  (re-search-forward "\{")
  (next-line 1)
  (beginning-of-line)
  (insert "try {\n" "Timer.startTimer(\"" "X" "\");\n")

  (end-of-defun)
;;  (re-search-backward "}")
  (next-line -1)
  (beginning-of-line)
  (insert "} finally {\n"
          "Timer.stopTimer(\"" "X" "\");\n"
          "}\n")

  (mark-defun)
  (indent-region)
 )




-----Original Message-----
From: Troy Daniels [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 24, 2002 4:32 PM
To: [EMAIL PROTECTED]
Subject: Re: A func to insert Timer code?


I think a better template to use is the following:

// Original version
int getValue() {
   return super.getValue() * 5;
}

// Instrumented version
int getValue() {
   try {
     Timer.startTimer("Test.getValue()");
      return super.getValue() * 5;
   } finally {
     Timer.stopTimer("Test.getValue()");
   }
}

This also handles correctly cases where there are multiple return 
statements, and you get timing information even if there's an 
exception.  The implementation is fairly simple:

(beginning-of-defun)
(insert "try {\n  Timer.startTime(" (get-function-name) ");\n")
(end-of-defun)
(insert "} finally {\n  Timer.stopTimer(" (get-function-name) ");\n}\n")

This isn't quite right.  At least on my emacs, the defun functions put you 
on the wrong side of the braces.  get-function-name is probably also not 
the right function name, but there is some function that does that.  (The 
name shows up in my modeline, so probably semantic/senator knows it.)

Troy

At 09:02 PM 1/24/02 +0100, Paul Ebermann wrote:
>""Altmann, Michael"" skribis:
>
> > I have been using xemacs and jde for quite a while, but I am not much of
an
> > elisp programmer.  Could someone help me (or point me to the appropriate
> > online resource) write the following elisp function?  I have a java
class
> > called Timer that I use to time various chunks of code.  It has methods
> > startTimer and stopTimer that should surround any code I want to time.
I
> > would like an elisp function to insert calls to Timer for the current
java
> > method.  For example, given a java program that looks like
> >
>[...]
> >
> > I would like code inserted like
> >
>[...]
> > void bar() {
> > Timer.startTimer("Test.bar");
> > int x=3;
> > Timer.stopTimer("Test.bar");
> > }
> > }
>
>(Sorry for missing tabs, my newsreader ignores or deletes them.)
>
>What should be done here?
>---
>int getValue() {
>     return super.getValue()*5;
>}
>---
>The simple Version
>---
>int getValue() {
>     Timer.startTimer("Test.getValue()");
>     Timer.stopTimer("Test.getValue()");
>     return super.getValue() * 5;
>}
>---
>is not really helpful (it does not time
>anything time-consuming).
>You may need something like
>---
>int getValue() {
>     Timer.startTimer("Test.getValue()");
>     int temp = super.getValue() * 5;
>     Timer.stopTimer("Test.getValue()");
>     return temp;
>}
>---
>So your function has to choose a name for a
>temporary variable, and using the right type ...
>
>
>Sorry, this does not really help, but only
>shows a possible problem.
>
>Paul

Troy Daniels
[EMAIL PROTECTED]
781-273-3388 x218

Reply via email to