- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Scala 中创建了 Guava 的 ImmutableRangeMap
和 Builder
的 Serializable
版本,以便在我的 Spark 应用程序中使用。我的 SerializableImmutableRangeMap
中也有一个零参数构造函数,那么为什么我在运行我的 Spark 应用程序时会得到 InvalidClassException: no valid constructor
?
这是我的 SerializableImmutableRangeClass
对象和类:
object SerializableImmutableRangeMap extends Serializable {
final class SerializableBuilder[K <: Comparable[_], V]() extends Serializable {
val keyRanges: RangeSet[K] = TreeRangeSet.create()
val rangeMap: RangeMap[K, V] = TreeRangeMap.create()
def put(range: Range[K], value: V): SerializableBuilder[K, V] = {
checkNotNull(range)
checkNotNull(value)
checkArgument(!range.isEmpty(), "Range must not be empty, but was %s", range)
if (!keyRanges.complement().encloses(range)) {
// it's an error case; we can afford an expensive lookup
for (entry: Entry[Range[K], V] <- JavaConversions.asScalaSet(rangeMap.asMapOfRanges().entrySet())) {
val key: Range[K] = entry.getKey()
if (key.isConnected(range) && !key.intersection(range).isEmpty()) {
throw new IllegalArgumentException(
"Overlapping ranges: range " + range + " overlaps with entry " + entry)
}
}
}
keyRanges.add(range)
rangeMap.put(range, value)
this
}
def putAll(rangeMap: RangeMap[K, _ <: V]): SerializableBuilder[K, V] = {
for (entry <- JavaConversions.asScalaSet(rangeMap.asMapOfRanges().entrySet())) {
put(entry.getKey(), entry.getValue())
}
this
}
def build(): SerializableImmutableRangeMap[K, V] ={
val map: java.util.Map[Range[K], V] = rangeMap.asMapOfRanges()
val rangesBuilder: ImmutableList.Builder[Range[K]] = new ImmutableList.Builder[Range[K]](map.size())
val valuesBuilder: ImmutableList.Builder[V] = new ImmutableList.Builder[V](map.size())
for (entry: Entry[Range[K], V] <- JavaConversions.asScalaSet(map.entrySet())) {
rangesBuilder.add(entry.getKey())
valuesBuilder.add(entry.getValue())
}
return new SerializableImmutableRangeMap[K, V](rangesBuilder.build(), valuesBuilder.build())
}
}
def builder[K <: Comparable[_], V](): SerializableBuilder[K, V] = {
new SerializableBuilder[K, V]()
}
}
class SerializableImmutableRangeMap[K <: Comparable[_], V](ranges: ImmutableList[Range[K]], values: ImmutableList[V]) extends ImmutableRangeMap[K, V](ranges, values) with Serializable {
def this() {
this(ImmutableList.of(), ImmutableList.of())
}
}
和堆栈跟踪:
java.io.InvalidClassException: com.google.common.collect.SerializableImmutableRangeMap; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:150)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:768)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1775)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1707)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1345)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at scala.collection.immutable.$colon$colon.readObject(List.scala:362)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1896)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at org.apache.spark.serializer.JavaDeserializationStream.readObject(JavaSerializer.scala:68)
at org.apache.spark.serializer.JavaSerializerInstance.deserialize(JavaSerializer.scala:94)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:60)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:41)
at org.apache.spark.scheduler.Task.run(Task.scala:64)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:203)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
最佳答案
Serializable
文档的相关部分:
To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.
您的父类(super class) (ImmutableRangeMap
) 没有无参数构造函数,也不是Serializable
。这就是问题。 Serializable
类(例如 SerializableImmutableRangeMap
)不需要无参数构造函数。
关于java - Scala - InvalidClassException : no valid constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32210952/
在我的程序中,我有一个 FacebookUser 类,其中包含该用户的所有 friend 的 ArrayList。所有这些 friend 也可能有 friend 。我正在使用递归来获取一个新的 Arr
我在与同事的代码集成时遇到一些问题。 他有一个实体类,我们称之为FooEntity: @Entity @XmlRootElement @Table(name = "...") public class
InvalidClassException: local class incompatible: stream classdesc serialVersionUID = -19641044047501
这个问题已经有答案了: What is a serialVersionUID and why should I use it? (25 个回答) 已关闭 9 年前。 请告诉我我该怎么做这个异常即将到来
在我的实现中,我使用 Java 序列化来发送一组二进制对象,这些二进制对象以通知 header 开头,通知 header 告知将传递多少个对象以及它们各自的大小。首先进行简单的测试,我能够成功传输 h
我是 RMI 新手。从java文档中我发现当本地和远程文件中的串行版本不匹配时会抛出InvalidClassException。但我写了一个虚拟类,即使静态创建的serialVersionUID,它也
读完连载后,我尝试对书中提供的示例进行实验。以下代码有一些变化,这基本上是从 SCJP 书中挑选的。 import java.io.FileInputStream; public class Seri
此错误并不典型,通常我只需在本地定义serialVersionUID即可解决该错误。这次我将其定义如下: private static final long serialVersionUID = -4
这是我第一次在android上画东西。我按照developers.android.com 上的教程进行操作: http://developer.android.com/training/custom-
这个问题已经有答案了: java.io.invalidClassException during serializing/deserializing (3 个回答) 已关闭 7 年前。 我正在开发一款
我在数据库中保存了一个 java 对象,然后几天后我更改了我的 jre 版本。现在,当我尝试读取同一个对象时,出现以下异常: Exception in thread "main" java.io.In
我正在尝试序列化一个对象,然后在将其数据发送到客户端程序后将其反序列化。 这是对象继承如何工作的示例。我正在序列化和反序列化的对象是人。 生活 -> 动物 -> NPC -> 人 -> child L
我正在解决这个问题,但收到此错误无法指出错误: 这是我的课: package stdaccmdmngmt; import java.io.File; import java.io.FileInputS
这个问题已经有答案了: 奥 git _a (5 个回答) 已关闭 9 年前。 我不知道为什么会发生这种情况。 class Course implements Serializable {
每当我尝试在项目中运行它时,我都会在运行时遇到异常。 EXCEPTION MESSAGE :Message : java.io.InvalidClassException: com.mypackage
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
public static void save(Settings settings) { try { ObjectOutputStream os =
我正在尝试制作在线绘画游戏。为此,我在 Android 中使用 Path 类,我需要将它发送到我的 Java 服务器。为此,我曾尝试扩展 Path 类,如下所示: public class Seria
当我尝试从数据库(mysql)检索blob数据时,出现以下异常。 java.io.InvalidClassException: com.nmsworks.cygnet.tmf.mtnm.element
我在执行 Java 程序时遇到错误。请帮我做什么?错误如下: java.io.InvalidClassException: buddyconnect.UserInfo; local class inc
我是一名优秀的程序员,十分优秀!