-1

In Java, I would like to be able to load a method from a file. This way the user has a way to customize the method. The user could make a txt file, type a normal Java method into it, then load it into the program. I know of Reflection, but I am not aware of a way to read methods like this. The program would execute it just like it was a method built in. Is there anyway to do this?

I would like to do this so the user can customize the calculculations that create values for displaying stuff on the screen. Here is my 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, what if the user wants to make placeWidth, say half of the screen. What if they want it to be half of the height divided by the width? Since this would be an advanced feature of the program, I don't want to go around making text fields and radio buttons for all the different things it could do. Instead one could change the code that calculates the values so they have more freedom.

  • This would not be easy to do, and I wonder if your question is a possible [XY question](http://mywiki.wooledge.org/XyProblem). What is the purpose of this request? Perhaps there's a better way of solving your overall problem. – Hovercraft Full Of Eels Sep 23 '14 at 11:59
  • 1
    Have a look at Java Reflections! I added an answer with further information. – Carol.Kar Sep 23 '14 at 11:59

2 Answers2

2

You could look at Java Runtime Compiler You would read the file and wrap it with code for a class and an interface for the method(s) you want to call. Once you have compiled it with the library, you can run it using reflection like any other class.

e.g.

public class GeneratedClass$12345 implements MyInterfaceForMethod {
 /* paste the contents of the method from file here */
}

Once you compile it, you will have a class which implements MyInterfaceForMethod from which you can create an instance and call the method via the interface (you might even avoid having to use reflection)

Here is an example from a 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.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

You state:

The problem is, what if the user wants to make placeWidth, say half of the screen. What if they want it to be half of the height divided by the width? Since this would be an advanced feature of the program, I don't want to go around making text fields and radio buttons for all the different things it could do. Instead one could change the code that calculates the values so they have more freedom.

Again I wonder if yours is an XY Problem and if you're confusing data with code. Why go through all the complex hoops of trying to read code when you could more easily fix this by giving your program the ability to read this information in via some sort of initialization text file? Myself if I were to do this, I'd use XML and would translate the XML data into Java objects via JAXB, but it could just as easily be done with an .ini file or something similar. This would be much easier to implement than to try to create a program that compiles and meshes in text to code.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373