From 6917211be15bec07da4d65791b905243e345eaef Mon Sep 17 00:00:00 2001 From: "linghong.zzq" Date: Tue, 3 Jun 2025 16:00:19 +0800 Subject: [PATCH 1/2] init --- .../java/org/apache/fory/config/Config.java | 11 ++++- .../apache/fory/config/ExceptionLogMode.java | 25 ++++++++++++ .../org/apache/fory/config/ForyBuilder.java | 15 ++++++- .../org/apache/fory/util/ExceptionUtils.java | 40 ++++++++++++++----- 4 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java diff --git a/java/fory-core/src/main/java/org/apache/fory/config/Config.java b/java/fory-core/src/main/java/org/apache/fory/config/Config.java index be1eec2157..90278b66bc 100644 --- a/java/fory-core/src/main/java/org/apache/fory/config/Config.java +++ b/java/fory-core/src/main/java/org/apache/fory/config/Config.java @@ -63,6 +63,8 @@ public class Config implements Serializable { private final boolean deserializeNonexistentEnumValueAsNull; private final boolean serializeEnumByName; private final int bufferSizeLimitBytes; + private final ExceptionLogMode exceptionLogMode; + private final int logSampleStep; public Config(ForyBuilder builder) { name = builder.name; @@ -99,8 +101,15 @@ public Config(ForyBuilder builder) { deserializeNonexistentEnumValueAsNull = builder.deserializeNonexistentEnumValueAsNull; serializeEnumByName = builder.serializeEnumByName; bufferSizeLimitBytes = builder.bufferSizeLimitBytes; + exceptionLogMode = builder.exceptionLogMode; + logSampleStep = builder.logSampleStep; } - + public ExceptionLogMode getExceptionLogMode() { + return exceptionLogMode; + } + public int getLogSampleStep() { + return logSampleStep; + } /** Returns the name for Fory serialization. */ public String getName() { return name; diff --git a/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java b/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java new file mode 100644 index 0000000000..ab60d93d0c --- /dev/null +++ b/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java @@ -0,0 +1,25 @@ +package org.apache.fory.config; +/* + * 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. + */ + +public enum ExceptionLogMode { + ALL_PRINT, + SAMPLE_PRINT, + NONE_PRINT +} diff --git a/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java b/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java index cd3955a1ce..5faf53c6ca 100644 --- a/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java +++ b/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java @@ -86,7 +86,8 @@ public final class ForyBuilder { boolean serializeEnumByName = false; int bufferSizeLimitBytes = 128 * 1024; MetaCompressor metaCompressor = new DeflaterMetaCompressor(); - + ExceptionLogMode exceptionLogMode = ExceptionLogMode.ALL_PRINT; + int logSampleStep; public ForyBuilder() {} /** @@ -144,7 +145,19 @@ public ForyBuilder serializeEnumByName(boolean serializeEnumByName) { this.serializeEnumByName = serializeEnumByName; return this; } + /** exception log level + * choose the log level, print the error message. + * */ + public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode) { + this.exceptionLogMode = exceptionLogMode; + return this; + } + public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode, int logSampleStep) { + this.exceptionLogMode = exceptionLogMode; + this.logSampleStep = logSampleStep; + return this; + } /** * Whether ignore reference tracking of all time types registered in {@link TimeSerializers} when * ref tracking is enabled. diff --git a/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java b/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java index dd63992345..ab8d91eaa0 100644 --- a/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java +++ b/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java @@ -20,6 +20,7 @@ package org.apache.fory.util; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.fory.Fory; @@ -55,17 +56,36 @@ public static StackOverflowError trySetStackOverflowErrorMessage( } } - public static RuntimeException handleReadFailed(Fory fory, Throwable t) { - if (fory.getRefResolver() instanceof MapRefResolver) { - ObjectArray readObjects = ((MapRefResolver) fory.getRefResolver()).getReadObjects(); - // carry with read objects for better trouble shooting. - List objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size); - throw new DeserializationException(objects, t); - } else { - Platform.throwException(t); - throw new IllegalStateException("unreachable"); + public static RuntimeException handleReadFailed(Fory fory, Throwable t) { + if (fory.getRefResolver() instanceof MapRefResolver) { + List exceptionObjects = getExceptionObjects(fory); + throw new DeserializationException(exceptionObjects, t); + } else { + Platform.throwException(t); + throw new IllegalStateException("unreachable"); + } + } + public static List getExceptionObjects(Fory fory) { + ObjectArray readObjects = ((MapRefResolver) fory.getRefResolver()).getReadObjects(); + // carry with read objects for better trouble shooting. + List objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size); + switch (fory.getConfig().getExceptionLogMode()) { + case NONE_PRINT: + return new ArrayList<>(); + case SAMPLE_PRINT: + return systematicSample(objects, Math.max(1, fory.getConfig().getLogSampleStep())); + default: + return objects; + } + } + + public static List systematicSample(List list, int step) { + List result = new ArrayList<>(); + for (int i = 0; i < list.size(); i += step) { + result.add(list.get(i)); + } + return result; } - } public static void ignore(Object... args) {} } From 25ae8a2e105e757d20b0c497b8a65cc442a24e9c Mon Sep 17 00:00:00 2001 From: "linghong.zzq" Date: Tue, 3 Jun 2025 16:07:24 +0800 Subject: [PATCH 2/2] format --- .../java/org/apache/fory/config/Config.java | 19 ++++--- .../apache/fory/config/ExceptionLogMode.java | 7 +-- .../org/apache/fory/config/ForyBuilder.java | 25 ++++----- .../org/apache/fory/util/ExceptionUtils.java | 53 ++++++++++--------- 4 files changed, 55 insertions(+), 49 deletions(-) diff --git a/java/fory-core/src/main/java/org/apache/fory/config/Config.java b/java/fory-core/src/main/java/org/apache/fory/config/Config.java index 90278b66bc..e80be242e8 100644 --- a/java/fory-core/src/main/java/org/apache/fory/config/Config.java +++ b/java/fory-core/src/main/java/org/apache/fory/config/Config.java @@ -63,8 +63,8 @@ public class Config implements Serializable { private final boolean deserializeNonexistentEnumValueAsNull; private final boolean serializeEnumByName; private final int bufferSizeLimitBytes; - private final ExceptionLogMode exceptionLogMode; - private final int logSampleStep; + private final ExceptionLogMode exceptionLogMode; + private final int logSampleStep; public Config(ForyBuilder builder) { name = builder.name; @@ -104,12 +104,15 @@ public Config(ForyBuilder builder) { exceptionLogMode = builder.exceptionLogMode; logSampleStep = builder.logSampleStep; } - public ExceptionLogMode getExceptionLogMode() { - return exceptionLogMode; - } - public int getLogSampleStep() { - return logSampleStep; - } + + public ExceptionLogMode getExceptionLogMode() { + return exceptionLogMode; + } + + public int getLogSampleStep() { + return logSampleStep; + } + /** Returns the name for Fory serialization. */ public String getName() { return name; diff --git a/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java b/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java index ab60d93d0c..d6809ac0e5 100644 --- a/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java +++ b/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java @@ -1,4 +1,5 @@ package org.apache.fory.config; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -19,7 +20,7 @@ */ public enum ExceptionLogMode { - ALL_PRINT, - SAMPLE_PRINT, - NONE_PRINT + ALL_PRINT, + SAMPLE_PRINT, + NONE_PRINT } diff --git a/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java b/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java index 5faf53c6ca..d7c79213e1 100644 --- a/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java +++ b/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java @@ -88,6 +88,7 @@ public final class ForyBuilder { MetaCompressor metaCompressor = new DeflaterMetaCompressor(); ExceptionLogMode exceptionLogMode = ExceptionLogMode.ALL_PRINT; int logSampleStep; + public ForyBuilder() {} /** @@ -145,19 +146,19 @@ public ForyBuilder serializeEnumByName(boolean serializeEnumByName) { this.serializeEnumByName = serializeEnumByName; return this; } - /** exception log level - * choose the log level, print the error message. - * */ - public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode) { - this.exceptionLogMode = exceptionLogMode; - return this; - } - public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode, int logSampleStep) { - this.exceptionLogMode = exceptionLogMode; - this.logSampleStep = logSampleStep; - return this; - } + /** exception log level choose the log level, print the error message. */ + public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode) { + this.exceptionLogMode = exceptionLogMode; + return this; + } + + public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode, int logSampleStep) { + this.exceptionLogMode = exceptionLogMode; + this.logSampleStep = logSampleStep; + return this; + } + /** * Whether ignore reference tracking of all time types registered in {@link TimeSerializers} when * ref tracking is enabled. diff --git a/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java b/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java index ab8d91eaa0..5926ccacc1 100644 --- a/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java +++ b/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java @@ -56,36 +56,37 @@ public static StackOverflowError trySetStackOverflowErrorMessage( } } - public static RuntimeException handleReadFailed(Fory fory, Throwable t) { - if (fory.getRefResolver() instanceof MapRefResolver) { - List exceptionObjects = getExceptionObjects(fory); - throw new DeserializationException(exceptionObjects, t); - } else { - Platform.throwException(t); - throw new IllegalStateException("unreachable"); - } + public static RuntimeException handleReadFailed(Fory fory, Throwable t) { + if (fory.getRefResolver() instanceof MapRefResolver) { + List exceptionObjects = getExceptionObjects(fory); + throw new DeserializationException(exceptionObjects, t); + } else { + Platform.throwException(t); + throw new IllegalStateException("unreachable"); } - public static List getExceptionObjects(Fory fory) { - ObjectArray readObjects = ((MapRefResolver) fory.getRefResolver()).getReadObjects(); - // carry with read objects for better trouble shooting. - List objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size); - switch (fory.getConfig().getExceptionLogMode()) { - case NONE_PRINT: - return new ArrayList<>(); - case SAMPLE_PRINT: - return systematicSample(objects, Math.max(1, fory.getConfig().getLogSampleStep())); - default: - return objects; - } + } + + public static List getExceptionObjects(Fory fory) { + ObjectArray readObjects = ((MapRefResolver) fory.getRefResolver()).getReadObjects(); + // carry with read objects for better trouble shooting. + List objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size); + switch (fory.getConfig().getExceptionLogMode()) { + case NONE_PRINT: + return new ArrayList<>(); + case SAMPLE_PRINT: + return systematicSample(objects, Math.max(1, fory.getConfig().getLogSampleStep())); + default: + return objects; } + } - public static List systematicSample(List list, int step) { - List result = new ArrayList<>(); - for (int i = 0; i < list.size(); i += step) { - result.add(list.get(i)); - } - return result; + public static List systematicSample(List list, int step) { + List result = new ArrayList<>(); + for (int i = 0; i < list.size(); i += step) { + result.add(list.get(i)); } + return result; + } public static void ignore(Object... args) {} }