2010-12-29 3 views
1

리플렉션을 포함하여 까다로운 일반 유형 문제가 있습니다. 여기에 코드가 있습니다.Java 일반 유형 및 리플렉션

public @interface MyConstraint { 
    Class<? extends MyConstraintValidator<?>> validatedBy(); 
} 

public interface MyConstraintValidator<T extends Annotation> { 
    void initialize(T annotation); 
} 

/** 
    @param annotation is annotated with MyConstraint. 
*/ 
public void run(Annotation annotation) { 
    Class<? extends MyConstraintValidator<? extends Annotation>> validatorClass = annotation.annotationType().getAnnotation(MyConstraint.class).validatedBy(); 
    validatorClass.newInstance().initialize(annotation) // will not compile! 
} 

위의 run() 메서드는 다음 오류 때문에 컴파일되지 않습니다.

The method initialize(capture#10-of ? extends Annotation) in the type MyConstraintValidator<capture#10-of ? extends Annotation> is not applicable for the arguments (Annotation) 

와일드 카드를 제거하면 컴파일되고 정상적으로 작동합니다. vairable의 유형 매개 변수를 선언하는 속성 방법은 무엇입니까 validatorClass?

감사합니다.

답변

2

? extends Annotation은 "Annotation의 모든 하위 유형"과 다른 "Annotation의 하위 유형"을 의미합니다.

메서드의 초기화에 "알 수없는 하위 유형의 주석이 필요합니다."어떤 점에서 알려지지 않은 하위 형식이 이제 AnotherAnnotation으로 알려져 있으며 AnotherAnnotation 유형이 아닐 수있는 Annotation 클래스의 개체를 전달하려고합니다. 그들은 양립 할 수 없다.

비슷한 질문은 대답 here입니다.