1

I'm using Scala with a Java library that expects to be passed a class with a public static void main(java.lang.String[]) so it can run call it via reflection for integration tests.

object RestServer {
    def main(args: Array[String]): Unit = { /* run the server */ }
}

Due to the behavior described in this answer to another question, this gets compiled to two classes.

public final class com.example.RestServer$ {
  public static final com.example.RestServer$ MODULE$;
  public static {};
  public void main(java.lang.String[]);
}

and

public final class com.example.RestServer {
  public static void main(java.lang.String[]);
}

When I pass the class to the library

@IntegrationTest(main = classOf[RestServer.type])
class MyTests extends RapidoidIntegrationTest { }

I'm actually passing the object singleton instance (RestServer$), not the RestServer class that has the static void main() method.

This wouldn't be a problem, except the library verifies that the method it is calling is both public and static before calling it?

How can I get the RestServer class instead?

RubberDuck
  • 11,933
  • 4
  • 50
  • 95

1 Answers1

1

If you have control over the RestServer source file you can add an empty companion class.

object RestServer {
    def main(args: Array[String]): Unit = { /* run the server */ }
}

class RestServer

That way Scala will recognize that a class RestServer exists, so classOf[RestServer] will compile and give you the class that contains the static method.

Jasper-M
  • 14,966
  • 2
  • 26
  • 37