-2

I don't want to recompile the code each time when lambda expression changes, is there any way i can pass lambda expression from external file(text/xml)

For ex : Old Lambda expression

(Car car) -> "red".equals(car.getColor())

New Updated Lambda expression

(Car car) -> "red".equals(car.getColor() && "100".equals(car.getPower())

Is there run-time compilation of Lambda expression supported by Java ?

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
sakumar
  • 489
  • 5
  • 11
  • 2
    What do you mean by "when it changes"? Changes how? – Vince Oct 25 '15 at 07:39
  • 1
    Normally this comment opens with "Welcome to Stack Overflow!" but you've been on the site more than three years, asked five previous questions, and posted two answers. Anyway: Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Oct 25 '15 at 07:41
  • It is possible to compile a class (containing a lambda or whatever) while running a Java program, then load and execute it. This, however, is a rather extreme procedure. It is quite possible that your actual problem can be solved using less circumstantial methods. – laune Oct 25 '15 at 07:51

2 Answers2

2

In Java, every lambda expression is part of a class. Therefore, the only way a lambda expression can change is if the enclosing class changes.

If the source code of a class changes (to implement the change in a lambda expression for example) then you need to recompile it. You can recompile Java classes at runtime; see Compiling external .java files from within Java. You then have to load the recompiled class ...

Having said that, it is rarely a good idea to generate / compile / load classes within a running application. It is complicated, and the chances are that it won't "pay off" as an optimization.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Short answer: No. Lambda expressions are code, and must be compiled, so you're not going to be able to just configure them. This is not like regular expressions.

Longer answer: First, see if you can express the changes you anticipate as configuration, then configure just these variables. This is only possible if you are able to anticipate the changes, and these are data-driven rather than structural.

Second, you can use a strategy pattern with a factory to allow different implementations to be substituted at runtime. This would allow you to drop in new implementations without rebuilding the full code base:

public class ConfigurableFactory {
    private final String strategyClassImpl;

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        System.out.println(new ConfigurableFactory().getStrategyImpl().doThing());
    }

    public ConfigurableFactory(){
        // TODO: Load from config
        strategyClassImpl ="StrategyImpl";
    }

    public Strategy getStrategyImpl() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
        return (Strategy) Class.forName(strategyClassImpl).newInstance();
    }
}

interface Strategy {
    String doThing();
}

class StrategyImpl implements Strategy {
    public String doThing() {
        return "Strategy 1";
    }
}

This isn't specific to lambda expressions, and in real development I've always found it more acceptable to just change the code and rebuild than any kind of hot-deploying trickery.

hugh
  • 2,237
  • 1
  • 12
  • 25