문자열에서 객체를 생성하고 싶습니다. 생성 된 객체가 IObject를 확장하는 IObjectImpl 유형이되도록하고 싶습니다.Intellij에서 이상한 오류가 발생했습니다. Java Generics issue
그래서 문자열과 인터페이스 클래스 (IObject를 확장하는 IObjectImpl.class는 필수 항목 임)를 사용하는 팩토리 클래스 메서드가 있습니다. 이 메서드는 문자열에서 생성 된 객체 (리플렉션 사용) 유형이 IObject인지 자동으로 감지하여 IObjectImpl로 캐스팅합니다.
나는 테스트를 위해 다음 코드를 작성했다. 그러나 Intellij는 오류를 표시하지 않고 동시에 main 메서드를 실행하는 동안 끝에 오류가 표시됩니다.
public <T extends IObject, E extends T> E getInstanceOfType(String clazz, Class type) {
try {
System.out.println("Type got is " + type);
return null;
} catch (Exception exception) {
throw new ObjectInstantiationException(String.format("Could not create the "
+ "instance of type %s", clazz), exception);
}
}
public static void main(String[] args) {
new Factory().getInstanceOfType("Some class", IObjectImpl.class);
}
오류 :
Error:(67, 49) java: ..path\Factory.java:67: incompatible types; inferred type argument(s) com.myCompany.IObject,java.lang.Object do not conform to bounds of type variable(s) T,E
found : <T,E>E
required: java.lang.Object
는 지금까지 유형을 확인하는 등, 난 그냥 eClass.isAssignableFrom (인 TClass) 접근 방식에 대해 알고.
마지막 목표는 캐스팅없이 IObjectImpl
에 정의 된 메서드를 호출 할 수 있어야한다는 것입니다. Java 1.6을 사용하여 어떻게 할 수 있습니까?
어쩌면이 일부 도움이 될 수 있습니다 http://stackoverflow.com/questions/21893223/java-generic-issue -incompatible-types-inferred-type-arguments-java-lang-obj – Stultuske
아마도'Class'에 대한 제네릭 바인딩을 잊어 버렸을까요? 그리고 아마도 원시 타입에 대한 경고를 받고 있습니까? – chrylis