Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.fory.serializer.JavaSerializer;
import org.apache.fory.serializer.JdkProxySerializer;
import org.apache.fory.serializer.LambdaSerializer;
import org.apache.fory.serializer.NonSerializableSerializer;
import org.apache.fory.serializer.ObjectSerializer;
import org.apache.fory.serializer.ObjectStreamSerializer;
import org.apache.fory.serializer.PrimitiveArraySerializers;
Expand Down Expand Up @@ -137,6 +138,7 @@ public class GraalvmSupport {
registerDefaultSerializerClass(ObjectStreamSerializer.class);
registerDefaultSerializerClass(JavaSerializer.class);
registerDefaultSerializerClass(ObjectSerializer.class);
registerDefaultSerializerClass(NonSerializableSerializer.class);
}

/** Returns true if current process is running in graalvm native image build stage. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
import org.apache.fory.serializer.JdkProxySerializer;
import org.apache.fory.serializer.LambdaSerializer;
import org.apache.fory.serializer.LocaleSerializer;
import org.apache.fory.serializer.NonSerializableSerializer;
import org.apache.fory.serializer.NoneSerializer;
import org.apache.fory.serializer.ObjectSerializer;
import org.apache.fory.serializer.OptionalSerializers;
Expand Down Expand Up @@ -1502,8 +1503,7 @@ public Class<? extends Serializer> getSerializerClass(Class<?> cls, boolean code
}
if (config.checkJdkClassSerializable()) {
if (cls.getName().startsWith("java") && !(Serializable.class.isAssignableFrom(cls))) {
throw new UnsupportedOperationException(
String.format("Class %s doesn't support serialization.", cls));
return NonSerializableSerializer.class;
}
}
if (ScalaTypes.SCALA_AVAILABLE && ReflectionUtils.isScalaSingletonObject(cls)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.fory.serializer;

import org.apache.fory.config.Config;
import org.apache.fory.context.ReadContext;
import org.apache.fory.context.WriteContext;

/**
* Serializer for non-serializable JDK types that are retained by reference during copy.
*
* <p>These objects can contain JVM-managed state, so copying their fields is unsupported.
*/
public final class NonSerializableSerializer<T> extends Serializer<T> {
public NonSerializableSerializer(Config config, Class<T> type) {
super(config, type, true);
}

@Override
public void write(WriteContext writeContext, T value) {
throw unsupportedOperation();
}

@Override
public T read(ReadContext readContext) {
throw unsupportedOperation();
}

private UnsupportedOperationException unsupportedOperation() {
return new UnsupportedOperationException(
String.format("Class %s doesn't support serialization.", type));
}
}
31 changes: 31 additions & 0 deletions java/fory-core/src/test/java/org/apache/fory/ForyCopyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import org.apache.fory.collection.LazyMap;
import org.apache.fory.config.Language;
import org.apache.fory.context.CopyContext;
import org.apache.fory.context.ReadContext;
import org.apache.fory.context.WriteContext;
import org.apache.fory.exception.ForyException;
import org.apache.fory.exception.SerializationException;
import org.apache.fory.resolver.TypeResolver;
import org.apache.fory.serializer.EnumSerializerTest;
import org.apache.fory.serializer.EnumSerializerTest.EnumFoo;
Expand Down Expand Up @@ -210,6 +212,35 @@ public void testCopyOnlySerializerStillRejectsSerialize() {
Assert.assertThrows(ForyException.class, () -> fory.serialize(beanA));
}

@Test
public void testCopyNonSerializableJdkObject() {
Fory fory =
builder()
.withLanguage(Language.JAVA)
.withRefTracking(true)
.withRefCopy(true)
.requireClassRegistration(false)
.build();
Package packageValue = String.class.getPackage();

Assert.assertSame(fory.copy(packageValue), packageValue);

Object[] values = new Object[] {packageValue};
Object[] copiedValues = fory.copy(values);
Assert.assertNotSame(copiedValues, values);
Assert.assertSame(copiedValues[0], packageValue);
}

@Test
public void testSerializeNonSerializableJdkObjectStillFails() {
Fory fory = builder().withLanguage(Language.JAVA).requireClassRegistration(false).build();
Package packageValue = String.class.getPackage();

SerializationException exception =
Assert.expectThrows(SerializationException.class, () -> fory.serialize(packageValue));
Assert.assertTrue(exception.getCause() instanceof UnsupportedOperationException);
}

@Test
public void testCopyFailureDoesNotLeakCopyDepth() {
Fory fory =
Expand Down
Loading