변경할 수없는 라이브러리에 정의 된 클래스를 확장하고 있습니다.raw 형의 파라미터를 가지는 메소드를 오버라이드 (override) 할 때, 체크되지 않는 경고를 피할 수 있습니까?
public class Parent
{
public void init(Map properties) { ... }
}
부모를 확장하는 'Child'클래스를 정의하고 있으며 Java 6을 제네릭과 함께 사용하는 경우 재정의하는 가장 좋은 방법은 무엇입니까? 확인되지 않은 경고를받지 않고 init 메소드를 사용합니까?
public class Child extends Parent
{
// warning: Map is a raw type. References to generic type Map<K,V> should be parameterized
public void init(Map properties) { }
}
일반 매개 변수를 추가하면 다음과 같이 나타납니다.
// error: The method init(Map<Object,Object>) of type Child has the same erasure as init(Map) of type Parent but does not override it
public void init(Map<Object,Object>) { ... }
// same error
public void init(Map<? extends Object,? extends Object>) { ... }
// same error
public void init(Map<?,?>) { ... }
이 오류는 특정 형식, 제한된 와일드 카드 또는 제한되지 않은 와일드 카드를 사용하는지 여부에 관계없이 발생합니다. @SuppressWarnings ("unchecked")를 사용하지 않고 경고없이 비 제네릭 메서드를 재정의 할 수있는 정확하고 관용적 인 방법이 있습니까?
@SuppressWarnings가 가장 좋은 방법입니다. 하지만 제네릭을 사용하지 않는 라이브러리에 제네릭을 강제로 넣으려고 할 때 일어나는 일입니다. –
'@SuppressWarnings ("rawtypes")'도 충분해야합니다. –