Re: Method Name extraction
Wojtek wrote:
Daniel Pitts wrote :
Wojtek wrote:
Alessio Stalla wrote :
On Sep 9, 5:18 pm, Wojtek <nowh...@a.com> wrote:
This has probably been hashed to death, however,
It these a way to extract the name of the method from within that
method?
public String getFoo()
{
Log.trace(this.class.GetName(), "getFoo", "I am in the method");
return "Foo";
}
public String getFooBar()
{
Log.trace(this.class.GetName(), "getFooBar", "I am in the method");
return getFoo() + "Bar";
}
If you are using this for logging, chances are that your log
implementation already does what you want, and you just have to tell
it to print the method name. Log4j for example supports this.
Else, you can inspect the return value of Thread.currentThread
().getStackTrace(). You can study the log4j source code to see how
they do it.
Yes, but that takes runtime cycles which I am loathe to waste on
logging. Besides the method name is also used in other places such as:
sql.commit("methodName");
I know I can set up a:
final String methodName="Foo";
but again that is runtime expense and also means that if I refactor
the method name, then I also need to change the text.
Something cheap, preferrably done by the compiler as a compiler
directive, rather than a runtime action.
You can use AOP and either instrument your code at class-load time or
at compile-time. Look for different AoP implementations, you might
find one you want.
Hmmm, from Wikipedia: "if a programmer makes a logical mistake in
expressing crosscutting, it can lead to widespread program failure"
If you make a logical mistake in a program, it can cause failure, who'd
have thought.
A little to fragile for me,
Don't let that scare you away, Using AoP to inject "methodName" into a
few places probably will be worth it.
moreover I would need to refactor the entire
code base (160,000+ lines right now).
Lines of code don't really mean anything. It would be number of methods
that already have a solution, vs the number of methods you want to apply
your advice too. Applying advice affects everything you tell it to,
without having to tell every place about it.
I was hoping for an annotation or something already existing that is
mostly benign
What would an annotation give you? That's almost a non sequitur.
Hmm, were you hoping for something like:
@FillWithMethodName
final String myMethodName = "";
Hardly seems better than using AOP (and thats the only way it would be
achieved anyway).
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>