Can a method variable be accessed using reflection?

How can I access a variable defined inside a method by using reflection?

I have to create criteria in Hibernate. The database is attribute based.

Consider a movie. A movie can have many languages and many genres. e.g.

Movieid           Property           Value
1                 Language           Hindi
1                 Language           English
1                 genre              action
2                 genre              comedy

Let’s assume there are a thousand records of different movies. The user selects a set of genres(action) and languages(hindi, english) to filter the result. Now, I have to create criteria in this order : expression = (property= english and genre = action) or (property= hindi and genre = action)

For the implementation, I have to lookup into a local variable defined inside a for loop condition.

Answer

You can’t. If it’s defined in a method, is it a local variable defined in that scope only. Since the method had no state outside of its scope, you have nothing to access.

Accessing a class member (which, by definition, is a state) you can access via “regular” reflection.

Leave a Reply

Your email address will not be published. Required fields are marked *