tags:

views:

226

answers:

8

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.

A: 

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.

Jherico
Yeah I am looking for serialization techniques where the class is not known but the name of the class is stored in the XML file. So its not just state of the class but also the name of the class, any application loading it may not be aware of what class to construct with what state until reading the XML file. This is where the curiosity comes from.
iQ
The general answer is Java is that you can find a class from its name using reflection. Libraries like Spring create a standaridzed way to turn an XML file into a set of created objects, all revolving around this mechamism.
Jherico
A: 

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.

Vladimir Dyuzhev
A: 

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.

Nikolai N Fetissov
+2  A: 

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.

yeah this is the sort of thing i'm after, run-time execution of text.
iQ
+4  A: 

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).

James Kingsbery
thank you they look interesting I will have a loook
iQ
How about getting some check mark love?
James Kingsbery
+2  A: 

[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.

Samuel_xL
+1  A: 

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());
True Soft
There was asked for "any text"...
Thorbjørn Ravn Andersen
This is something along the lines of what i'm looking although I haven't tested it. This maybe the solution to construction unknown objects with unknown parameters. Now i need how I could execute any text including methods, if methods could be covered then that is a big step.
iQ
A: 

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 :)

Thorbjørn Ravn Andersen
Downvoters, please comment on why...
Thorbjørn Ravn Andersen
It seems none of them ever heard the word "parser"...
Vladimir Dyuzhev