使用反射调用Type参数化方法

时间:2015-12-29 03:47:34

标签: java reflection google-cloud-dataflow

谷歌云数据流sdk有一个类,可以为不同的Avro类型注册编码器。

CoderRegistry cr = p.getCoderRegistry();
cr.registerStandardCoders();
cr.registerCoder(Row.class, AvroCoder.of(Row.class));
cr.registerCoder(Destination.class, AvroCoder.of(Destination.class));
cr.registerCoder(Device.class, AvroCoder.of(Device.class));
cr.registerCoder(Location.class, AvroCoder.of(Location.class));
cr.registerCoder(Source.class, AvroCoder.of(Source.class));
cr.registerCoder(DimensionalMetric.class, AvroCoder.of(DimensionalMetric.class));
cr.registerCoder(DimensionSet.class, AvroCoder.of(DimensionSet.class));
cr.registerCoder(MetricSet.class, AvroCoder.of(MetricSet.class));

但是你可以想象这变得非常麻烦,我想使用java反射API自动注册包com.brightcove.rna.model中的所有类。我想这会是这样的:

void registerAllModels(Pipeline p) {
    CoderRegistry cr = p.getCoderRegistry();
    Reflections r = new Reflections("com.brightcove.rna.model");
    Set<Class<? extends IndexedRecord>> classes = r.getSubTypesOf(IndexedRecord.class);
    for (Class<? extends IndexedRecord> clazz : classes) {
        cr.registerCoder(clazz, AvroCoder.of(clazz));
    }
}

不幸的是,这不起作用。我在这里做错了什么?

2 个答案:

答案 0 :(得分:4)

您在以下行中遇到编译错误:

cr.registerCoder(clazz, AvroCoder.of(clazz));

错误是:

The method registerCoder(Class<?>, Class<?>) in the type CoderRegistry is not applicable for the arguments (Class<capture#1-of ? extends IndexedRecord>, AvroCoder<capture#2-of ? extends IndexedRecord>).

基本上,问题是Java编译器无法推断两个参数上的通配符类型是相同的,并且报告错误。这是一个常见的Java问题,例如,请参阅this问题。

修复如下,可能需要您禁止有关原始类型的编译器警告和未经检查的转换:

  cr.registerCoder(clazz, (Coder) AvroCoder.of(clazz.newInstance().getSchema()));

这解决了两个问题:

  • 确保使用registerCoder(Class<T>, Coder<T>)代替重载的registerCoder(Class<?>, Class<?>)
  • AvroCoder.of(Class<T>)使用Avro的ReflectData.get().getSchema()生成架构,该架构可能不适用于所有类型,例如GenericRecord。另一种方法是通过AvroCoder.of(Schema)构造编码器,并从自动生成的类中获取模式。

答案 1 :(得分:0)

事实证明,在我收到的错误消息上阅读更多内容有助于我找到问题的根源。

AnalyticsPipeline.java:110: error: no suitable method found for registerCoder(Class<CAP#1>,AvroCoder<CAP#2>)
            cr.registerCoder(clazz, AvroCoder.of(clazz));
              ^
    method CoderRegistry.registerCoder(Class<?>,Class<?>) is not applicable
      (argument mismatch; no instance(s) of type variable(s) T#1 exist so that AvroCoder<T#1> conforms to Class<?>)
    method CoderRegistry.registerCoder(Class<?>,CoderFactory) is not applicable
      (argument mismatch; no instance(s) of type variable(s) T#1 exist so that AvroCoder<T#1> conforms to CoderFactory)
    method CoderRegistry.<T#2>registerCoder(Class<T#2>,Coder<T#2>) is not applicable
      (inferred type does not conform to equality constraint(s)
        inferred: CAP#3
        equality constraints(s): CAP#3,CAP#1)
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>of(Class<T#1>)
    T#2 extends Object declared in method <T#2>registerCoder(Class<T#2>,Coder<T#2>)
  where CAP#1,CAP#2,CAP#3 are fresh type-variables:
    CAP#1 extends IndexedRecord from capture of ? extends IndexedRecord
    CAP#2 extends IndexedRecord from capture of ? extends IndexedRecord
    CAP#3 extends IndexedRecord from capture of ? extends IndexedRecord

如上所述,javac认为论证的类型不匹配(正确,因为我没有提供任何信息)。我的解决方案结果是一个相当简单的修改。通过添加一个修复类型的辅助函数。

void registerCoders(Pipeline p) {
        CoderRegistry cr = p.getCoderRegistry();
        Reflections r = new Reflections("com.brightcove.rna.model");
        Set<Class<? extends IndexedRecord>> classes = r.getSubTypesOf(IndexedRecord.class);
        for (Class<? extends IndexedRecord> clazz : classes) {
            registerAvroType(cr, clazz);
        }
    }

    <T extends IndexedRecord> void registerAvroType(CoderRegistry cr, Class<T> clazz) {
        cr.registerCoder(clazz, AvroCoder.of(clazz));
    }

我能够向编译器传达类型确实相同并且问题已得到解决。