Petr Mensik
Petr Mensik

Reputation: 27526

Get static variable in the class from import static by reflection

Like the title suggests, I have a bunch of classes which uses import static package.class.CONSTANT; to import some constant into them. And now I would like to get this constant from the class by reflection, is something like that possible?Because this is not obviously a field of given class so ClassWithConstantImported.class.getDeclaredFields() doesn't return me anything (in case there are no other fields). Any tips?

Upvotes: 1

Views: 1019

Answers (2)

Sebastian Redl
Sebastian Redl

Reputation: 72054

No, that's not possible. Imports are resolved by the compiler and leave no trace in the compiled class. You can potentially find references to the constants but there is no way to tell if there was an import directive for it.

Upvotes: 2

Norwæ
Norwæ

Reputation: 1575

It is in not possible, since the imported constant is not part of the class file of the importing class (its value might be, though).

Upvotes: 1

Related Questions