• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • 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?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I thought too. Refactor time
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!