This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Dynamically Access an Object Property Value

 
Ranch Hand
Posts: 1950
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a list of objects I am iterating thru. Each object has several int properties(fields).

Before my iteration loop begins I know which property that I want to return the value of.

In my loop syntax how can I set up the getter dynamically without massive if statements?

Here is where I am at so far but cannot see how to get the value.(The property value I need returned may be userp15)

 
Marshal
Posts: 14046
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I have to say that's quite a doozie of a code listing you got there.

What's wrong with just calling getUserP15()?

Also, there's something fundamentally wrong with a call that looks like this:

I hope that's just a typo because otherwise, what does that even mean? You'd never do someObject.toString().toString(), would you?
 
Steve Dyke
Ranch Hand
Posts: 1950
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Well, I have to say that's quite a doozie of a code listing you got there.

What's wrong with just calling getUserP15()?

Also, there's something fundamentally wrong with a call that looks like this:

I hope that's just a typo because otherwise, what does that even mean? You'd never do someObject.toString().toString(), would you?



Sorry had to leave the entire quote. Thanks for the feedback.

First, I am trying to make the getter dynamic because this method can take one of several values(the calling code could send userp12, userp55, userp75, etc.)

Second. I have a Default class that has EmployeeMasterList employeeMasterList object as a property.

The EmployeeMasterList class has a List(of EmployeeMaster objects) property called employeeMasterList
 
Junilu Lacar
Marshal
Posts: 14046
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really shouldn't assume people will be able to infer things from code like that. They might be able to guess but the more details you give, the easier it is to give helpful advice.

It seems to me that you're trying to use a String, which is what I'm guessing preferenceFieldName is, to figure out which getter method to invoke inside your loop. Using the Reflection API is one choice but it's not ideal. It's hard to justify using the Reflection API in business logic like this.

The obvious brute force way would be to have a series of if-else statements like so:

Since you'd only have a limited number of these getUserpXX() methods in EmployeeMaster, it's still manageable albeit a bit tedious when you have more than a few properties.

You might be able to use a Map<String, IntSupplier> and lambdas or method references to whittle a long if-else-if-else series of statements down to a few lines of code.
 
Steve Dyke
Ranch Hand
Posts: 1950
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

The obvious brute force way would be to have a series of if-else statements like so:



Thanks. I was really trying to avoid the brute force method, making it more flexible if more properties are added(currently there are 14).

I was hopping to be able to dynamically construct the .getUserpXX syntax.
 
Marshal
Posts: 7180
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is confusing in many ways.

The declaration has nothing to do with the leading comment. In other words, userp12 isn't an object, it is a variable of primitive type int.

Also that line isn't the loop. Loop is somewhere down below. So I'd suggest remove those comments completely in order not to clutter code even more.

Class EmployeeMaster to me somehow doesn't read fluently. EmployeeMaster constructor accepting database Connection and ResultSet also isn't intuitive. Seems like this EmployeeMaster just facilitates some work space for the need of some dirty work to do.

You may want to explain in a bit more abstract words what are you trying to achieve?



 
Liutauras Vilda
Marshal
Posts: 7180
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not clear to me, where the preferenceField = preferenceFieldName; value, which is on the right hand side comes from?
 
Junilu Lacar
Marshal
Posts: 14046
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Dyke wrote: I was really trying to avoid the brute force method, making it more flexible if more properties are added(currently there are 14).

I was hopping to be able to dynamically construct the .getUserpXX syntax.


There's always Apache Commons BeanUtils: http://commons.apache.org/proper/commons-beanutils/

Better that than trying to roll your own Reflection API-based code.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!