Scala类final def this()声明没有公共无参数构造函数

时间:2016-06-04 21:58:18

标签: java scala generics polymorphism apache-kafka

尝试构建一个使用Jackson Smile 2.6.6和jackson-module-scala的通用Scala 2.11 Jackson序列化程序。无论出于何种原因,Java都找不到公共的无参数构造函数。虽然下面的类编译,但我在运行时从Kafka客户端收到以下错误:

 df <- data.frame(a = c(0.123, 123.456), b = c(321.321, 0.321))
 df <- as.matrix(df)
 df1 <- ifelse(row(df)==col(df),round(df,2),floor(df))
 df1
 #      [,1]   [,2]
 # [1,]   0.12 321.00
 # [2,] 123.00   0.32
 df1 <- as.data.frame(df1)

这是课程,我该如何解决?我尝试了使用 df1 <- ifelse(row(df)==col(df),round(df,2),round(df)) df1 # [,1] [,2] # [1,] 0.123 321.000 # [2,] 123.000 0.321 df1 <- as.data.frame(df1) 和将私有var移动到类中的各种组合,虽然我可能做错了,但也尝试使用无参数Caused by: org.apache.kafka.common.KafkaException: Could not instantiate class ai.bernie.api.util.serialization.kafka.JacksonGenericSerializer Does it have a public no-argument constructor? at org.apache.kafka.common.utils.Utils.newInstance(Utils.java:318) ~[kafka-clients-0.10.0.0.jar:na] at org.apache.kafka.common.config.AbstractConfig.getConfiguredInstance(AbstractConfig.java:201) ~[kafka-clients-0.10.0.0.jar:na] at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:671) ~[kafka-clients-0.10.0.0.jar:na] ... 27 common frames omitted Caused by: java.lang.InstantiationException: ai.bernie.api.util.serialization.kafka.JacksonGenericSerializer at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_51] at org.apache.kafka.common.utils.Utils.newInstance(Utils.java:314) ~[kafka-clients-0.10.0.0.jar:na] ... 29 common frames omitted Caused by: java.lang.NoSuchMethodException: ai.bernie.api.util.serialization.kafka.JacksonGenericSerializer.<init>() at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_51] at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_51] ... 30 common frames omitted 方法创建伴随对象。 / p>

final def

修改

我还应该提到我正在使用Google Guice,虽然我怀疑它会影响这个,因为序列化类本身没有被注入,只有使用它的Kafka Consumer / Producer父类。

1 个答案:

答案 0 :(得分:2)

因为JacksonGenericSerializer类型参数具有上下文绑定(即[T: Manifest]),辅助“无参数”构造函数实际上采用了参数:Manifest[T]

您可以使用javap确认:

$ javap JacksonGenericSerializer.class

Compiled from "JacksonGenericSerializer.scala"
public class JacksonGenericSerializer<T> implements java.io.Closeable, java.lang.AutoCloseable, org.apache.kafka.common.serialization.Serializer<T>, org.apache.kafka.common.serialization.Deserializer<T> {
  public void configure(java.util.Map<java.lang.String, ?>, boolean);
  public byte[] serialize(java.lang.String, T);
  public T deserialize(java.lang.String, byte[]);
  public void close();
  public JacksonGenericSerializer(scala.reflect.Manifest<T>);
}

此问题涉及类似问题:How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor?