Disabling compile-time dependency checking when compiling Java classes

后端 未结 8 1186
一整个雨季
一整个雨季 2020-12-28 09:10

Consider the following two Java classes:

a.) class Test { void foo(Object foobar) { } }

b.) class Test { void foo(pkg.not.in.classpath.FooBar foobar) { } }
         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-28 09:30

    Java by design does compile-time depenency checking and uses it not only to determine types but to determine method calls when they are overloaded. I know of no way around that.

    What can be done (and is done for, say, JDBC drivers) is to delay dependency checking through the use of reflection. You can get the class from Class.forName without the compiler knowing the class at compile time. In general, however, this means that the code is written to an interface and at runtime a class is loaded that implements the interface.

提交回复
热议问题