Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sofaboot 4.0 support #93

Merged
merged 8 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.alipay.sofa</groupId>
<artifactId>hessian</artifactId>
<version>3.4.0</version>
<version>3.5.0-beta1</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Ant Group
* Copyright (c) 2004-2023 All Rights Reserved.
*/
package com.caucho.hessian.io;

import com.caucho.hessian.io.AbstractDeserializer;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author junyuan
* @version AbstractFieldSpecificDeserializer.java, v 0.1 2023年05月06日 14:21 junyuan Exp $
*/
public abstract class AbstractFieldSpecificDeserializer extends AbstractDeserializer {

protected Map<String, Field> _fields;

public AbstractFieldSpecificDeserializer(Class<?> cl) {
_fields = getFieldMapForSerialize(cl);
}

protected Map<String, Field> getFieldMapForSerialize(Class cl) {
Map<String, Field> fields = new HashMap<String, Field>();
for (; cl != null; cl = cl.getSuperclass()) {
Field[] originFields = cl.getDeclaredFields();
for (int i = 0; i < originFields.length; i++) {
Field field = originFields[i];
if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) {
continue;
} else if (fields.containsKey(field.getName())) {
continue;

Check warning on line 36 in src/main/java/com/caucho/hessian/io/AbstractFieldSpecificDeserializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/AbstractFieldSpecificDeserializer.java#L36

Added line #L36 was not covered by tests
}
fields.put(field.getName(), field);
}
}
return fields;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Ant Group
* Copyright (c) 2004-2023 All Rights Reserved.
*/
package com.caucho.hessian.io;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author junyuan
* @version AbstractFieldSpecificSerializer.java, v 0.1 2023年04月10日 19:34 junyuan Exp $
*/
public abstract class AbstractFieldSpecificSerializer extends AbstractSerializer {

protected Field[] _fields;

public AbstractFieldSpecificSerializer(Class<?> clazz) {
this._fields = getFieldsForSerialize(clazz);
}

public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
if (obj == null) {
out.writeNull();
return;

Check warning on line 29 in src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java#L28-L29

Added lines #L28 - L29 were not covered by tests
}

if (out.addRef(obj)) {
return;

Check warning on line 33 in src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java#L33

Added line #L33 was not covered by tests
}
Class cl = obj.getClass();
int ref = out.writeObjectBegin(cl.getName());

if (ref < -1) {
writeObject10(obj, out);

Check warning on line 39 in src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java#L39

Added line #L39 was not covered by tests
}
else {
if (ref == -1) {
writeDefinition20(out);
out.writeObjectBegin(cl.getName());
}

writeInstance(obj, out);
}
}

private void writeObject10(Object obj, AbstractHessianOutput out)
throws IOException
{
for (int i = 0; i < _fields.length; i++) {
Field field = _fields[i];

Check warning on line 55 in src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java#L55

Added line #L55 was not covered by tests

out.writeString(field.getName());

Check warning on line 57 in src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java#L57

Added line #L57 was not covered by tests

serializeField(out, obj, field);

Check warning on line 59 in src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java#L59

Added line #L59 was not covered by tests
}

out.writeMapEnd();
}

Check warning on line 63 in src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/AbstractFieldSpecificSerializer.java#L62-L63

Added lines #L62 - L63 were not covered by tests

private void writeDefinition20(AbstractHessianOutput out)
throws IOException
{
out.writeClassFieldLength(_fields.length);

for (int i = 0; i < _fields.length; i++) {
Field field = _fields[i];

out.writeString(field.getName());
}
}

public void writeInstance(Object obj, AbstractHessianOutput out)
throws IOException
{
for (int i = 0; i < _fields.length; i++) {
Field field = _fields[i];
serializeField(out, obj, field);
}
}

protected abstract void serializeField(AbstractHessianOutput out, Object obj, Field field) throws IOException;

/**
* get all fields
* include super class
* exclude transient or static
* @param cl
* @return
*/
protected Field[] getFieldsForSerialize(Class cl) {
List<Field> fields = new ArrayList<Field>();
for (; cl != null; cl = cl.getSuperclass()) {
Field[] originFields = cl.getDeclaredFields();
for (int i = 0; i < originFields.length; i++) {
Field field = originFields[i];
if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) {
continue;
}
fields.add(field);
}
}
return fields.toArray(new Field[0]);
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/caucho/hessian/io/JavaDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

package com.caucho.hessian.io;

import com.caucho.hessian.util.ReflectionUtil;
import sun.misc.Unsafe;

import java.io.IOException;
Expand Down Expand Up @@ -99,7 +100,7 @@ public JavaDeserializer(Class cl)
_readResolve = getReadResolve(cl);

if (_readResolve != null) {
_readResolve.setAccessible(true);
ReflectionUtil.setAccessible(_readResolve);
}

Constructor[] constructors = cl.getDeclaredConstructors();
Expand Down Expand Up @@ -138,7 +139,7 @@ else if (param[j].isPrimitive())
}

if (_constructor != null) {
_constructor.setAccessible(true);
ReflectionUtil.setAccessible(_constructor);
Class[] params = _constructor.getParameterTypes();
_constructorArgs = new Object[params.length];
for (int i = 0; i < params.length; i++) {
Expand Down Expand Up @@ -325,11 +326,7 @@ else if (fieldMap.get(field.getName()) != null)
continue;

// XXX: could parameterize the handler to only deal with public
try {
field.setAccessible(true);
} catch (Throwable e) {
e.printStackTrace();
}
ReflectionUtil.setAccessible(field);

Class type = field.getType();
FieldDeserializer deser;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/caucho/hessian/io/JavaSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

package com.caucho.hessian.io;

import com.caucho.hessian.util.ReflectionUtil;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand All @@ -70,8 +72,9 @@
public JavaSerializer(Class cl)
{
_writeReplace = getWriteReplace(cl);
if (_writeReplace != null)
_writeReplace.setAccessible(true);
if (_writeReplace != null) {
ReflectionUtil.setAccessible(_writeReplace);

Check warning on line 76 in src/main/java/com/caucho/hessian/io/JavaSerializer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/JavaSerializer.java#L76

Added line #L76 was not covered by tests
}

ArrayList primitiveFields = new ArrayList();
ArrayList compoundFields = new ArrayList();
Expand All @@ -86,7 +89,7 @@
continue;

// XXX: could parameterize the handler to only deal with public
field.setAccessible(true);
ReflectionUtil.setAccessible(field);

if (field.getType().isPrimitive() ||
field.getType().getName().startsWith("java.lang.") &&
Expand Down
41 changes: 39 additions & 2 deletions src/main/java/com/caucho/hessian/io/SerializerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import com.alipay.hessian.ClassNameResolver;
import com.alipay.hessian.ClassNameResolverBuilder;
import com.caucho.burlap.io.BurlapRemoteObject;
import com.caucho.hessian.io.atomic.AtomicDeserializer;
import com.caucho.hessian.io.atomic.AtomicSerializer;
import com.caucho.hessian.io.java8.DurationHandle;
import com.caucho.hessian.io.java8.InstantHandle;
import com.caucho.hessian.io.java8.Java8TimeSerializer;
Expand All @@ -66,12 +68,22 @@
import com.caucho.hessian.io.java8.ZoneIdSerializer;
import com.caucho.hessian.io.java8.ZoneOffsetHandle;
import com.caucho.hessian.io.java8.ZonedDateTimeHandle;
import com.caucho.hessian.io.throwable.StackTraceElementDeserializer;
import com.caucho.hessian.io.throwable.StackTraceElementSerializer;
import com.caucho.hessian.io.throwable.ThrowableHelper;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -236,7 +248,7 @@
serializer = new ArraySerializer();

else if (Throwable.class.isAssignableFrom(cl))
serializer = new ThrowableSerializer(cl);
serializer = ThrowableHelper.getSerializer(cl);

else if (InputStream.class.isAssignableFrom(cl))
serializer = new InputStreamSerializer();
Expand Down Expand Up @@ -343,6 +355,9 @@
else if (Enum.class.isAssignableFrom(cl))
deserializer = new EnumDeserializer(cl);

else if (Throwable.class.isAssignableFrom(cl))
deserializer = ThrowableHelper.getDeserializer(cl);

else
deserializer = getDefaultDeserializer(cl);

Expand Down Expand Up @@ -620,7 +635,7 @@

try {
Class stackTrace = Class.forName("java.lang.StackTraceElement");

_staticSerializerMap.put(stackTrace, new StackTraceElementSerializer());
_staticDeserializerMap.put(stackTrace, new StackTraceElementDeserializer());
} catch (Throwable e) {
}
Expand Down Expand Up @@ -663,6 +678,28 @@
log.warning(String.valueOf(t.getCause()));
}

try {
AtomicSerializer atomicSerializer = new AtomicSerializer();
_staticSerializerMap.put(AtomicInteger.class, atomicSerializer);
_staticSerializerMap.put(AtomicLong.class, atomicSerializer);
_staticSerializerMap.put(AtomicBoolean.class, atomicSerializer);
_staticSerializerMap.put(AtomicReference.class, atomicSerializer);
_staticSerializerMap.put(AtomicLongArray.class, atomicSerializer);
_staticSerializerMap.put(AtomicIntegerArray.class, atomicSerializer);
_staticSerializerMap.put(AtomicReferenceArray.class, atomicSerializer);

_staticDeserializerMap.put(AtomicInteger.class, new AtomicDeserializer(AtomicInteger.class));
_staticDeserializerMap.put(AtomicLong.class, new AtomicDeserializer(AtomicLong.class));
_staticDeserializerMap.put(AtomicBoolean.class, new AtomicDeserializer(AtomicBoolean.class));
_staticDeserializerMap.put(AtomicReference.class, new AtomicDeserializer(AtomicReference.class));
_staticDeserializerMap.put(AtomicLongArray.class, new AtomicDeserializer(AtomicLongArray.class));
_staticDeserializerMap.put(AtomicIntegerArray.class, new AtomicDeserializer(AtomicIntegerArray.class));
_staticDeserializerMap.put(AtomicReferenceArray.class, new AtomicDeserializer(AtomicReferenceArray.class));

} catch (Throwable t) {
log.warning(String.valueOf(t.getCause()));

Check warning on line 700 in src/main/java/com/caucho/hessian/io/SerializerFactory.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/caucho/hessian/io/SerializerFactory.java#L699-L700

Added lines #L699 - L700 were not covered by tests
}

}

/**
Expand Down

This file was deleted.

Loading
Loading