package net.sf.joafip;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import net.sf.joafip.store.entity.classinfo.ClassInfo;
import net.sf.joafip.store.entity.objectio.ObjectAndPersistInfo;
import net.sf.joafip.store.service.bytecode.ClassLoaderNoImpl;
import net.sf.joafip.store.service.classinfo.ClassInfoFactory;
import net.sf.joafip.store.service.objectio.ObjectIOClassNotFoundException;
import net.sf.joafip.store.service.objectio.ObjectIODataCorruptedException;
import net.sf.joafip.store.service.objectio.ObjectIODataRecordNotFoundException;
import net.sf.joafip.store.service.objectio.ObjectIOException;
import net.sf.joafip.store.service.objectio.ObjectIOInvalidClassException;
import net.sf.joafip.store.service.objectio.ObjectIONotSerializableException;
import net.sf.joafip.store.service.objectio.manager.IObjectIOManagerForProxyObjectIO;
import net.sf.joafip.store.service.proxy.IProxyCallBack;
import net.sf.joafip.store.service.proxy.IProxyCallBackToImplement;
import asm.net.sf.joafip.store.service.proxy.Bob1EnhancedSrcDump;
public class MainLoadClass implements IProxyCallBackToImplement {
public static void main(final String[] args) {
final MainLoadClass main = new MainLoadClass();
try {
main.test();
} catch (Exception e) {
e.printStackTrace();
}
}
private MainLoadClass() {
super();
}
private void test() throws Exception {
final byte[] byteCode = Bob1EnhancedSrcDump.dump();
final OutputStream outputStream = new FileOutputStream(
"runtime/Bob1Enhanced.class");
outputStream.write(byteCode);
outputStream.close();
final Class<?> classLoaderClass = Class
.forName("java.lang.ClassLoader");
final Method defineClassMethod = classLoaderClass.getDeclaredMethod(
"defineClass", new Class[] { String.class, byte[].class,
Integer.TYPE, Integer.TYPE });
final ClassLoader classLoader = new ClassLoaderNoImpl();
// final ClassLoader classLoader = ClassLoader.getSystemClassLoader();
final Object[] args = new Object[] {
"net.sf.joafip.store.service.proxy.Bob1Enhanced", byteCode,
Integer.valueOf(0), Integer.valueOf(byteCode.length) };
defineClassMethod.setAccessible(true);
final Class<?> definedClass = (Class<?>) defineClassMethod.invoke(
classLoader, args);
if (definedClass == null) {
throw new Exception("failed define class");
}
/*
* create instance
*/
Object object = definedClass.newInstance();
System.out.println(System.identityHashCode(object));
/*
* test cast to IProxyCallBack
*/
IProxyCallBack proxyCallBack = (IProxyCallBack) object;
proxyCallBack.setProxyCallBack$JOAFIP$(this);
System.out.println(proxyCallBack.isLoaded$JOAFIP$());
}
@Override
public void forceLoad$JOAFIP$() throws ObjectIOException,
ObjectIODataRecordNotFoundException, ObjectIOInvalidClassException,
ObjectIOClassNotFoundException, ObjectIODataCorruptedException,
ObjectIONotSerializableException {
// no implementation
}
@Override
public void unload$JOAFIP$() {
// no implementation
}
@Override
public long getMyFileAccessSessionIdentifier$JOAFIP$() {
// no implementation
return 0;
}
@Override
public ClassInfo getProxiedClass$JOAFIP$() {
// no implementation
return null;
}
@Override
public void intercept$JOAFIP$() {
// no implementation
}
@Override
public void methodEnd$JOAFIP$() {
// no implementation
}
@Override
public boolean isLoaded$JOAFIP$() {
// no implementation
return false;
}
@Override
public void setLoaded$JOAFIP$(boolean loaded) throws ObjectIOException {
// no implementation
}
@Override
public ClassInfoFactory getClassInfoFactory$JOAFIP$() {
// no implementation
return null;
}
@Override
public void setInstance$JOAFIP$(ObjectAndPersistInfo objectAndPersistInfo) {
// no implementation
}
@Override
public IObjectIOManagerForProxyObjectIO getObjectIOManager$JOAFIP$() {
// no implementation
return null;
}
@Override
public void setLoading$JOAFIP$(boolean loading) {
// no implementation
}
}