title: How to create an object from a string in Java (how to eval a string)?
body: <p>I know eval is "evil", but I'm using it in a way that the user can't ever abuse it. </p>
  
  <p>Let's say I've got a string "new Integer(5)". I want to do something such that I can set a variable, let's say foo, to new Integer(5). Something like</p>
  
  <pre><code>Integer foo;
  String bar = "new Integer(5)"
  *magic happens*
  System.out.println(foo) -&gt; 5
  </code></pre>
  
  <p>I've looked around and it looks like I have a few options. Can the getSystemJavaCompiler() method in ToolProvider do this? Or should I use BeanShell? Or is there something else? Note that this is from a string, not a file.</p>