Batakj
Batakj

Reputation: 12743

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.

Upvotes: 2

Views: 1891

Answers (2)

cjs
cjs

Reputation: 27231

I'm guessing that your data for these movies are stored in an RDBMS. Just do a simple relational query to get exactly the results you need. This problem is a perfect example of the sort of for which people developed relational database management systems.

Upvotes: 1

Yuval Adam
Yuval Adam

Reputation: 165242

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.

Upvotes: 18

Related Questions