Skip to content

Commit b531cb3

Browse files
committed
Add bytecode generator
1 parent d19a00d commit b531cb3

1 file changed

Lines changed: 91 additions & 44 deletions

File tree

src/test/java/org/xerial/snappy/SnappyLoaderTest.java

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,57 +24,104 @@
2424
//--------------------------------------
2525
package org.xerial.snappy;
2626

27+
import static org.junit.Assert.*;
28+
29+
import java.io.BufferedInputStream;
30+
import java.io.ByteArrayOutputStream;
31+
import java.io.File;
32+
import java.io.FileNotFoundException;
33+
import java.io.FileOutputStream;
34+
import java.io.IOException;
35+
import java.net.URL;
36+
import java.security.ProtectionDomain;
37+
38+
import javassist.ClassPool;
39+
import javassist.CtClass;
40+
import javassist.CtField;
41+
import javassist.CtNewMethod;
42+
43+
import org.codehaus.plexus.classworlds.ClassWorld;
44+
import org.codehaus.plexus.classworlds.realm.ClassRealm;
2745
import org.junit.Test;
46+
import org.xerial.util.FileResource;
2847
import org.xerial.util.log.Logger;
2948

3049
public class SnappyLoaderTest
3150
{
3251
private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class);
3352

34-
// @Ignore
35-
// @Test
36-
// public void loadFromSytemClassLoader() throws Exception {
37-
//
38-
// ClassLoader parent = this.getClass().getClassLoader().getParent();
39-
// ClassWorld cw = new ClassWorld();
40-
// ClassRealm L1 = cw.newRealm("l1", parent);
41-
// ClassRealm L2 = cw.newRealm("l2", parent);
42-
//
43-
// File nativeLib = SnappyLoader.findNativeLibrary();
44-
// assertNotNull(nativeLib);
45-
//
46-
// ClassPool pool = ClassPool.getDefault();
47-
// CtClass cl = pool.makeClass("org.xerial.snappy.SnappyNativeLoader");
48-
// cl.addField(CtField.make("static boolean isLoaded = false;", cl));
49-
// String m1 = FileResource.loadIntoString(SnappyLoaderTest.class, "load.code");
50-
// String m2 = FileResource.loadIntoString(SnappyLoaderTest.class, "loadLibrary.code");
51-
// cl.addMethod(CtNewMethod.make(m1, cl));
52-
// cl.addMethod(CtNewMethod.make(m2, cl));
53-
//
54-
// ProtectionDomain systemPD = System.class.getProtectionDomain();
55-
// byte[] bytecode = cl.toBytecode();
56-
// FileOutputStream f = new FileOutputStream("src/main/resources/org/xerial/snappy/SnappyNativeLoader.bytecode");
57-
// f.write(bytecode);
58-
// f.close();
59-
//
60-
// //Class< ? > loaderClass = cl.toClass(parent, System.class.getProtectionDomain());
61-
// //_logger.info(cl.getName());
62-
// //Class< ? > loaderClass = cl.toClass();
63-
//
64-
// Class< ? > classLoader = Class.forName("java.lang.ClassLoader");
65-
// java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class,
66-
// byte[].class, int.class, int.class, ProtectionDomain.class });
67-
//
68-
// defineClass.setAccessible(true);
69-
// defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain());
70-
//
71-
// Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader");
72-
// _logger.info(forName.toString());
73-
//
74-
// //Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found
75-
// //_logger.info(snappyClass.getName());
76-
//
77-
// }
53+
public static BufferedInputStream openByteStream(Class< ? > referenceClass, String resourceFileName)
54+
throws IOException {
55+
URL url = FileResource.find(referenceClass, resourceFileName);
56+
if (url != null) {
57+
return new BufferedInputStream(url.openStream());
58+
}
59+
else
60+
return null;
61+
}
62+
63+
public static <T> String loadIntoString(Class<T> referenceClass, String path) throws IOException {
64+
BufferedInputStream in = openByteStream(referenceClass, path);
65+
if (in == null)
66+
throw new FileNotFoundException(
67+
String.format("reference class:%s, path:%s", referenceClass.getName(), path));
68+
69+
ByteArrayOutputStream buf = new ByteArrayOutputStream();
70+
try {
71+
byte[] tmp = new byte[4028];
72+
for (int readBytes = 0; (readBytes = in.read(tmp)) != -1;) {
73+
buf.write(tmp, 0, readBytes);
74+
}
75+
buf.flush();
76+
return buf.toString();
77+
}
78+
finally {
79+
in.close();
80+
}
81+
}
82+
83+
@Test
84+
public void loadFromSytemClassLoader() throws Exception {
85+
86+
ClassLoader parent = this.getClass().getClassLoader().getParent();
87+
ClassWorld cw = new ClassWorld();
88+
ClassRealm L1 = cw.newRealm("l1", parent);
89+
ClassRealm L2 = cw.newRealm("l2", parent);
90+
91+
File nativeLib = SnappyLoader.findNativeLibrary();
92+
assertNotNull(nativeLib);
93+
94+
ClassPool pool = ClassPool.getDefault();
95+
CtClass cl = pool.makeClass("org.xerial.snappy.SnappyNativeLoader");
96+
cl.addField(CtField.make("static boolean isLoaded = false;", cl));
97+
String m1 = loadIntoString(SnappyLoaderTest.class, "load.code");
98+
String m2 = loadIntoString(SnappyLoaderTest.class, "loadLibrary.code");
99+
cl.addMethod(CtNewMethod.make(m1, cl));
100+
cl.addMethod(CtNewMethod.make(m2, cl));
101+
102+
ProtectionDomain systemPD = System.class.getProtectionDomain();
103+
byte[] bytecode = cl.toBytecode();
104+
FileOutputStream f = new FileOutputStream("target/SnappyNativeLoader.bytecode");
105+
f.write(bytecode);
106+
f.close();
107+
108+
//Class< ? > loaderClass = cl.toClass(parent, System.class.getProtectionDomain());
109+
//_logger.info(cl.getName());
110+
//Class< ? > loaderClass = cl.toClass();
111+
112+
Class< ? > classLoader = Class.forName("java.lang.ClassLoader");
113+
java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class,
114+
byte[].class, int.class, int.class, ProtectionDomain.class });
115+
116+
defineClass.setAccessible(true);
117+
defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain());
118+
119+
Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader");
120+
121+
//Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found
122+
//_logger.info(snappyClass.getName());
123+
124+
}
78125

79126
@Test
80127
public void load() throws Exception {

0 commit comments

Comments
 (0)