1 16 package com.google.gwt.dev.shell; 17 18 import com.google.gwt.util.tools.Utility; 19 20 import org.eclipse.swt.graphics.Image; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.lang.reflect.Field ; 26 27 30 public class LowLevel { 31 32 public static final String PACKAGE_PATH = LowLevel.class.getPackage().getName().replace( 33 '.', '/').concat("/"); 34 35 private static boolean sInitialized = false; 36 37 40 public static void clobberFieldObjectValue(Class victimClass, 41 Object victimObject, String fieldName, Object value) { 42 Throwable rethrow = null; 43 try { 44 Field victimField = victimClass.getDeclaredField(fieldName); 45 victimField.setAccessible(true); 46 victimField.set(victimObject, value); 47 return; 48 } catch (IllegalArgumentException e) { 49 rethrow = e; 50 } catch (SecurityException e) { 51 rethrow = e; 52 } catch (IllegalAccessException e) { 53 rethrow = e; 54 } catch (NoSuchFieldException e) { 55 rethrow = e; 56 } 57 throw new RuntimeException ("Unable to clobber field '" + fieldName 58 + "' from class " + victimClass.getName(), rethrow); 59 } 60 61 64 public static void clobberFieldObjectValue(Object victim, String fieldName, 65 Object value) { 66 if (victim != null) { 67 clobberFieldObjectValue(victim.getClass(), victim, fieldName, value); 68 } else { 69 throw new NullPointerException ("victim must not be null"); 70 } 71 } 72 73 76 public static void deleteGlobalRefInt(int globalRef) { 77 _deleteGlobalRefInt(globalRef); 78 } 79 80 84 public static String getEnv(String key) { 85 return _getEnv(key); 86 } 87 88 public static synchronized void init() { 89 if (!sInitialized) { 90 String libName = "gwt-ll"; 91 try { 92 String installPath = Utility.getInstallPath(); 93 try { 94 installPath = new File (installPath).getCanonicalPath(); 96 } catch (IOException e) { 97 } 99 System.load(installPath + '/' + System.mapLibraryName(libName)); 100 } catch (UnsatisfiedLinkError e) { 101 StringBuffer sb = new StringBuffer (); 102 sb.append("Unable to load required native library '" + libName 103 + "'. Detailed error:\n"); 104 sb.append(e.getMessage() + ")\n\n"); 105 sb.append("Your GWT installation may be corrupt"); 106 System.err.println(sb.toString()); 107 throw new UnsatisfiedLinkError (sb.toString()); 108 } 109 sInitialized = true; 110 } 111 } 112 113 116 public static Image loadImage(String name) { 117 ClassLoader cl = LowLevel.class.getClassLoader(); 118 InputStream is = cl.getResourceAsStream(LowLevel.PACKAGE_PATH + name); 119 if (is != null) { 120 try { 121 Image image = new Image(null, is); 122 return image; 123 } finally { 124 try { 125 is.close(); 126 } catch (IOException e) { 127 } 128 } 129 } else { 130 return new Image(null, 1, 1); 133 } 134 } 135 136 139 public static int newGlobalRefInt(Object o) { 140 return _newGlobalRefInt(o); 141 } 142 143 147 public static Object objFromGlobalRefInt(int globalRef) { 148 return _objFromGlobalRefInt(globalRef); 149 } 150 151 154 public static Object snatchFieldObjectValue(Class victimClass, 155 Object victimObject, String fieldName) { 156 Throwable rethrow = null; 157 try { 158 Field victimField = victimClass.getDeclaredField(fieldName); 159 victimField.setAccessible(true); 160 return victimField.get(victimObject); 161 } catch (IllegalArgumentException e) { 162 rethrow = e; 163 } catch (SecurityException e) { 164 rethrow = e; 165 } catch (IllegalAccessException e) { 166 rethrow = e; 167 } catch (NoSuchFieldException e) { 168 rethrow = e; 169 } 170 throw new RuntimeException ("Unable to snatch field '" + fieldName 171 + "' from class " + victimClass.getName(), rethrow); 172 } 173 174 177 public static Object snatchFieldObjectValue(Object victim, String fieldName) { 178 if (victim != null) { 179 return snatchFieldObjectValue(victim.getClass(), victim, fieldName); 180 } else { 181 throw new NullPointerException ("victim must not be null"); 182 } 183 } 184 185 private static native void _deleteGlobalRefInt(int globalRef); 187 188 private static native String _getEnv(String key); 189 190 private static native int _newGlobalRefInt(Object o); 191 192 private static native Object _objFromGlobalRefInt(int globalRef); 193 195 198 private LowLevel() { 199 } 200 201 } 202 | Popular Tags |