У меня есть lib.jar, который содержит следующие классы:
Lib:
package lib;
public class Lib {
public <T extends Test<T>> void test(T test) {
}
}
Тест:
package lib;
public class Test<SELF extends Test<SELF>> {
}
В моем проекте Maven я добавилlib.jar как зависимость системной области от пути.Проект состоит из следующих классов:
TestSub:
package app;
import lib.Test;
public class TestSub extends Test<TestSub> {
}
TestSubSub:
package app;
public class TestSubSub extends TestSub {
}
И приложение:
package app;
import lib.Lib;
public class App {
public static void main(String[] args) {
Lib lib = new Lib();
lib.test(new TestSub()); // <-- This compiles
lib.test(new TestSubSub()); // <-- This does not compile
lib.test((TestSub) new TestSubSub()); // <-- This compiles
}
}
Как выв комментариях класса App можно видеть только компиляции lib.test(new TestSub());
и lib.test((TestSub) new TestSubSub());
, но не lib.test(new TestSubSub());
.Когда я не использую maven и не добавляю lib.jar в качестве External Jar в проект Eclipse Java, с этим кодом проблем нет.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project app: Compilation failure
[ERROR] <path to project>/src/main/java/app/App.java:[9,20] method test in class lib.Lib cannot be applied to given types;
[ERROR] required: T
[ERROR] found: app.TestSubSub
[ERROR] reason: inferred type does not conform to upper bound(s)
[ERROR] inferred: app.TestSubSub
[ERROR] upper bound(s): lib.Test<app.TestSubSub>
Что здесь не так?