3

I know eval is "evil", but I'm using it in a way that the user can't ever abuse it.

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

Integer foo;
String bar = "new Integer(5)"
*magic happens*
System.out.println(foo) -> 5

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.

swampsjohn
  • 6,266
  • 7
  • 35
  • 40
  • 2
    I think the fear that the user will 'abuse' eval is not the main evil. In most cases it is programmers that abuse eval. In a strongly typed language like Java, eval will only confuse maintenance programmers. – Josiah Jul 23 '09 at 00:19
  • Why do you want to do this. I can think of no real situation in which this would be useful. I guess you have an XY problem. – Raedwald Dec 10 '14 at 13:03

7 Answers7

3

I would use a scripting language like beanshell, jruby, jython, etc.

John Doe
  • 859
  • 5
  • 10
3

You'd have to use something like Janino.

Vinay Sajip
  • 84,585
  • 13
  • 155
  • 165
3

Here's a possible way to get most of the way there via using javax.tools. Note that this code is rather long and not exactly the most efficient or portable way to do this, but you should get the idea.

import javax.tools.*;
import java.util.*;
import java.io.*;
import java.lang.reflect.*;

public class Test {
  public static void main(String[] args) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavaFileObject fileObj = 
      new StringJavaFileObject("public class InterpTest { public static void test() { System.out.println(\"Hello World\"); } }");
    List<JavaFileObject> tasks = new ArrayList<JavaFileObject>();
    tasks.add(fileObj);

    JavaFileManager defFileMgr = compiler.getStandardFileManager(null, null, null);
    MemoryJavaFileManager fileMgr = new MemoryJavaFileManager(defFileMgr);
    compiler.getTask(null, fileMgr, null, null, null, tasks).call();

    ClassLoader loader = new ByteArrayClassLoader();
    Class clazz = loader.loadClass("InterpTest");
    Method method = clazz.getMethod("test");
    method.invoke(null);
  }

  public static class StringJavaFileObject extends SimpleJavaFileObject {
    protected String str;

    public StringJavaFileObject(String str) {
      super(java.net.URI.create("file:///InterpTest.java"), JavaFileObject.Kind.SOURCE);
      this.str = str;
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncErrors) {
      return str;
    }
  }

  public static class MemoryJavaFileObject extends SimpleJavaFileObject {
    public static ByteArrayOutputStream out = new ByteArrayOutputStream();

    public MemoryJavaFileObject(String uri, JavaFileObject.Kind kind) {
      super(java.net.URI.create(uri), kind);
    }

    @Override
    public OutputStream openOutputStream() {
      return out;
    }
  }

  public static class ByteArrayClassLoader extends ClassLoader {
    public Class findClass(String name) {
      byte[] bytes = MemoryJavaFileObject.out.toByteArray();
      return super.defineClass(name, bytes, 0, bytes.length);
    }
  }

  public static class MemoryJavaFileManager implements JavaFileManager {
    protected JavaFileManager parent;

    public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
      return new MemoryJavaFileObject("file:///InterpTest.class", kind);
    }

    public MemoryJavaFileManager(JavaFileManager parent) { this.parent = parent; }
    public void close() throws IOException { parent.close(); }
    public void flush() throws IOException { parent.flush(); }
    public ClassLoader getClassLoader(JavaFileManager.Location location) { return parent.getClassLoader(location); }
    public FileObject getFileForInput(JavaFileManager.Location location, String packageName, String relName) throws IOException { return parent.getFileForInput(location, packageName, relName); }
    public FileObject getFileForOutput(JavaFileManager.Location location, String packageName, String relName, FileObject sibling) throws IOException { return parent.getFileForOutput(location, packageName, relName, sibling); }
    public JavaFileObject getJavaFileForInput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind) throws IOException { return parent.getJavaFileForInput(location, className, kind); }
    public boolean handleOption(String current, Iterator<String> remaining) { return parent.handleOption(current, remaining); }
    public boolean hasLocation(JavaFileManager.Location location) { return parent.hasLocation(location); }
    public String inferBinaryName(JavaFileManager.Location location, JavaFileObject file) { return parent.inferBinaryName(location, file); }
    public boolean isSameFile(FileObject a, FileObject b) { return parent.isSameFile(a, b); }
    public Iterable<JavaFileObject> list(JavaFileManager.Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException { return parent.list(location, packageName, kinds, recurse); }
    public int isSupportedOption(String option) { return parent.isSupportedOption(option); }
  }
}
toluju
  • 4,007
  • 2
  • 21
  • 27
2

This kind of thing is possible, but it would be horrendously expensive for a task as simple as this. In this example, I'd consider using Class.forName() to map "Integer" to a class, and Java reflection invoke the Constructor.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
1

The Integer class takes a String in its constructor to set the value, assuming the provided string contains only numeric text.

Integer foo;

public void setFoo(String str) {
   if(isInt(str)) {
     foo = new Integer(str.trim());
   }
}

// Returns a boolean based on if the provided string contains only numbers
private boolean isInt(String str) {
  boolean isInt = true;

  try {
    Integer.parseInt(str.trim());
  } catch (NumberFormatException nfe) {
    isInt = false;
  }

  return isInt;
}

// Get the value as an int rather than Integer
public int getIntValue(String str) {
  return Integer.parseInt(str.trim());
}
OMG Ponies
  • 300,587
  • 73
  • 490
  • 482
  • It was an example, I'm looking for a general solution – swampsjohn Jul 23 '09 at 00:44
  • Java is a strongly typed language, but most stuff can take String(s) for constructor parameters. Why you'd supply a string whose contents are Java language rather than use Java directly? – OMG Ponies Jul 23 '09 at 01:01
0

Java is a statically typed language, so I don't think you can do that.

Jack Leow
  • 20,663
  • 4
  • 48
  • 55
  • 1
    Well you can do it, but involves calling the Java compiler (or similar) at runtime, and then dynamically loading the resulting bytecode file. – Stephen C Jul 23 '09 at 02:08
0

You can use Java Scripting API. Default language is JavaScript, but you can plug in any language. it would require java 1.6 though.

Denis Tulskiy
  • 18,506
  • 6
  • 47
  • 63