Skip to content

Commit

Permalink
Merge pull request #71 from AlbumenJ/0608-jdk
Browse files Browse the repository at this point in the history
Support for more types
  • Loading branch information
chickenlj authored Jun 10, 2024
2 parents ba3511e + 2a35e05 commit 4983a3f
Show file tree
Hide file tree
Showing 84 changed files with 4,250 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.alibaba.com.caucho.hessian.io;

import java.io.Serializable;
import java.util.Arrays;
import java.util.EnumSet;

class EnumSetHandler implements Serializable, HessianHandle {
private Class type;
private Object[] objects;

EnumSetHandler(Class type, Object[] objects) {
this.type = type;
this.objects = objects;
}

@SuppressWarnings("unchecked")
private Object readResolve() {
EnumSet enumSet = EnumSet.noneOf(type);
enumSet.addAll(Arrays.asList(objects));
return enumSet;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.alibaba.com.caucho.hessian.io;

import com.alibaba.com.caucho.hessian.HessianException;
import sun.misc.Unsafe;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.EnumSet;
import java.util.logging.Level;

public class EnumSetSerializer extends AbstractSerializer {
private static final boolean _isEnabled;

@SuppressWarnings("restriction")
private static Unsafe _unsafe;

private static long _elementTypeOffset;

static {
boolean isEnabled = false;

try {
Class<?> unsafe = Class.forName("sun.misc.Unsafe");
Field theUnsafe = null;
for (Field field : unsafe.getDeclaredFields()) {
if (field.getName().equals("theUnsafe"))
theUnsafe = field;
}

if (theUnsafe != null) {
theUnsafe.setAccessible(true);
_unsafe = (Unsafe) theUnsafe.get(null);
}

isEnabled = _unsafe != null;

String unsafeProp = System.getProperty("com.caucho.hessian.unsafe");

if ("false".equals(unsafeProp))
isEnabled = false;

if (isEnabled) {
Field elementType = EnumSet.class.getDeclaredField("elementType");
_elementTypeOffset = _unsafe.objectFieldOffset(elementType);
}
} catch (Throwable e) {
log.log(Level.FINER, e.toString(), e);
}

_isEnabled = isEnabled;
}

@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
if (obj == null) {
out.writeNull();
} else {
EnumSet enumSet = (EnumSet) obj;
Class type = getElementClass(enumSet);
Object[] objects = enumSet.toArray();
out.writeObject(new EnumSetHandler(type, objects));
}
}

private Class<?> getElementClass(EnumSet enumSet) throws IOException {
if (!_isEnabled) {
if (enumSet.isEmpty()) {
throw new HessianFieldException("Unable to serialize empty EnumSet without unsafe access");
}
return enumSet.iterator().next().getClass();
}
try {
return (Class<?>) _unsafe.getObject(enumSet, _elementTypeOffset);
} catch (Throwable e) {
throw new HessianException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@
* Serializing a locale.
*/
public class LocaleSerializer extends AbstractSerializer {
private static LocaleSerializer SERIALIZER = new LocaleSerializer();

public static LocaleSerializer create() {
return SERIALIZER;
}

@Override
public void writeObject(Object obj, AbstractHessianOutput out)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.alibaba.com.caucho.hessian.io;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* Serializing an object for known object types.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.alibaba.com.caucho.hessian.io;

import java.io.IOException;
import java.util.UUID;

/**
* Deserializing a uuid valued object
**/
public class UUIDDeserializer extends AbstractDeserializer {

@Override
public Class getType() {
return UUID.class;
}

@Override
public Object readObject(AbstractHessianInput in, Object[] fields) throws IOException {
String uuidString = in.readString();
UUID uuid = UUID.fromString(uuidString);

in.addRef(uuid);
return uuid;
}

@Override
public Object readObject(AbstractHessianInput in, String[] fieldNames) throws IOException {
String uuidString = in.readString();
UUID uuid = UUID.fromString(uuidString);

in.addRef(uuid);
return uuid;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.alibaba.com.caucho.hessian.io.atomic;

import com.alibaba.com.caucho.hessian.io.AbstractDeserializer;
import com.alibaba.com.caucho.hessian.io.AbstractHessianInput;
import com.alibaba.com.caucho.hessian.io.IOExceptionWrapper;

import java.io.IOException;
import java.util.concurrent.atomic.LongAdder;

/**
* Serializing an object for known object types.
*/
public class LongAdderDeserializer extends AbstractDeserializer {

public Class<?> getType() {
return LongAdder.class;
}

@Override
public Object readObject(AbstractHessianInput in,
Object[] fields)
throws IOException {
try {
return readObject(in, (String[]) fields);
} catch (IOException e) {
throw e;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IOExceptionWrapper(LongAdder.class.getName() + ":" + e, e);
}
}

@Override
public Object readObject(AbstractHessianInput in,
String[] fieldNames)
throws IOException {
try {
long l = in.readLong();
LongAdder obj = new LongAdder();
obj.add(l);
in.addRef(obj);

return obj;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOExceptionWrapper(LongAdder.class.getName() + ":" + e, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.alibaba.com.caucho.hessian.io.atomic;

import com.alibaba.com.caucho.hessian.io.AbstractHessianOutput;
import com.alibaba.com.caucho.hessian.io.AbstractSerializer;

import java.io.IOException;
import java.util.concurrent.atomic.LongAdder;

/**
* Serializing an object for known object types.
*/
public class LongAdderSerializer extends AbstractSerializer {

@Override
public void writeObject(Object obj, AbstractHessianOutput out)
throws IOException {

String replacedClName = "java.util.concurrent.atomic.LongAdder$SerializationProxy";

int ref = out.writeObjectBegin(replacedClName);

if (ref < -1) {
out.writeString("value");
out.writeObject(((LongAdder) obj).longValue());

out.writeMapEnd();
} else {
if (ref == -1) {
out.writeClassFieldLength(1);

out.writeString("value");

out.writeObjectBegin(replacedClName);
}

out.writeObject(((LongAdder) obj).longValue());
}
}
}
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.alibaba.com.caucho.hessian.io.chronology;

import com.alibaba.com.caucho.hessian.io.HessianHandle;

import java.io.Serializable;
import java.time.chrono.AbstractChronology;
import java.time.chrono.Chronology;

public class AbstractChronologyHandle implements HessianHandle, Serializable {

private final String id;

public AbstractChronologyHandle(AbstractChronology o) {
this.id = o.getId();
}

private Object readResolve() {
return Chronology.of(id);
}
}

Loading

0 comments on commit 4983a3f

Please sign in to comment.