如何在泛型类上调用通配方法?

时间:2014-11-08 11:03:19

标签: java generics

我有一个泛型类,其中type变量用于方法的参数。当我使用有界通配符从SetMapList检索泛型类的实例时,如何调用此方法?

泛型类的超级:

//Verifier.java
import java.lang.annotation.Annotation;

public interface Verifier<A extends Annotation> {
    // A verifier needs the annotation to extract verification information.
    public boolean verifyValue(Object value, A annotation);
}

具体实施:

//MinMaxVerifyer.java
public class MinMaxVerifyer implements Verifier<MinMax> {
    @Override
    public boolean verifyValue(Object value, MinMax annotation) {
        if (!(value instanceof Number)) return false;

        long l = ((Number)value).longValue();
        long min = annotation.min();
        long max = annotation.max();

        return l >= min && l <= max;
    }
}

示例中使用的注释:

//MinMax.java
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.METHOD)
@Inherited
public @interface MinMax {
    long min() default Long.MIN_VALUE;
    long max() default Long.MAX_VALUE;
}

包含违规通话的课程:

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;

public class ParameterVerifier {
    private static final Map<String, Verifier<? extends Annotation>> verifyers
        = new HashMap<>();

    static {
        verifyers.put("MinMax", new MinMaxVerifyer());
        // verifyers.put(Range.class, new RangeVerifier());
        // verifyers.put(NotNull.class, new NotNullVerifier());
    }

    public void validate(Method method, Object value) {
        for (Annotation annotation : method.getAnnotations()) {
            String name = annotation.getClass().getSimpleName();
            Verifier<? extends Annotation> verifier = verifyers.get(name);

            if (verifier != null) {
                boolean valid = verifier.verifyValue(value, annotation);
                // ^ The method
                // verifyValue(Object, capture#5-of ? extends Annotation)
                // in the type Verifier<capture#5-of ? extends Annotation>
                // is not  applicable for the arguments (Object, Annotation)
            }
        }
    }
}

我正在努力让这个工作,以便用户可以添加他们自己的Annotation / Verifier对,并且Verifier应该使用Annotation子类进行参数化。我不在乎我是否需要在ParameterVerifier中进行一些转换(不在验证器中,这是公共API)。我知道如何使用一些额外的逻辑添加类型安全对,为清楚起见,这里省略了。

相关:The method validate(capture#2-of ? extends Object) in the type Validator<capture#2-of ? extends Object> is not applicable for the arguments (String)Java generic/wildcard type mismatch都建议限制特定的Verifier子类型或从Verifier中删除类型变量。我正在寻找更优雅的解决方案。 (考虑MinMaxVerifyer.java不可变,仅适用于其他人。)

1 个答案:

答案 0 :(得分:0)

我不知道为什么我以前没有发现这个。我以为我会尝试各种仿制药的组合。这可以很好地编译和完成工作。

  • 从检索到的元素中删除通配符(但不是通用的),然后转换并告诉编译器它是安全的。
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;

public class ParameterVerifier {

    private static final Map<String, Verifier<? extends Annotation>> verifiers
        = new HashMap<>();

    static {
        verifiers.put(Range.class.getSimpleName(), new RangeVerifyer());
    }

    public void validate(Method method, Object value) {
        for (Annotation annotation : method.getAnnotations()) {
            String name = annotation.getClass().getSimpleName();
            @SuppressWarnings("unchecked")
            Verifier<Annotation> verifier =
                (Verifier<Annotation>)verifiers.get(name);
            if (verifier != null) {
                boolean valid = verifier.verify(value, annotation);
            }
        }
    }
}