This questions is close to this, but there is a major difference.
May requirements:
(1) I want to generate a Java function with generic return value.
(2) The list of input parameters is always the same. (unlike in link above).
(3) The function shall know, which type of return parameter is expected.
My tries:
public <T> T getProperty(String name)
{
T value;
try {
if (T instanceof String) {value = (T) getStringProperty(name);}
if (T instanceof Long) {value = (T) getLongProperty(name);}
if (T instanceof Boolean) {value = (T) getBooleanProperty(name);}
} catch (Exception e) {
LOGGER.error("error at getProperty", e);
}
return value;
}
This it is not working, since no instance of T
is generated. Trys with T value = new T();
also failed.
Any quick fixes? Or is this approach not recommended?