View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.kryo;
15  
16  import ch.qos.logback.classic.spi.IThrowableProxy;
17  import ch.qos.logback.classic.spi.StackTraceElementProxy;
18  import ch.qos.logback.classic.spi.ThrowableProxyVO;
19  import com.esotericsoftware.kryo.kryo5.Kryo;
20  import com.esotericsoftware.kryo.kryo5.Serializer;
21  import com.esotericsoftware.kryo.kryo5.io.Input;
22  import com.esotericsoftware.kryo.kryo5.io.Output;
23  
24  /**
25   * Serializer for {@link ThrowableProxyVO} used by the vanilla stream format.
26   *
27   * <p>Relies only on the public API of {@link ThrowableProxyVO}, i.e. its getters and
28   * {@link ThrowableProxyVO#builder()}. Kryo's reflection based FieldSerializer would require
29   * module ch.qos.logback.classic to open package ch.qos.logback.classic.spi.
30   */
31  public class VanillaThrowableProxyVOSerializer extends Serializer<ThrowableProxyVO> {
32  
33      @Override
34      public void write(Kryo kryo, Output output, ThrowableProxyVO throwableProxyVO) {
35          output.writeString(throwableProxyVO.getClassName());
36          output.writeString(throwableProxyVO.getOverridingMessage());
37          output.writeString(throwableProxyVO.getMessage());
38          output.writeVarInt(throwableProxyVO.getCommonFrames(), true);
39          kryo.writeObjectOrNull(output, throwableProxyVO.getStackTraceElementProxyArray(), StackTraceElementProxy[].class);
40          kryo.writeObjectOrNull(output, asThrowableProxyVO(throwableProxyVO.getCause()), ThrowableProxyVO.class);
41  
42          IThrowableProxy[] suppressed = throwableProxyVO.getSuppressed();
43          if (suppressed == null) {
44              output.writeVarInt(-1, true);
45          } else {
46              output.writeVarInt(suppressed.length, true);
47              for (IThrowableProxy s : suppressed) {
48                  kryo.writeObjectOrNull(output, asThrowableProxyVO(s), ThrowableProxyVO.class);
49              }
50          }
51          output.writeBoolean(throwableProxyVO.isCyclic());
52      }
53  
54      @Override
55      public ThrowableProxyVO read(Kryo kryo, Input input, Class<? extends ThrowableProxyVO> type) {
56          String className = input.readString();
57          String overridingMessage = input.readString();
58          String message = input.readString();
59          int commonFrames = input.readVarInt(true);
60          StackTraceElementProxy[] steps = kryo.readObjectOrNull(input, StackTraceElementProxy[].class);
61          ThrowableProxyVO cause = kryo.readObjectOrNull(input, ThrowableProxyVO.class);
62  
63          IThrowableProxy[] suppressed = null;
64          int suppressedLength = input.readVarInt(true);
65          if (suppressedLength >= 0) {
66              suppressed = new IThrowableProxy[suppressedLength];
67              for (int i = 0; i < suppressedLength; i++) {
68                  suppressed[i] = kryo.readObjectOrNull(input, ThrowableProxyVO.class);
69              }
70          }
71          boolean cyclic = input.readBoolean();
72  
73          return ThrowableProxyVO.builder()
74                  .setClassName(className)
75                  .setOverridingMessage(overridingMessage)
76                  .setMessage(message)
77                  .setCommonFramesCount(commonFrames)
78                  .setStackTraceElementProxyArray(steps)
79                  .setCause(cause)
80                  .setSuppressed(suppressed)
81                  .setCyclic(cyclic)
82                  .build();
83      }
84  
85      private static ThrowableProxyVO asThrowableProxyVO(IThrowableProxy proxy) {
86          if (proxy == null || proxy instanceof ThrowableProxyVO) {
87              return (ThrowableProxyVO) proxy;
88          }
89          return ThrowableProxyVO.build(proxy);
90      }
91  }