com.aspectran.core.util.CustomObjectInputStream Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2019 The Aspectran Project
*
* Licensed 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.aspectran.core.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
/**
* For re-inflating serialized objects, this class uses the thread context classloader
* rather than the JVM's default classloader selection.
*/
public class CustomObjectInputStream extends ObjectInputStream {
public CustomObjectInputStream() throws IOException {
super();
}
public CustomObjectInputStream(InputStream in) throws IOException {
super(in);
}
@Override
public Class> resolveClass(java.io.ObjectStreamClass cl)
throws IOException, ClassNotFoundException {
try {
return Class.forName(cl.getName(), false, ClassUtils.getDefaultClassLoader());
} catch (ClassNotFoundException e) {
return super.resolveClass(cl);
}
}
@Override
protected Class> resolveProxyClass(String[] interfaces)
throws ClassNotFoundException {
ClassLoader loader = ClassUtils.getDefaultClassLoader();
ClassLoader nonPublicLoader = null;
boolean hasNonPublicInterface = false;
// define proxy in class loader of non-public interface(s), if any
@SuppressWarnings("rawtypes")
Class>[] classObjs = new Class[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
Class> cl = Class.forName(interfaces[i], false, loader);
if ((cl.getModifiers() & Modifier.PUBLIC) == 0) {
if (hasNonPublicInterface) {
if (nonPublicLoader != cl.getClassLoader()) {
throw new IllegalAccessError("conflicting non-public interface class loaders");
}
} else {
nonPublicLoader = cl.getClassLoader();
hasNonPublicInterface = true;
}
}
classObjs[i] = cl;
}
try {
@SuppressWarnings("deprecation")
Class> proxyClass = Proxy.getProxyClass(hasNonPublicInterface ? nonPublicLoader : loader, classObjs);
return proxyClass;
} catch (IllegalArgumentException e) {
throw new ClassNotFoundException(null, e);
}
}
}