Add bytecode generator

This commit is contained in:
Taro L. Saito 2011-06-24 13:42:14 +09:00
parent d19a00ddb6
commit b531cb36dc

View File

@ -24,57 +24,104 @@
//-------------------------------------- //--------------------------------------
package org.xerial.snappy; package org.xerial.snappy;
import static org.junit.Assert.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.security.ProtectionDomain;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtNewMethod;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.junit.Test; import org.junit.Test;
import org.xerial.util.FileResource;
import org.xerial.util.log.Logger; import org.xerial.util.log.Logger;
public class SnappyLoaderTest public class SnappyLoaderTest
{ {
private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class); private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class);
// @Ignore public static BufferedInputStream openByteStream(Class< ? > referenceClass, String resourceFileName)
// @Test throws IOException {
// public void loadFromSytemClassLoader() throws Exception { URL url = FileResource.find(referenceClass, resourceFileName);
// if (url != null) {
// ClassLoader parent = this.getClass().getClassLoader().getParent(); return new BufferedInputStream(url.openStream());
// ClassWorld cw = new ClassWorld(); }
// ClassRealm L1 = cw.newRealm("l1", parent); else
// ClassRealm L2 = cw.newRealm("l2", parent); return null;
// }
// File nativeLib = SnappyLoader.findNativeLibrary();
// assertNotNull(nativeLib); public static <T> String loadIntoString(Class<T> referenceClass, String path) throws IOException {
// BufferedInputStream in = openByteStream(referenceClass, path);
// ClassPool pool = ClassPool.getDefault(); if (in == null)
// CtClass cl = pool.makeClass("org.xerial.snappy.SnappyNativeLoader"); throw new FileNotFoundException(
// cl.addField(CtField.make("static boolean isLoaded = false;", cl)); String.format("reference class:%s, path:%s", referenceClass.getName(), path));
// String m1 = FileResource.loadIntoString(SnappyLoaderTest.class, "load.code");
// String m2 = FileResource.loadIntoString(SnappyLoaderTest.class, "loadLibrary.code"); ByteArrayOutputStream buf = new ByteArrayOutputStream();
// cl.addMethod(CtNewMethod.make(m1, cl)); try {
// cl.addMethod(CtNewMethod.make(m2, cl)); byte[] tmp = new byte[4028];
// for (int readBytes = 0; (readBytes = in.read(tmp)) != -1;) {
// ProtectionDomain systemPD = System.class.getProtectionDomain(); buf.write(tmp, 0, readBytes);
// byte[] bytecode = cl.toBytecode(); }
// FileOutputStream f = new FileOutputStream("src/main/resources/org/xerial/snappy/SnappyNativeLoader.bytecode"); buf.flush();
// f.write(bytecode); return buf.toString();
// f.close(); }
// finally {
// //Class< ? > loaderClass = cl.toClass(parent, System.class.getProtectionDomain()); in.close();
// //_logger.info(cl.getName()); }
// //Class< ? > loaderClass = cl.toClass(); }
//
// Class< ? > classLoader = Class.forName("java.lang.ClassLoader"); @Test
// java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class, public void loadFromSytemClassLoader() throws Exception {
// byte[].class, int.class, int.class, ProtectionDomain.class });
// ClassLoader parent = this.getClass().getClassLoader().getParent();
// defineClass.setAccessible(true); ClassWorld cw = new ClassWorld();
// defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain()); ClassRealm L1 = cw.newRealm("l1", parent);
// ClassRealm L2 = cw.newRealm("l2", parent);
// Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader");
// _logger.info(forName.toString()); File nativeLib = SnappyLoader.findNativeLibrary();
// assertNotNull(nativeLib);
// //Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found
// //_logger.info(snappyClass.getName()); ClassPool pool = ClassPool.getDefault();
// CtClass cl = pool.makeClass("org.xerial.snappy.SnappyNativeLoader");
// } cl.addField(CtField.make("static boolean isLoaded = false;", cl));
String m1 = loadIntoString(SnappyLoaderTest.class, "load.code");
String m2 = loadIntoString(SnappyLoaderTest.class, "loadLibrary.code");
cl.addMethod(CtNewMethod.make(m1, cl));
cl.addMethod(CtNewMethod.make(m2, cl));
ProtectionDomain systemPD = System.class.getProtectionDomain();
byte[] bytecode = cl.toBytecode();
FileOutputStream f = new FileOutputStream("target/SnappyNativeLoader.bytecode");
f.write(bytecode);
f.close();
//Class< ? > loaderClass = cl.toClass(parent, System.class.getProtectionDomain());
//_logger.info(cl.getName());
//Class< ? > loaderClass = cl.toClass();
Class< ? > classLoader = Class.forName("java.lang.ClassLoader");
java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class,
byte[].class, int.class, int.class, ProtectionDomain.class });
defineClass.setAccessible(true);
defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain());
Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader");
//Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found
//_logger.info(snappyClass.getName());
}
@Test @Test
public void load() throws Exception { public void load() throws Exception {