A facility for getting the current method's name

Sean Chou zhouyx at linux.vnet.ibm.com
Mon Sep 26 00:05:22 PDT 2011


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