开发者

How to get hold of a method name without using magic strings

In java is there any library to get the field name without using any magic strings, for example, for following class

class User
{
int id;

int getId()
{
return id;
}

void setId(int id)
{
this.id = id;
}
}

Now what i want is something like this,

String fieldName = Utility.getFieldName(User.getId());

this should return text "id"

I know this is not achievable开发者_运维问答 by usual means, but I am thinking if it can be done by wrapping the User class with a dynamic proxy (so this would be more like

String fieldName = Utility.getFieldName(Utility.createProxy<User>().getId());

) and intercepting the method call and obtaining method name from stack, and use some thread joins to look it like a simple method call when looked at the Utility class. Does anyone knows if there is any library to do this type of operation?


This may be done using reflection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜