How to generate Java Code from XML at runtime?

L

Lucia

Hello, Experte,

I'm looking for a possibility to load the Java code dynamically into a
class at the run time. The Java Code should be readed from an
XML-file. There are just some "If-Then" expressions and should be
added into one class.

Does someone know if it is possible?

best regards

Lucia
 
L

Lucia

Hello, Dorothy,

thank you for your answer. I will try to explain my requirements. In
my java-application there are many classes to controll the machines.
These Classes contain a lot of "IF-THEN" statements.

For example,
if (material=True and machine=idel) then startmachine(parameter);

These conditions should always be changed for the new situations. That
means that I should frequently rewrite the code. Hence we had a new
idea. If we can programm a graphical user interface, where the user
can type the "If-Then-Statements" as a text. These
"IF-Then-Statements" can be stored in a special format (e.g. XML) and
can be dynamically loaded in these classes.

The critical step is, how I can load not the whole class, but just
several "IF-THEN-Statements" in the class at run time.

Now can you understand what I want to reach? Do you have a good idea?

Best regards

Lucia
 
C

Christophe Vanfleteren

Lucia said:
Hello, Dorothy,

thank you for your answer. I will try to explain my requirements. In
my java-application there are many classes to controll the machines.
These Classes contain a lot of "IF-THEN" statements.

For example,
if (material=True and machine=idel) then startmachine(parameter);

These conditions should always be changed for the new situations. That
means that I should frequently rewrite the code. Hence we had a new
idea. If we can programm a graphical user interface, where the user
can type the "If-Then-Statements" as a text. These
"IF-Then-Statements" can be stored in a special format (e.g. XML) and
can be dynamically loaded in these classes.

The critical step is, how I can load not the whole class, but just
several "IF-THEN-Statements" in the class at run time.

Now can you understand what I want to reach? Do you have a good idea?

Best regards

Lucia
You might use a scripting language like beanshell (www.beanshell.org)
instead (beanshell is just like java, but has some extentions to make it
easily scriptable).

Place the if / then statements in a script, an run that instead.
 
T

Thomas Weidenfeller

thank you for your answer. I will try to explain my requirements. In
my java-application there are many classes to controll the machines.
These Classes contain a lot of "IF-THEN" statements.

For example,
if (material=True and machine=idel) then startmachine(parameter);

I guess you can get a lot of ideas from looking at the theory and
practice of state machines (FSA, etc.).
The critical step is, how I can load not the whole class, but just
several "IF-THEN-Statements" in the class at run time.

One way to respresent states and state transitions are tables. It
should be trivial to store such tables in a text format like XML.

/Thomas
 
W

William Brogden

Lucia said:
Hello, Dorothy,

thank you for your answer. I will try to explain my requirements. In
my java-application there are many classes to controll the machines.
These Classes contain a lot of "IF-THEN" statements.

For example,
if (material=True and machine=idel) then startmachine(parameter);

These conditions should always be changed for the new situations. That
means that I should frequently rewrite the code. Hence we had a new
idea. If we can programm a graphical user interface, where the user
can type the "If-Then-Statements" as a text. These
"IF-Then-Statements" can be stored in a special format (e.g. XML) and
can be dynamically loaded in these classes.

That sounds alot like a "rules engine" - take a look at JESS -
http://herzberg.ca.sandia.gov/jess/

I just got a copy of the book about JESS written by the creator -
some of the examples sound like exactly what you are trying to do.

http://www.manning.com/friedman-hill/ <- the book page

WBB
 
S

Sudsy

Lucia said:
Hello, Dorothy,

thank you for your answer. I will try to explain my requirements. In
my java-application there are many classes to controll the machines.
These Classes contain a lot of "IF-THEN" statements.

For example,
if (material=True and machine=idel) then startmachine(parameter);

These conditions should always be changed for the new situations. That
means that I should frequently rewrite the code. Hence we had a new
idea. If we can programm a graphical user interface, where the user
can type the "If-Then-Statements" as a text. These
"IF-Then-Statements" can be stored in a special format (e.g. XML) and
can be dynamically loaded in these classes.

The critical step is, how I can load not the whole class, but just
several "IF-THEN-Statements" in the class at run time.

Sounds like a classic application for a finite state machine (FSM).
Straightforward to implement in Java. The code would be very small.
 
R

Roedy Green

The critical step is, how I can load not the whole class, but just
several "IF-THEN-Statements" in the class at run time.

There are several ways you can do it.

You generate Java source code, then compile it. The source code would
be for a class that overrides your main class. It may have just one
method in it that does the if-then logic, and whatever else minimally
must exist in the method to get it started.

The other option is to generate JVM byte code on the fly. See
http://mindprod.com/jgloss/jasm.html. This is more difficult, but then
you bypass the compile step. You may be forced to use this approach
where you cannot guarantee the presence of the latest and greatest
Javac.exe at run time.

See http://mindprod.com/jgloss/javacexe.html for how to invoke javac
without firing up a new JVM (much faster).

I would be happy to write the part that converts the XML either to
Java or byte code for a fee if you feel overwhelmed.

You might want to build these conditions visually with drag and drop.
The advantage there is it impossible to make syntax or other
grammatical errors.

You might want to invent a simplified language for describing them.
You then need a parser. see http://mindprod.com/jgloss/parser.html

The great advantage of generating java source code is you can always
create an escape in your specification language to allow tricky cases
to be specially handled with custom Java code.

You would find that raw XML would be even more confusing, and
certainly more bulky than the raw Java itself. XML would just be your
intermediate target.
 
Joined
Aug 19, 2011
Messages
1
Reaction score
0
Generate Java code from xml at runtime

Hi Lucia,

I am also planning to implement/modify the java files at run time, similar to the question which you have posted, your help in this regards would be greatly appreciated.

In brief I would like to add portion/part of java code parsing the XML file.

regards,
Vinay
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top