开发者

How do I include the text of a file in my Java code?

开发者 https://www.devze.com 2022-12-18 13:51 出处:网络
It seems that Java is not set up to do what I have previously done in C++ (no big surprise there). I\'ve got a set of rules that are generated from开发者_如何学Python another application (a series of

It seems that Java is not set up to do what I have previously done in C++ (no big surprise there). I've got a set of rules that are generated from开发者_如何学Python another application (a series of if-then checks). These rules change from time to time, so in C++ I would do this:

double variableForRules=1;
bool condition=false;
#include "rules.out";
if(condition) //do something

Essentially the if-then checks in rules.out would use the "variableForRules" (and several other variables) to decide whether condition should be set to true. If it gets set to true after the eval of the rules, the program does something.

Is there a similar way to do this in Java? Or is my only option to have rules.out actually be an entire class that needs to be instantiated, etc.?

Thanks!


Since you're autogenerating that rules.out, you could autogenerate your Java function as well. Hopefully it's not too painful to add that functionality.


Since there is no preprocessor in Java, you can't do this. Like you said, you have to implement your logic inside a class.


Maybe you could use scripting for that. You could take a look at the Java Scripting Programmer's guide.


In Java, it would be common for the other application to save the rules into an .xml or .properties file, and then have Java read in that file.


rules.out actually has to be an entire class that needs to be instantiated for code to be executed.

Since rules.out is generated by third-party application, best thing would be to write your own CppToJavaTransformer that reads file rules.out as input and generates Rules.java. This assumes rules.out is available before compile time and Rules.java will be used at compile time. Drawback of this is that there is an extra transformation required.

Alternately you can write code that interprets rules.out and execute required instructions using introspection. This is hard way but rules.out can be changed at runtime as well.


Even if could include the rules file it would be of no help. Your rule are dynamic as you said. Looks like you java code needs to change for a different scenario.


You could try using reflection (See: Creating New Objects and Invoking Methods by Name)

First generate a Rules.java in the manner in which you currently build rules.out and compile it.

Then load the class file into your app at runtime in the manner in which JDBC drivers were traditionally loaded.

Class clazz = Class.forName("com.mydomain.Rules");

If your app runs for long periods of time (longer then the lifetime of a single Rules.class file) then you would have to create your own ClassLoader in order to swap out the underlying class during a single runtime.

0

精彩评论

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