-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from AlbumenJ/0608-jdk
Support for more types
- Loading branch information
Showing
84 changed files
with
4,250 additions
and
87 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/EnumSetHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/EnumSetSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/UUIDDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...an-lite/src/main/java/com/alibaba/com/caucho/hessian/io/atomic/LongAdderDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/atomic/LongAdderSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
.../src/main/java/com/alibaba/com/caucho/hessian/io/chronology/AbstractChronologyHandle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
Oops, something went wrong.