-1

I have an interface as below

public interface AccessController {

}

I have a class as below

public class LocalDriver {
   WebDriver driver;

   public static WebDriver driver() {
       if ("need to know?" instanceof AccessController)
           return driver;
       else
           return null;
   }
}

Then I have a caller class

public class CallerClass {

    public static void main(String[] args) {
        LocalDriver.driver();
    }

}

Now how do I check if the CallerClass is aninstanceOf AccessController interface in the driver() method which is in a different class?

shmosel
  • 49,289
  • 6
  • 73
  • 138
Damien-Amen
  • 7,232
  • 12
  • 46
  • 75

1 Answers1

1

You could pass the caller into your static method and interrogate it. Something like:

public static WebDriver driver(Object caller) {
   if (caller instanceof AccessController)
    return driver;
   else 
    return null;
 }

You would use this in the following manner:

// In the caller
LocalDriver.driver(this);

If you also need to support this concept from a static context, you could try:

public static WebDriver driver(Class<?> callerClass) {
   if (caller.isAssignableFrom(AccessController.class))
    return driver;
   else 
    return null;
 }

This can be called from both contexts:

LocalDriver.driver(this.getClass());  // instance context
LocalDriver.driver(CallerClass.class);  // static context
dave
  • 11,641
  • 5
  • 47
  • 65