NETTY-419 ObjectEncoder/ObjectDecoder fails with NPE when deserializing the java.lang.Class instance of an interface type

* Fixed a bug where deserializing an interface class fails due to wrong lookup.
* Implemented class lookup caching
This commit is contained in:
Trustin Lee 2011-08-02 08:22:02 +09:00
parent 99daeebe4a
commit 5cdcc67086
2 changed files with 66 additions and 5 deletions

View File

@ -21,6 +21,8 @@ import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.StreamCorruptedException;
import java.util.HashMap;
import java.util.Map;
/**
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
@ -31,6 +33,7 @@ import java.io.StreamCorruptedException;
*/
class CompactObjectInputStream extends ObjectInputStream {
private final Map<String, Class<?>> classCache = new HashMap<String, Class<?>>();
private final ClassLoader classLoader;
CompactObjectInputStream(InputStream in) throws IOException {
@ -65,7 +68,7 @@ class CompactObjectInputStream extends ObjectInputStream {
case CompactObjectOutputStream.TYPE_THIN_DESCRIPTOR:
String className = readUTF();
Class<?> clazz = loadClass(className);
return ObjectStreamClass.lookup(clazz);
return ObjectStreamClass.lookupAny(clazz);
default:
throw new StreamCorruptedException(
"Unexpected class descriptor type: " + type);
@ -74,16 +77,33 @@ class CompactObjectInputStream extends ObjectInputStream {
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
// Query the cache first.
String className = desc.getName();
try {
return loadClass(className);
} catch (ClassNotFoundException ex) {
return super.resolveClass(desc);
Class<?> clazz = classCache.get(className);
if (clazz != null) {
return clazz;
}
// And then try to resolve.
try {
clazz = loadClass(className);
} catch (ClassNotFoundException ex) {
clazz = super.resolveClass(desc);
}
classCache.put(className, clazz);
return clazz;
}
protected Class<?> loadClass(String className) throws ClassNotFoundException {
// Query the cache first.
Class<?> clazz;
clazz = classCache.get(className);
if (clazz != null) {
return clazz;
}
// And then try to load.
ClassLoader classLoader = this.classLoader;
if (classLoader == null) {
classLoader = Thread.currentThread().getContextClassLoader();
@ -94,6 +114,8 @@ class CompactObjectInputStream extends ObjectInputStream {
} else {
clazz = Class.forName(className);
}
classCache.put(className, clazz);
return clazz;
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 2011 Red Hat, Inc.
*
* Red Hat 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 org.jboss.netty.handler.codec.serialization;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
/**
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
*/
public class CompactObjectSerializationTest {
@Test
public void testInterfaceSerialization() throws Exception {
PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
CompactObjectOutputStream out = new CompactObjectOutputStream(pipeOut);
CompactObjectInputStream in = new CompactObjectInputStream(pipeIn);
out.writeObject(List.class);
Assert.assertSame(List.class, in.readObject());
}
}