1 32 package org.jruby.test; 33 34 import java.io.ByteArrayOutputStream ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.lang.reflect.InvocationTargetException ; 38 import java.lang.reflect.Method ; 39 40 import org.jruby.Ruby; 41 import org.jruby.runtime.ThreadContext; 42 import org.jruby.runtime.builtin.IRubyObject; 43 44 47 public class TestHelper { 48 private String privateField = "pfValue"; 49 public String localVariable1; 50 51 public static void removeWarningsFromEclipse() { 54 TestHelper helper = new TestHelper("A"); 55 helper.privateMethod(); 56 TestHelper.staticPrivateMethod(); 57 } 58 59 private TestHelper(String x) { 60 privateField = x; 61 } 62 63 public TestHelper() { 64 } 65 66 private String privateMethod() { 67 return privateField; 68 } 69 70 private static String staticPrivateMethod() { 71 return "staticPM"; 72 } 73 74 public String identityTest() { 75 return "Original"; 76 } 77 78 83 public static String [] createArray(int i) { 84 return new String [i]; 85 } 86 87 90 public static void throwException() { 91 throw new RuntimeException ("testException"); 92 } 93 94 97 public static SomeInterface getInterfacedInstance() { 98 return new SomeImplementation(); 99 } 100 101 public static Object getLooslyCastedInstance() { 102 return new SomeImplementation(); 103 } 104 105 public static Object getNull() { 106 return null; 107 } 108 109 public static interface SomeInterface { 110 String doStuff(); 111 String dispatchObject(Object iObject); 112 } 113 114 private static class SomeImplementation implements SomeInterface { 115 public String doStuff() { 116 return "stuff done"; 117 } 118 119 public String dispatchObject(Object iObject) { 120 return iObject == null ? null : iObject.toString(); 121 } 122 } 123 124 125 public static Class loadAlternateClass() throws ClassNotFoundException { 126 AlternateLoader loader = new AlternateLoader(); 127 Class klass = loader.loadClass("org.jruby.test.TestHelper"); 128 return klass; 129 } 130 131 134 public static IRubyObject loadAndCall(IRubyObject self, String name, byte[] javaClass, String methodName) 135 throws Throwable { 136 Loader loader = new Loader(); 137 Class c = loader.loadClass(name, javaClass); 138 Method method = c.getMethod(methodName, new Class [] { Ruby.class, IRubyObject.class }); 139 Ruby runtime = self.getRuntime(); 140 ThreadContext tc = runtime.getCurrentContext(); 141 142 tc.pushRubyClass(self.getType()); 143 144 try { 145 return (IRubyObject) method.invoke(null, new Object [] { runtime, self }); 146 } catch (InvocationTargetException e) { 147 throw unrollException(e); 148 } finally { 149 tc.popRubyClass(); 150 } 151 } 152 153 private static Throwable unrollException(InvocationTargetException e) { 154 while (e.getCause() instanceof InvocationTargetException ) { 155 e = (InvocationTargetException ) e.getCause(); 156 } 157 return e.getCause(); 158 } 159 160 public static String getClassName(Class klass) { 161 return klass.getName(); 162 } 163 164 public static void throwTestHelperException() { 165 throw new TestHelperException(); 166 } 167 168 private static class Loader extends ClassLoader { 169 170 public Class loadClass(String name, byte[] javaClass) { 171 Class cl = defineClass(name, 172 javaClass, 173 0, 174 javaClass.length); 175 resolveClass(cl); 176 return cl; 177 } 178 179 } 180 private static class AlternateLoader extends ClassLoader { 181 182 protected Class findModClass(String name) throws ClassNotFoundException { 183 byte[] classBytes = loadClassBytes(name); 184 replace(classBytes, "Original", "ABCDEFGH"); 185 return defineClass(name, classBytes, 0, classBytes.length); 186 } 187 private void replace(byte[] classBytes, String find, String replaceWith) { 188 byte[] findBytes = find.getBytes(); 189 byte[] replaceBytes = replaceWith.getBytes(); 190 for (int i=0; i<classBytes.length; i++) { 191 boolean match = true; 192 for (int j=0; j<findBytes.length; j++) { 193 if (classBytes[i+j] != findBytes[j]) { 194 match = false; 195 break; 196 } 197 } 198 if (match) { 199 for (int j=0; j<findBytes.length; j++) 200 classBytes[i+j] = replaceBytes[j]; 201 return; 202 } 203 } 204 } 205 public Class loadClass(String name) throws ClassNotFoundException { 206 if (name.equals("org.jruby.test.TestHelper")) 207 return findModClass(name); 208 return super.loadClass(name); 209 } 210 private byte[] loadClassBytes(String name) throws ClassNotFoundException { 211 InputStream stream = null; 212 try { 213 String fileName = name.replaceAll("\\.", "/"); 214 fileName += ".class"; 215 byte[] buf = new byte[1024]; 216 ByteArrayOutputStream bytes = new ByteArrayOutputStream (); 217 int bytesRead = 0; 218 stream = getClass().getResourceAsStream("/" + fileName); 219 while ((bytesRead = stream.read(buf)) != -1) { 220 bytes.write(buf, 0, bytesRead); 221 } 222 return bytes.toByteArray(); 223 } catch (Exception e) { 224 e.printStackTrace(); 225 throw new ClassNotFoundException (e.getMessage(),e); 226 } finally { 227 if (stream != null) 228 try { 229 stream.close(); 230 } catch (IOException e1) { 231 e1.printStackTrace(); 232 } 233 } 234 } 235 } 236 237 private static class TestHelperException extends RuntimeException { 238 private static final long serialVersionUID = 3649034127816624007L; 239 } 240 } 241 | Popular Tags |