Java get valueOf for generic subclass of java.lang.Number or primitive
<p>After reading through a lot of questions, I asked myself if it is possible to solve the dilemma of transforming a string into a generic number WITHOUT using a hardcoded approach.</p>

<p>For example: I get from a method a parameter with the type Class
With Number.isAssignableFrom or other ways I can check, if this is a number class. But I also get from the user an input. As a string. </p>

<p>The question is: Can I now somehow transform this string into the requested number object without building an if statement for every case? </p>

<p>Example code, properly not working:</p>

<pre><code>Object ret = null;

for(int i=0;i&lt;method.getParameterTypes().length; i++ ) {
    Class&lt;?&gt; param = method.getParameterTypes()[i];
    String argString = getUserInput(_in, "Argument ("+param.getSimpleName()+"): ");

    if( Number.isAssignableFrom(param) )
        ret = ((Class&lt;NumberChild&gt;)param).valueOf(argString);
    /// OR
        ret = param.cast( Double.valueOf(argString) )
}
</code></pre>

<p>The even advance this question: Could it be possible to cast every primitive in something similar in from the above way?</p>

<p>Note: The approach here should completely focus on a non hardcoded solution. My currently running code uses the approach of hardcoding every single case. But in those cases a more general solution would be much more interesting.</p>

<p><strong>EDIT:</strong>
I'm sorry for misunderstandings, but I mean with an hardcoded approach, an approach that tests through every possible case like:</p>

<pre><code> if( integer ); do ...
 if( double ); do ...
 if( long ); do ...
</code></pre>

<p>But that is exactly, what I want to work around. To clarify: This is just a challenge. My life or code is not depending on it, I just want to know IF it is possible!!</p>