Method Name extraction

W

Wojtek

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";
}


Something cheap, preferrably done by the compiler as a compiler
directive, rather than a runtime action.
 
W

Wojtek

ahh, that would be:

this.getClass().getName()

And Logtrace takes (classname, methodname, message) as its parameters,
not that it matters
 
A

Alessio Stalla

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.
Something cheap, preferrably done by the compiler as a compiler
directive, rather than a runtime action.

That's harder, I guess.

Bye,
Alessio
 
M

markspace

Wojtek said:
ahh, that would be:

this.getClass().getName()


"this" is not needed, it's implied. This works the same as:

getClass().getName();

However the above is a method call. You might want to consider a
constant. That would be "cheaper" than the method call.

ClassName.class.getName();

Sorry, I don't know any way to get the method name or the line number
(or the filename). I'd almost be interested to see those added to the
compiler spec. I wish I'd thought of that when they were asking for
simple compiler changes.
 
W

Wojtek

markspace wrote :
"this" is not needed, it's implied. This works the same as:

getClass().getName();

However the above is a method call. You might want to consider a constant.
That would be "cheaper" than the method call.

ClassName.class.getName();

Yes I have a constant:
public static final String CLASS_NAME = Foo.class.getName();
Sorry, I don't know any way to get the method name or the line number (or the
filename). I'd almost be interested to see those added to the compiler spec.
I wish I'd thought of that when they were asking for simple compiler
changes.

Me too.
 
W

Wojtek

Alessio Stalla wrote :
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.
 
D

Daniel Pitts

Wojtek said:
Alessio Stalla wrote :

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.
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.
 
W

Wojtek

Daniel Pitts wrote :
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"

A little to fragile for me, moreover I would need to refactor the
entire code base (160,000+ lines right now).

I was hoping for an annotation or something already existing that is
mostly benign
 
J

Joshua Cranmer

Wojtek said:
Something cheap, preferrably done by the compiler as a compiler
directive, rather than a runtime action.

Well, this probably isn't a feasible option for you, but it is possible
to use a C preprocessor (e.g., cpp) on Java code...
 
D

Daniel Pitts

Wojtek said:
Daniel Pitts wrote :

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).
 
A

Arne Vajhøj

Joshua said:
Well, this probably isn't a feasible option for you, but it is possible
to use a C preprocessor (e.g., cpp) on Java code...

It can definitely insert filename and line number.

But method name ??

(that would require understanding Java syntax)

Arne
 
A

Arne Vajhøj

Wojtek said:
Daniel Pitts wrote :

Hmmm, from Wikipedia: "if a programmer makes a logical mistake in
expressing crosscutting, it can lead to widespread program failure"

A little to fragile for me,

Not that much different from other code.

Software bugs can cause software failure. That is how it is.

AOP can make it a bit tricky to track down what exactly is going on,
but if you limit your AOP usage, then it is manageable.
moreover I would need to refactor the entire
code base (160,000+ lines right now).

I was hoping for an annotation or something already existing that is
mostly benign

No matter what technique you decide on then you will need
modify code where you get the value.

The aspects that makes the value available can be compiled/weaved
into the code by the compiler/processor.

Arne
 
W

Wojtek

Daniel Pitts wrote :
Hmm, were you hoping for something like:

@FillWithMethodName
final String myMethodName = "";

Well yes, actually...

Though I was thinking more like:

@MethodNameExtract(WOJTEK1)
private static final String WOJTEK1 = "";
public String getFoo()
{
Log.trace(CLASS_NAME,WOJTEK1,"getting Foo");
return "Foo";
}

So after the (pre) compiler does its thing, the code would look like:

private static final String WOJTEK1 = "getFoo";
 
A

Arne Vajhøj

Wojtek said:
Daniel Pitts wrote :

Well yes, actually...

Though I was thinking more like:

@MethodNameExtract(WOJTEK1)
private static final String WOJTEK1 = "";
public String getFoo()
{
Log.trace(CLASS_NAME,WOJTEK1,"getting Foo");
return "Foo";
}

So after the (pre) compiler does its thing, the code would look like:

private static final String WOJTEK1 = "getFoo";

I think that would be difficult because Java is not member
order aware in that way.

Arne
 
A

Arne Vajhøj

Wojtek said:
Daniel Pitts wrote :

Well yes, actually...

Though I was thinking more like:

@MethodNameExtract(WOJTEK1)
private static final String WOJTEK1 = "";
public String getFoo()
{
Log.trace(CLASS_NAME,WOJTEK1,"getting Foo");
return "Foo";
}

So after the (pre) compiler does its thing, the code would look like:

private static final String WOJTEK1 = "getFoo";

Here is an AspectJ example:

public class C1 {
public void m1() {
System.out.println(Current.getClznam() + " " +
Current.getMthnam());
C2 o = new C2();
o.m2();
}
public static void main(String[] args) {
C1 o = new C1();
o.m1();
}
}

public class C2 {
public void m2() {
System.out.println(Current.getClznam() + " " +
Current.getMthnam());
}
}

import org.aspectj.lang.*;
import org.aspectj.lang.annotation.*;

@Aspect
public class Current {
private static String clznam;
private static String mthnam;
@Pointcut("call(public * *.*(..)) && !within(Current) &&
!call(public * Current.*(..)) && !call(public * java.lang.*.*(..))")
public void allcalls() { };
@Before("allcalls()")
public void beforeCall(JoinPoint thisJoinPoint) {
clznam = thisJoinPoint.getSignature().getDeclaringTypeName();
mthnam = thisJoinPoint.getSignature().getName();
}
public static String getClznam() {
return clznam;
}
public static String getMthnam() {
return mthnam;
}
}

Absolutely not good OOP code, but it shows some of the
capabilities.

Arne
 
W

Wojtek

Arne Vajhøj wrote :
It can definitely insert filename and line number.

But method name ??

(that would require understanding Java syntax)

Hmmm, when I get the time, maybe I will write an Eclipse plugin that
gets called at complie time to preprocess the file and trigger on an
annotation.

When I get the time, sigh...
 
M

Martin Gregorie

What's wrong in having just:

public String getFoo()
{
Log.trace("YourClassName", "getFoo", "getting Foo"); return "Foo";
}

?
I prefer

public String getFoo()
{
String foo = "Foo";
Log.trace(foo + " getFoo()");
return foo;
}

because its rather more informative, but ymmv.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top