A facility for getting the current method's name
Behrang Saeedzadeh
behrangsa at gmail.com
Mon Sep 26 04:31:50 PDT 2011
Sean, Mario,
I am looking for something like JavaScript's arguments.callee.name or
Ruby's __method__. AFAIK, accessing the current method's name is a
very cheap operation in JS and Ruby while creating a stack trace is a
moderately expensive operation in Java.
Having something like Method.getCurrentMethod() would not require any
changes to the syntax of the language and should be possible without
breaking backwards compatibility.
Cheers,
Behrang Saeedzadeh
http://www.behrang.org
On Mon, Sep 26, 2011 at 5:05 PM, Sean Chou <zhouyx at linux.vnet.ibm.com> wrote:
> Do you mean method literal ? There is a proposal on 2009's coin.
> http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000388.html
>
> 2011/9/26 Behrang Saeedzadeh <behrangsa at gmail.com>
>>
>> Hi,
>>
>> Would it be possible to add a feature to get the current method's name
>> in an efficient without first creating a stack trace?
>>
>> Something like:
>>
>> public void foo() {
>> String methodName = Method.getCurrentMethod().getName();
>> assert methodName.equals("foo");
>> }
>>
>> This would make writing code like this more elegant and refactor-friendly:
>>
>> public void init(Bar bar) {
>> if (this.bar != null) {
>> throw new IllegalStateException(Method.getCurrentMethod().getName()
>> + " should not be called more than once");
>> }
>> this.bar = bar;
>> }
>>
>> Cheers,
>> Behrang Saeedzadeh
>> http://www.behrang.org
>
>
>
> --
> Best Regards,
> Sean Chou
>
>
More information about the jdk8-dev
mailing list