java - How to read a method from file -
in java, able load method file. way user has way customize method. user make txt file, type normal java method it, load program. know of reflection, not aware of way read methods this. program execute method built in. there anyway this?
i user can customize calculculations create values displaying stuff on screen. here existing method:
void setscale() { int windowdiagonal = (int) math.sqrt(comp.getwidth() * comp.getwidth() + comp.getheight() * comp.getheight()); double scale = windowdiagonal / 2500.0; placewidth = (int) (comp.getwidth()/2.5);//(int) (800 * scale); arrowlinesize = (int) (20*scale); linesize = (int) (10*scale); arrowsize = (int) (40*scale); arrowheight = (int) (100 * scale); arrownamegap = (int) (10 * scale); roundracegap = (int) (50 * scale); roundracelinegap = (int) (30 * scale); leftlightgap = (int) (20 * scale); lightsize = (int) (200 * scale); lighttextgap = (int) (20 * scale); lighttogapratio = 5;// (int) (5 * scale);// todo maybe??? linewinnergap = (int) (10 * scale); nametimegap = (int) (10 * scale); //bottom linenextupgap = (int) (10 * scale); nextupnextuptitlegap = (int) (10 * scale); nextuptitlenextupgap = (int) (10 * scale); nextupafternextupgap = (int) (10 * scale); afternextupbottomgap = (int) (100 * scale); titleroundracegap = (int) (20 * scale); toptitlegap = (int) (20 * scale); winnerarrowgap = (int) (40 * scale); comp.setfont(new font("arial", font.bold, (int) (60 * scale))); //$non-nls-1$ }
the problem is, if user wants make placewidth, half of screen. if want half of height divided width? since advanced feature of program, don't want go around making text fields , radio buttons different things do. instead 1 change code calculates values have more freedom.
you @ java runtime compiler read file , wrap code class , interface method(s) want call. once have compiled library, can run using reflection other class.
e.g.
public class generatedclass$12345 implements myinterfaceformethod { /* paste contents of method file here */ }
once compile it, have class implements myinterfaceformethod
can create instance , call method via interface (you might avoid having use reflection)
here example unit test
cachedcompiler cc = compilerutils.cached_compiler; string text = "generated test " + new date(); class compiled = cc.loadfromjava(eg_foo_bar_tee, "package eg;\n" + '\n' + "import eg.components.barimpl;\n" + "import eg.components.teeimpl;\n" + "import eg.components.foo;\n" + '\n' + "public class foobartee{\n" + // more source code deleted.
Comments
Post a Comment