Skip to content

Commit

Permalink
[Java] Add detailed lambda error (#698)
Browse files Browse the repository at this point in the history
* add detailed lambda serialization error

* refine serializer creation stacktrace
  • Loading branch information
chaokunyang authored Jul 19, 2023
1 parent 7b613c3 commit c30145e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public LambdaSerializer(Fury fury, Class cls) {
if (cls != ReplaceStub.class) {
if (!Serializable.class.isAssignableFrom(cls)) {
String msg =
String.format("Lambda needs to implement %s for serialization", Serializable.class);
String.format(
"Lambda %s needs to implement %s for serialization",
cls, Serializable.class.getName());
throw new UnsupportedOperationException(msg);
}
writeReplaceMethod =
Expand Down
51 changes: 27 additions & 24 deletions java/fury-core/src/main/java/io/fury/serializer/Serializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,33 @@ public static <T> Serializer<T> newSerializer(
return new CompatibleSerializer(fury, type);
}
try {
try {
Constructor<? extends Serializer> ctr =
serializerClass.getConstructor(Fury.class, Class.class);
ctr.setAccessible(true);
return ctr.newInstance(fury, type);
} catch (NoSuchMethodException e) {
Utils.ignore(e);
}
try {
Constructor<? extends Serializer> ctr = serializerClass.getConstructor(Fury.class);
ctr.setAccessible(true);
return ctr.newInstance(fury);
} catch (NoSuchMethodException e) {
Utils.ignore(e);
}
try {
Constructor<? extends Serializer> ctr = serializerClass.getConstructor(Class.class);
ctr.setAccessible(true);
return ctr.newInstance(type);
} catch (NoSuchMethodException e) {
Utils.ignore(e);
}
return serializerClass.newInstance();
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
Constructor<? extends Serializer> ctr =
serializerClass.getConstructor(Fury.class, Class.class);
ctr.setAccessible(true);
return ctr.newInstance(fury, type);
} catch (NoSuchMethodException e) {
Utils.ignore(e);
}
try {
Constructor<? extends Serializer> ctr = serializerClass.getConstructor(Fury.class);
ctr.setAccessible(true);
return ctr.newInstance(fury);
} catch (NoSuchMethodException e) {
Utils.ignore(e);
}
try {
Constructor<? extends Serializer> ctr = serializerClass.getConstructor(Class.class);
ctr.setAccessible(true);
return ctr.newInstance(type);
} catch (NoSuchMethodException e) {
Utils.ignore(e);
}
return serializerClass.newInstance();
} catch (InvocationTargetException e) {
fury.getClassResolver().resetSerializer(type, serializer);
if (e.getCause() != null) {
Platform.throwException(e.getCause());
} else {
Platform.throwException(e);
}
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;

import io.fury.Fury;
import io.fury.FuryTestBase;
Expand Down Expand Up @@ -53,4 +55,16 @@ public void testLambda(Fury fury) {
assertSame(
fury.getClassResolver().getSerializerClass(Class.class), Serializers.ClassSerializer.class);
}

@Test
public void testLambdaUnserializableMsg() {
Fury fury = Fury.builder().disableSecureMode().build();
Function<Object, String> function = String::valueOf;
assertThrows(UnsupportedOperationException.class, () -> fury.serialize(function));
try {
fury.serialize(function);
} catch (Exception e) {
assertTrue(e.getMessage().contains("LambdaSerializerTest"));
}
}
}

0 comments on commit c30145e

Please sign in to comment.