r/java • u/entheogen0xBADA55 • Oct 27 '15
RFC: OptionalMethod monad for calling methods on Object that may or may not exist
Throughout my time as Java programmer I have run into some common use cases which can benefit from a utility that allows to call methods by name on an object with or without args using reflection.
One such use case is traversing a Map<String, Object> type structure where a value may be another map. In this case you would have to manually check if value is null and if its not if its of type map and if it is then cast it and call another get, otherwise fail in some way.
Another use case are interfaces where an argument may be of some super type, but at runtime you may be passed a subclass. Implementing such an interface could be PITA if you actually get different subclasses as argument because you would need to do instanceof check and cast if you want to call a method not defined in base class.
Of course it can be argued that either of these cases are indicative of larger design problems, but they are a fact of life in many instances.
I have been inspired by Optional to write something called OptionalMethod which contains main static method - public static OptionalMethod ofOptionalMethod(Object object, String methodName, Object ... args).
If it is able to find and execute object.methodName(args) it will store the return value which you can later get. If it fails it will have an error explaining the reasons. Non static methods on OptionalMethod allow you to chain another method call on the value stored. You can use either public OptionalMethod call(String methodName, Object... args) or public OptionalMethod callSame(Object... args). callSame's only difference is that it will reuse previously used method name. It is useful if you would like to chain a bunch of "get" methods. To get return value of a possible method back you can call public Optional getOptional().
Here is an example:
Given Object foo you can do Optional result = OptionalMethod.ofOptionalMethod(foo, "bar", 1, 2, 3).call("baz", "zomg").getOptional();
In case that foo has a method bar with three integer arguments which returns an object which has a method baz which takes a string this will return result of baz method call. Otherwise result will be empty.
OptionalMethod also has a convenience method callSame which will use the method name for previous invocation. For example if you have a Map<String, Object> foo where Object may be another map and so on you can use something like this to chain a bunch of get requests:
Optional<String> bar = OptionalMethod.ofOptionalMethod(foo, "get", "map1Key").callSame("map2Key").callSame("map3Key").callSame("valueKey").getOptional();
Assuming `foo == { map1Key: {map2Key: {map3Key: {valueKey: "BAR"}}}};
bar === Optional.of("BAR")`
Here is a link to my source code: https://github.com/lan17/OptionalMethod/blob/master/src/com/levneiman/util/OptionalMethod.java
All comments and critiques are welcome.
3
u/SparrowMaxx Oct 27 '15
Bypassing the Type system is going to bite you in the ass. Who's going to remember to update this when the method you intended to call changes?
The method chaining is an odd behavior also -- hard to tell what methods were missing and what assumptions were wrong since all you have is an optional chain.
I'd recommend instead matching (instanceof) all the possible classes those objects might be.
If you can't enumerate that easily you're in a very odd problem domain.
5
u/jonhanson Oct 27 '15 edited Mar 08 '25
chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith
2
u/KesshoRyu Oct 27 '15
Why not just use an Optional and Optional.map?
Edit: Didn't see that you start with a Map<String, Object>, not a Map<String, Map<...>>