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
9 changes: 8 additions & 1 deletion src/main/java/com/caucho/hessian/io/EnumDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ public class EnumDeserializer extends AbstractDeserializer {

public EnumDeserializer(Class cl)
{
try {
// hessian/33b[34], hessian/3bb[78]
if (cl.isEnum())
_enumType = cl;
else if (cl.getSuperclass().isEnum())
_enumType = cl.getSuperclass();
else
throw new RuntimeException("Class " + cl.getName() + " is not an enum");

try {
_valueOf = cl.getMethod("valueOf",
new Class[] { Class.class, String.class });
} catch (Exception e) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/caucho/hessian/io/EnumSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class EnumSerializer extends AbstractSerializer {

public EnumSerializer(Class cl)
{
// hessian/32b[12], hessian/3ab[23]
if (!cl.isEnum() && cl.getSuperclass().isEnum())
cl = cl.getSuperclass();

try {
_name = cl.getMethod("name", new Class[0]);
} catch (Exception e) {
Expand All @@ -74,6 +78,9 @@ public void writeObject(Object obj, AbstractHessianOutput out)

Class cl = obj.getClass();

if (!cl.isEnum() && cl.getSuperclass().isEnum())
cl = cl.getSuperclass();

String name = null;
try {
name = (String) _name.invoke(obj, (Object[]) null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;
import com.caucho.hessian.test.ClassEnum;
import com.caucho.hessian.test.Color;
import org.junit.Test;

Expand Down Expand Up @@ -264,6 +265,47 @@ public void testEnum() throws Exception {
assertTrue(o == Color.BLANK);
}

/**
* 检测ClassEnum的序列化情况
* @throws Exception
*/
@Test
public void testClassEnum() throws IOException {
ClassEnum classEnum = ClassEnum.A;

ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new SerializerFactory());
hout.writeObject(classEnum);
hout.close();

byte[] body = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
Hessian2Input hin = new Hessian2Input(bin);
hin.setSerializerFactory(new GenericSerializerFactory());

Object o = hin.readObject();
assertEquals(GenericObject.class, o.getClass());
Object obj = GenericUtils.convertToObject(o);
assertEquals(obj.getClass(), ClassEnum.A.getClass());
assertTrue(obj == ClassEnum.A);

GenericObject col = GenericUtils.convertToGenericObject(obj);
bout = new ByteArrayOutputStream();
hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(col);
hout.close();

body = bout.toByteArray();
bin = new ByteArrayInputStream(body, 0, body.length);
hin = new Hessian2Input(bin);
hin.setSerializerFactory(new SerializerFactory());
o = hin.readObject();
assertEquals(o.getClass(), ClassEnum.A.getClass());
assertTrue(o == ClassEnum.A);
}

/**
* 检测Calendar的序列化情况
* @throws Exception
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/caucho/hessian/test/ClassEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 com.caucho.hessian.test;

/**
* @author lianglipeng.llp@alibaba-inc.com
* @version $Id: ClassEnum.java, v 0.1 2025年05月29日 15:46 立蓬 Exp $
*/
public enum ClassEnum {
A {
@Override
public void test() {
System.out.println("A");
}
},
B {
@Override
public void test() {
System.out.println("B");
}
};
public abstract void test();
}