开发者

Turning text into executable statements

This is a question out of开发者_运维技巧 curiousity for java or c++, I wanted to ask if it is possible to turn any text input into some executable statements?

For example say I have a text file with info like:

"class: Abc, Param: 32"

Now say in C++ or Java I want to read that file and do something like:

new Abc(32);

How would I do that? Its easy enough to read the value Abc but how do say create a class Abc? Is there a standard way to do that? in both C++ and Java?

Main curiosity came from those persistance mechanisms in Java that store object properties in XML file and create an object by reading that XML file, how do they do that? Is that seperate from what I am asking for above? EDIT: This is different from the standard java serialization, i've seen this as solutions for long term persistence where object implementation can change and instead of serializing they store properties including execution statements in XML files which are used to create an object at runtime.


If your goal is to take any text input and run arbitrary commands, then the places to start looking on the JVM are the Java 6 compiler API (http://java.sun.com/javase/6/docs/api/javax/tools/package-summary.html) or JSR 223 (http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting).

If your goal is storage and retrieval of information from text file, look at protocol buffers (http://code.google.com/p/protobuf/) or the Java serialization API (http://www.javaworld.com/javaworld/jw-07-2000/jw-0714-flatten.html).


You first need to parse your mini-language. So (as mentioned) Antlr, or Javacc to create a parser for your language.

After that, you need to use the meta-programming features of your 'interpreter'/target language. For Java, you want to start with reflection, and if you absolutely need it, later move on to byte code generation.


[C++ answer]

You can't esaily do that. You'd need to parse/compile the C++ lines at run time, which is not something you want to deal with, trust me.

But if you just came with that from "those persistance mechanisms in Java that store object properties in XML file", then the keyword you're looking for is Serialization. Many serialization techniques and libraries are available for C++, such as boost::serialization.


In java you can do it like this:

// Read data from file:
String cls = "java.math.BigDecimal";
String arg = "123.45";

// Create the object
Class<?> objClass = Class.forName(cls);
Object obj = objClass.getConstructor(new Class[] {String.class }).newInstance(arg);
System.out.println("The object is: " + obj.toString() + "; type: " 
    + obj.getClass().getSimpleName());


Java 6 supports scripting languages which can do "stuff" with a passed string like what you describe.

There is a JavaScript engine and a BeanShell engine available, but I am not sure if they can create new classes on the fly. So the thing you need to do is find a supported scripting language you like on

https://scripting.dev.java.net/

and install it, and use it :)


You parse the input according to a grammar (remember yacc?) and attach semantic actions to derivation rules. This is a very remote description of what compilers do.


For C++ a lot of what you propose could be done using the macro functionality of the pre-compiler. For Java, it wouldn't be hard to use reflection to implement the above example. Your example is pretty sparse though. Can you give a use case for why you'd be interested in doing this?

If you're just looking for a mechanism for setting up the initial state of your program, then what you want to look for is information on Inversion of Control. In Java, Spring is the big IoC library out there. I don't know what C++ equivalents there might be. I suspect C++'s lack of reflection functionality will make IoC much harder.

EDIT: I didn't really read your last paragraph before. Serialization to and from XML can be accomplished a number of different ways, but in Java if you just want to be able to serialize and deserialize the state of an object JAXB is a library that allows this.


Create a parser. For Java and C++ I advice you to use Antlr.

For classes unknown at parser generation time you may also need to use Reflection API.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜