在实现泛型类时不能推断类型变量

时间:2015-03-27 13:08:59

标签: java generics

我定义了以下类:

public class AttributeVisitor extends SimpleAnnotationValueVisitor8<Object, Object> {

    public AttributeVisitor() {
        super();
    }

    public AttributeVisitor(Object defaultValue) {
        super(defaultValue);
    }

    @Override
    public Object visitEnumConstant(VariableElement c, Object attribute) {
        return super.visitEnumConstant(c, attribute);
    }
}

我正试图在下面的剪辑中使用它

for(Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> mirrorValues: annotationMirror.getElementValues().entrySet()){
    mirrorValues.getValue().accept(new AttributeVisitor());
}

编译时,我收到以下错误:

method accept in interface javax.lang.model.element.AnnotationValue cannot be applied to given types;
[ERROR] required: javax.lang.model.element.AnnotationValueVisitor<R,P>,P
[ERROR] found: com.cvent.analytics.validator.AttributeVisitor
[ERROR] reason: cannot infer type-variable(s) R,P
[ERROR] (actual and formal argument lists differ in length)

任何想法,这里有什么不对?

2 个答案:

答案 0 :(得分:0)

The accept method expects two arguments。这编译:

mirrorValues.getValue().accept(new AttributeVisitor(), null);

答案 1 :(得分:0)

错误:

cannot infer type arguments for `Resource<>`

原因:

cannot infer type-variable(s) T
    (argument mismatch; String cannot be converted to ResponseMessage)
  where T is a type-variable:
    T extends Object declared in class Resource

解决方案:

new ResponseMessage("Message", null, null)) by replacing string message