Skip to content

Commit

Permalink
fix(java): Fix replace resolver serializaiton (apache#1812)
Browse files Browse the repository at this point in the history
## What does this PR do?

<!-- Describe the purpose of this PR. -->

## Related issues

Closes apache#1805
Closes apache#1804


## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?

## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
  • Loading branch information
chaokunyang authored Aug 21, 2024
1 parent 56e86a5 commit 8d5f8f3
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ public String getMessage() {
if (readObjects == null) {
return super.getMessage();
} else {
return "Deserialize failed, read objects are: " + readObjects;
try {
return "Deserialize failed, read objects are: " + readObjects;
} catch (Throwable e) {
StringBuilder builder =
new StringBuilder("Deserialize failed, type of read objects are: [");
for (Object readObject : readObjects) {
builder.append(readObject == null ? null : readObject.getClass()).append(", ");
}
builder.delete(builder.length() - 2, 2);
builder.append("]");
return builder.toString();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.apache.fury.type.TypeUtils.COLLECTION_TYPE;
import static org.apache.fury.type.TypeUtils.MAP_TYPE;
import static org.apache.fury.type.TypeUtils.collectionOf;
import static org.apache.fury.type.TypeUtils.getArrayComponent;
import static org.apache.fury.type.TypeUtils.mapOf;

import java.io.ObjectStreamClass;
Expand Down Expand Up @@ -242,7 +243,8 @@ public List<Descriptor> getDescriptors(ClassResolver resolver, Class<?> cls) {
if (rawType.isEnum()
|| rawType.isAssignableFrom(descriptor.getRawType())
|| NonexistentClass.isNonexistent(rawType)
|| rawType.isAssignableFrom(FinalObjectTypeStub.class)) {
|| rawType == FinalObjectTypeStub.class
|| (rawType.isArray() && getArrayComponent(rawType) == FinalObjectTypeStub.class)) {
descriptor = descriptor.copyWithTypeName(newDesc.getTypeName());
descriptors.add(descriptor);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,10 @@ public void writeClassInternal(MemoryBuffer buffer, Class<?> cls) {
classInfo = new ClassInfo(this, cls, null, null, classId == null ? NO_CLASS_ID : classId);
classInfoMap.put(cls, classInfo);
}
writeClassInternal(buffer, classInfo);
}

public void writeClassInternal(MemoryBuffer buffer, ClassInfo classInfo) {
short classId = classInfo.classId;
if (classId == REPLACE_STUB_ID) {
// clear class id to avoid replaced class written as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void write(MemoryBuffer buffer, Object value) {
}

private void writeObject(MemoryBuffer buffer, Object value, MethodInfoCache jdkMethodInfoCache) {
classResolver.writeClass(buffer, writeClassInfo);
classResolver.writeClassInternal(buffer, writeClassInfo);
jdkMethodInfoCache.objectSerializer.write(buffer, value);
}

Expand Down
1 change: 1 addition & 0 deletions scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ resolvers += Resolver.ApacheMavenSnapshotsRepo
libraryDependencies ++= Seq(
"org.apache.fury" % "fury-core" % furyVersion,
"org.scalatest" %% "scalatest" % "3.2.19" % Test,
"dev.zio" %% "zio" % "2.1.7" % Test,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
* under the License.
*/

package org.apache.fury.serializer
package org.apache.fury.serializer.scala

import org.apache.fury.Fury
import org.apache.fury.config.Language
import org.apache.fury.serializer.scala.ScalaDispatcher
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.serializer.scala

import org.apache.fury.Fury
import org.apache.fury.config.CompatibleMode
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import zio.Chunk

import scala.collection.immutable.ArraySeq

object SingletonObject {
case object Query

case class ArraySeqQuery(c: ArraySeq[Query.type])

case class ArrayQuery(c: Array[Query.type])

case class CaseChunk(c: Chunk[Int])
}

class CompatibleSingleObjectSerializerTest extends AnyWordSpec with Matchers {
def fury: Fury = {
org.apache.fury.Fury
.builder()
.withScalaOptimizationEnabled(true)
.requireClassRegistration(false)
.withRefTracking(true)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.build()
}

"fury scala object support" should {
"serialize/deserialize" in {
fury.deserialize(fury.serialize(singleton)) shouldBe singleton
fury.deserialize(fury.serialize(Pair(singleton, singleton))) shouldEqual Pair(singleton, singleton)
}
"nested type serialization in object type" in {
val x = A.B.C("hello, world!")
val bytes = fury.serialize(x)
fury.deserialize(bytes) shouldEqual A.B.C("hello, world!")
}
"testArraySeqQuery" in {
val o = SingletonObject.ArraySeqQuery(ArraySeq(SingletonObject.Query))
fury.deserialize(
fury.serialize(
o)) shouldEqual o
}
"testArrayQuery" in {
val o = SingletonObject.ArrayQuery(Array(SingletonObject.Query))
fury.deserialize(fury.serialize(o)).getClass shouldEqual o.getClass
}
"testCaseChunk" in {
val o = SingletonObject.CaseChunk(Chunk(1))
fury.deserialize(fury.serialize(o)) shouldEqual o
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.fury.serializer
package org.apache.fury.serializer.scala

import org.apache.fury.Fury
import org.apache.fury.config.Language
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.fury.serializer
package org.apache.fury.serializer.scala

import org.apache.fury.Fury
import org.apache.fury.config.Language
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.fury.serializer
package org.apache.fury.serializer.scala

import org.apache.fury.Fury
import org.apache.fury.config.Language
Expand Down

0 comments on commit 8d5f8f3

Please sign in to comment.