Since Java doesn't support union types when specifying return types (an unfortunate oversight, given that it supports them elsewhere), you must specify a single return type.
Depending on how the callers wish to respond to the different values, various options exist:
- if the callers wish to treat all types the same, define a common superinterface for the types that allows that caller to do so, and return that one
- if the caller wish to detect and distinguish the types, you can:
- return Object, document the types, and have them use
instanceof
. This is not type safe, i.e. the compiler will not warn if you check against the wrong type, or if a refactoring has changed the type
- return a type that provides getters for all possibilites as well as means to discover which value was set. This is more type safe, but requires some care to be safe if a refactoring were to add additional types later.
- pass the value in typesafe callbacks that the caller provides to you, or return a value that accepts a visitor. This requires more setup, and using callbacks may be inconvenient for the caller, but it does allow for type safe switching of arbitrary types
- pass the typical value by returning it, and atypical values by throwing them, possibly using checked exceptions. This is slightly easier for the caller than using callbacks, but still rather cumbersome. There is also a slight runtime cost in capturing the stacktrace (which you could disable, but that might be unexpected), so using this for very frequent occurrences may be suboptimal. This approach is particularly suitable if the exceptional values typically cause the caller to abort what it is doing and escalate the exception to its callers.
An example for the second technique:
class FooReturn {
enum Type {
number,
bool,
complex
}
final Type type;
int number;
boolean bool;
ComplexType complex;
FooReturn(int x) {
type = number;
number = x;
}
// other constructor and getters
}
The use of an enum allows typesafe switching, and which will alert the caller if possible types are added or removed in a later refactoring:
var value = foo(42);
switch (value.getType()) {
case number:
int n = value.getNumber();
// deal with it
return whatever;
case bool:
if (value.getBool()) {
return doThis();
} else {
return doThat();
}
case complex:
var c = value.getComplex();
return doSomethingComplex(c);
}
throw new AssertionError("Unexpected type: " + value);
The caller may also use if-statements, of course:
var value = foo(42);
if (value.getType() == FooReturn.Type.number) {
dealWithNumbers(42);
} else {
throw new IllegalStateException("This should not be possible :-/");
}
This doesn't offer exhaustiveness checking, but can be useful if you only need to deal with a small subset of cases.
Edit
The intention is to use the return of the aforementioned method to invoke other methods that have predefined arguments (such as int, boolean and String) using Java's reflection API (invoke).
That probably doesn't require distinguishing or type safety. Since Method.invoke
takes an Object[]
as argument, I'd simply return that array, so the caller can do:
myMethod.invoke(foo(42));
If the foo method knows the method being invoked, having it return a lambda might more sense, because it might allow bypassing the reflection thing and therefore be more type safe:
foo(42).run();
where foo is being defined like
Runnable foo(int x) {
if (x == 42) {
return () -> someObject.methodOne();
} else {
return () -> someObject.methodTwo();
}
}
Object
is too generic, and I would not recommend it, since it basically reduces the type safety of this code significantly. I'd suggest using either the Interface related to the return type or a parent class of the return type(s) for this kind of method.