This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Using Java Reflection to call local methods. Is this legit?

 
Sheriff
Posts: 4720
309
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across some code today that looked very much like this, although in the actual code the method list was 82 items long:

A side effect of this was that IntelliJ reported that method1() and method2() were unused and invited me to delete them. Not good.

If it were me I would not have thought to use Reflection here and would probably have written it simply as this:

Can anyone present a compelling argument that might explain why the original author would have chosen to use Reflection here?
 
Sheriff
Posts: 14758
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't think of any reason other than just generally mucking about with reflection to see how it works. With 82 items, I'm inclined to think that you might have had a Wally or Jack who was trying to look busy.
 
Tim Cooke
Sheriff
Posts: 4720
309
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I thought too. Refactor time
 
author
Posts: 23855
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

One possibility, perhaps as a future enhancement, is to add support for a pattern. Imagine being able to call "method\\d+" ...

Henry
 
Tim Cooke
Sheriff
Posts: 4720
309
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Funny you should mention that Henry, because that very thing happens elsewhere in this codebase for Object creation. Instances of message type classes MessageXX where the XX part is derived from an enum value. It's all rather unpleasant.

For this example though, until that future enhancement becomes useful I'm winding it back to something much more straight forward.
 
What are you doing in my house? Get 'em tiny ad!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!