如何在 Java 中从字符串创建对象(如何评估字符串)?

发布于 2024-07-29 01:37:36 字数 386 浏览 3 评论 0原文

我知道 eval 是“邪恶的”,但我使用它的方式是用户永远不能滥用它。

假设我有一个字符串“new Integer(5)”。 我想做一些事情,这样我就可以将一个变量(比如 foo)设置为 new Integer(5)。 就像

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

我环顾四周,看起来我有几个选择。 ToolProvider 中的 getSystemJavaCompiler() 方法可以做到这一点吗? 或者我应该使用 BeanShell 吗? 或者还有别的什么吗? 请注意,这是来自字符串,而不是文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

岁月如刀 2024-08-05 01:37:36

这是通过使用 javax.tools 实现大部分目标的可能方法。 请注意,这段代码相当长,并且不完全是最有效或可移植的方法,但您应该明白这一点。

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); }
  }
}

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); }
  }
}
我们只是彼此的过ke 2024-08-05 01:37:36

我会使用像 beanshell、jruby、jython 等脚本语言。

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

落日海湾 2024-08-05 01:37:36

您必须使用类似 Janino 的东西。

You'd have to use something like Janino.

云胡 2024-08-05 01:37:36

这种事情是可能的,但对于像这样简单的任务来说,成本会非常高。 在此示例中,我会考虑使用 Class.forName() 将“Integer”映射到类,并且 Java 反射调用构造函数。

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.

梦屿孤独相伴 2024-08-05 01:37:36

Integer 类 采用 String在其构造函数中设置值,假设提供的字符串仅包含数字文本。

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());
}

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());
}
迷爱 2024-08-05 01:37:36

Java 是一种静态类型语言,所以我认为你不能做到这一点。

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

原谅我要高飞 2024-08-05 01:37:36

您可以使用 Java 脚本 API 。 默认语言是 JavaScript,但您可以插入任何语言。 但它需要 java 1.6。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文