Type checking in variable-length argument lists
<p>I need to create a TreeNode class, that will be able to store <strong>childs of two types</strong>: String and TreeNode. <strong>Number of childs is not fixed.</strong></p>

<p>I want to create TreeNode objects somehow like this:</p>

<pre><code>TreeNode a = new TreeNode("str", new TreeNode("str2"), "str3"); //Correct
TreeNode b = new TreeNode(a, "str4); //Correct
TreeNode c = new TreeNode(54); //Wrong
</code></pre>

<p>How can I do arguments <strong>type checking</strong> with wildcards or something else in <strong>compile time</strong>?</p>

<p>My inappropriate runtime solution:</p>

<pre><code>private static final boolean debug = "true".equals(System.getProperty("debug"));

public &lt;T&gt; TreeNode (T... childs) {
    if (debug) {
        for (Object child : childs) {
            if (!(child instanceof String || child instanceof TreeNode)) {
                throw new RuntimeException("Type of childs must me Tree or String");
            }
        }
    }
}
</code></pre>
