NimChimpsky
NimChimpsky

Reputation: 47290

How to get only the class name with no package information using java refleciton

Class MyClass has a method getMyClassId and I want to invoke something like this :

Method method = clazz.getMethod("get" + clazz.getName() + "Id");     
method.invoke(myObject)

But clazz.getName() returns the fully qualified package information, I could do some string manipulation, but wondered if there was a better way ?

Upvotes: 0

Views: 66

Answers (2)

Panos Bariamis
Panos Bariamis

Reputation: 4653

try

Method method = clazz.getMethod("get" + clazz.getSimpleName() + "Id");

Upvotes: 0

Morfic
Morfic

Reputation: 15508

Try using class.getSimpleName()

Upvotes: 4

Related Questions