Method to construct instances from strings and return them with their type?
<p>Given two classes <code>A</code> and <code>B</code> with one-argument constructors and a binary function <code>f</code>, I need to parse strings of the form </p>

<pre><code>"f(A(10), B(20))"
"f(A(2), A(3))"
</code></pre>

<p>create the appropriate instances and call the respective functions:</p>

<pre><code>class Functions {
    public static double f(A a1, A a2);
    public static double f(A a, B b);
    public static double f(B b1, B b2);
    // ...
}
</code></pre>

<p>So what I am looking for is a method of the kind</p>

<pre><code>T createInstanceFromString(String desc);
</code></pre>

<p>to create the instances from strings like "A(10)" or "B(20)". Of course and unfortunately, this is impossible in Java.</p>

<p>Any advice on solutions to this problem are greatly appreciated.</p>

<p><em>Edit</em>: I forgot to mention that there is a potentially large number of different classes <code>A</code>, <code>B</code>, <code>C</code>, etc and a large number of functions <code>f</code>, <code>g</code>, etc. I'd like to use <code>createInstanceFromString</code> to separate the parsing from calling, otherwise one has to conditionally parse separately for each function call, which would bring a lot of code duplication.</p>

<p><em>Edit2</em>: The question is not about how to parse, but about how to parse and then return an instance in a general purpose method/factory/whatever.</p>
