mercredi 30 novembre 2022

How to call a function that will create an object of a class if required?

Context:

I've found a bug in a selenium framework I'm working on, in which the Web Browser (at least Chrome) crashes unexpectedly with no notification.
As a patch, I'm reinitializing the WebDriver so it keeps working, but right now I'm creating a new EdgeDriver and I want to create a new WebDriver of the same type it was before (the one that crashed).

I came up with this approach:

driver = Map.of(
            ChromeDriver.class, getFunction(ChromeDriver.class),
            EdgeDriver.class, getFunction(EdgeDriver.class),
            FirefoxDriver.class, getFunction(FirefoxDriver.class),
            OperaDriver.class, getFunction(OperaDriver.class)
      ).entrySet().stream().filter((e) -> e.getKey().isInstance(driver))
      .map((e)->e.getValue().identity()).findFirst().orElseThrow(() -> new RuntimeException("WebDriver not detected"));

...

@SneakyThrows
static Function<Class<? extends RemoteWebDriver>, RemoteWebDriver> getFunction(Class<? extends RemoteWebDriver> driverClass){
   return c -> {
            try {
               return c.getConstructor().newInstance();
            } catch (IllegalAccessException | InstantiationException e) {
               throw new RuntimeException(e);
            }
         };
}

The problem is that I can't use this type of call

e.getValue().identity()

Do you know how can I achieve this?

I'm using the Map approach so I don't have to specify a bunch of if's with all the code inside.
And I'm using a separate method returning a Function so I don't have to spend resources in creating the instances before (and if they) are required.

I have faced this problem before and I'm pretty sure I'm going to keep facing it, I have almost no idea what I'm writing in the functional programming section because I'm very new to it. I hope I'm on the right track but honestly, I doubt it.

As an extra tip if you can provide a way to become an expert in functional programming in Java will be great because I've been looking for a course or any resources deep enough and I have not been able to find anything so far. Not only will solve this problem but it will help me to solve any future problems like this.

Aucun commentaire:

Enregistrer un commentaire