Skip to content

Commit d1d810a

Browse files
Lo1ntlo1nt
andauthored
fix: roll back previous throwable and stack trace element ser for compatibility (#111)
* fix: roll back previous throwable and stack trace element ser for compatibility * chore: format --------- Co-authored-by: lo1nt <[email protected]>
1 parent da95611 commit d1d810a

File tree

5 files changed

+223
-6
lines changed

5 files changed

+223
-6
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3+
*
4+
* The Apache Software License, Version 1.1
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
*
18+
* 3. The end-user documentation included with the redistribution, if
19+
* any, must include the following acknowlegement:
20+
* "This product includes software developed by the
21+
* Caucho Technology (http://www.caucho.com/)."
22+
* Alternately, this acknowlegement may appear in the software itself,
23+
* if and wherever such third-party acknowlegements normally appear.
24+
*
25+
* 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26+
* endorse or promote products derived from this software without prior
27+
* written permission. For written permission, please contact
28+
29+
*
30+
* 5. Products derived from this software may not be called "Resin"
31+
* nor may "Resin" appear in their names without prior written
32+
* permission of Caucho Technology.
33+
*
34+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37+
* DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39+
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40+
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42+
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43+
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44+
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45+
*
46+
* @author Scott Ferguson
47+
*/
48+
49+
package com.caucho.hessian.io;
50+
51+
/**
52+
*
53+
* @author lo1nt
54+
* @version StackTraceElementDeserializer.java, v 0.1 2024-03-08 16:46 lo1nt Exp $
55+
*/
56+
@Deprecated
57+
public class StackTraceElementDeserializer extends
58+
com.caucho.hessian.io.throwable.StackTraceElementDeserializer {
59+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3+
*
4+
* The Apache Software License, Version 1.1
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
*
18+
* 3. The end-user documentation included with the redistribution, if
19+
* any, must include the following acknowlegement:
20+
* "This product includes software developed by the
21+
* Caucho Technology (http://www.caucho.com/)."
22+
* Alternately, this acknowlegement may appear in the software itself,
23+
* if and wherever such third-party acknowlegements normally appear.
24+
*
25+
* 4. The names "Burlap", "Resin", and "Caucho" must not be used to
26+
* endorse or promote products derived from this software without prior
27+
* written permission. For written permission, please contact
28+
29+
*
30+
* 5. Products derived from this software may not be called "Resin"
31+
* nor may "Resin" appear in their names without prior written
32+
* permission of Caucho Technology.
33+
*
34+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37+
* DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39+
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40+
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42+
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43+
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44+
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45+
*
46+
* @author Scott Ferguson
47+
*/
48+
49+
package com.caucho.hessian.io;
50+
51+
/**
52+
*
53+
* @author lo1nt
54+
* @version ThrowableSerializer.java, v 0.1 2024-03-08 16:46 lo1nt Exp $
55+
*/
56+
@Deprecated
57+
public class ThrowableSerializer extends com.caucho.hessian.io.throwable.ReflectThrowableSerializer {
58+
public ThrowableSerializer(Class<?> clazz) {
59+
super(clazz);
60+
}
61+
}

src/main/java/com/caucho/hessian/io/throwable/ReflectThrowableSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.io.IOException;
1111

1212
/**
13-
* use under jdk 17
13+
* use under jdk < 17
1414
* @author junyuan
1515
* @version ReflectThrowableSerializer.java, v 0.1 2023年05月06日 10:46 junyuan Exp $
1616
*/

src/main/java/com/caucho/hessian/io/throwable/StackTraceElementDeserializer.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ public StackTraceElementDeserializer() {
8282
super(StackTraceElement.class);
8383

8484
try {
85+
// default, only read class, method, file and line
86+
_defaultConstructor = StackTraceElement.class.getDeclaredConstructor(String.class, String.class,
87+
String.class, int.class);
8588
if (_fields.size() > 4) {
8689
// available since java 9
8790
_constructorJdk9 = StackTraceElement.class.getDeclaredConstructor(String.class, String.class,
88-
String.class, String.class,
89-
String.class, String.class, int.class);
91+
String.class, String.class, String.class, String.class, int.class);
9092
}
91-
// default, only read class, method, file and line
92-
_defaultConstructor = StackTraceElement.class.getDeclaredConstructor(String.class, String.class,
93-
String.class, int.class);
9493
} catch (Exception e) {
9594
log.log(Level.FINE, e.toString(), e);
9695
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.caucho.hessian.io;
18+
19+
import org.junit.Assert;
20+
import org.junit.BeforeClass;
21+
import org.junit.Test;
22+
23+
import java.io.ByteArrayInputStream;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
27+
/**
28+
*
29+
* @author lo1nt
30+
* @version CompatibleTest.java, v 0.1 2024-03-08 16:58 lo1nt Exp $
31+
*/
32+
public class CompatibleTest {
33+
34+
Throwable t = null;
35+
private static ByteArrayOutputStream os;
36+
private static SerializerFactory factory;
37+
38+
@BeforeClass
39+
public static void setUp() {
40+
factory = new TestSerializeFactory();
41+
os = new ByteArrayOutputStream();
42+
}
43+
44+
{
45+
Throwable x = null;
46+
try {
47+
x.getStackTrace();
48+
} catch (NullPointerException e) {
49+
t = e;
50+
}
51+
}
52+
53+
@Test
54+
public void TestCreateCompatibleClass() {
55+
try {
56+
ThrowableSerializer ct = new ThrowableSerializer(RuntimeException.class);
57+
StackTraceElementDeserializer cs = new StackTraceElementDeserializer();
58+
} catch (Throwable t) {
59+
Assert.fail("should be no error");
60+
}
61+
}
62+
63+
@Test
64+
public void TestSerialize() throws IOException {
65+
os.reset();
66+
Hessian2Output output = new Hessian2Output(os);
67+
68+
output.setSerializerFactory(factory);
69+
output.writeObject(t);
70+
output.flush();
71+
72+
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
73+
Hessian2Input input = new Hessian2Input(is);
74+
input.setSerializerFactory(factory);
75+
Throwable actual = (Throwable) input.readObject();
76+
Assert.assertEquals(t.getStackTrace().length, actual.getStackTrace().length);
77+
Assert.assertEquals(t.getMessage(), actual.getMessage());
78+
}
79+
80+
private static class TestSerializeFactory extends SerializerFactory {
81+
@Override
82+
public Serializer getSerializer(Class cl) throws HessianProtocolException {
83+
if (Throwable.class.isAssignableFrom(cl)) {
84+
return new ThrowableSerializer(cl);
85+
}
86+
87+
return super.getSerializer(cl);
88+
}
89+
90+
@Override
91+
public Deserializer getDeserializer(Class cl) throws HessianProtocolException {
92+
if (StackTraceElement.class.isAssignableFrom(cl)) {
93+
return new StackTraceElementDeserializer();
94+
}
95+
return super.getDeserializer(cl);
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)