• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I thought too. Refactor time
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic