How is an Instance linked to Formal Parameter (or inferred) in Anonymous Class Method in Java?

Viewed 53

I am trying to understand how parameter driver (from method apply in anonymous class ExpectedCondition) gets a WebDriver instance.

package org.openqa.selenium.support.ui;
public class ExpectedConditions {
    public static ExpectedCondition<Boolean> javaScriptThrowsNoExceptions(final String javaScript) {
        return new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                try {
                    ((JavascriptExecutor) driver).executeScript(javaScript);
                    return true;
                } catch (WebDriverException e) {
                    return false;
                }
            }

            @Override
            public String toString() {
                return String.format("js %s to be executable", javaScript);
            }
        };
    }
}

package org.openqa.selenium.support.ui;
public interface ExpectedCondition<T> extends Function<WebDriver, T> {}

So far, I acknowledge that (as shown on the code below) I explicitly instantiate WebDriver which is used by WebDriverWait which ultimately invokes the anonymous class' method that, assuming, will use the WebDriver instance that was explicitly created.

WebDriver driver = new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.javaScriptThrowsNoExceptions("javascript:void(0);"));

I understand that an anonymous class has access to the members of its enclosing scope but I don´t see how that can be applied here.

I also know that, at least when it's about generics, Java performs some inference mechanisms to perform type checking, to decide on what method to execute, etc., so I don´t know if I am missing something important in the big picture or maybe there´s something going on behind the scenes.

0 Answers
Related