KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > shell > LowLevel


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

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 JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26
27 /**
28  * Various cross-platform low-level helper methods.
29  */

30 public class LowLevel {
31
32   public static final String JavaDoc PACKAGE_PATH = LowLevel.class.getPackage().getName().replace(
33       '.', '/').concat("/");
34
35   private static boolean sInitialized = false;
36
37   /**
38    * Clobbers a field on an object to which we do not have access.
39    */

40   public static void clobberFieldObjectValue(Class JavaDoc victimClass,
41       Object JavaDoc victimObject, String JavaDoc fieldName, Object JavaDoc value) {
42     Throwable JavaDoc rethrow = null;
43     try {
44       Field JavaDoc victimField = victimClass.getDeclaredField(fieldName);
45       victimField.setAccessible(true);
46       victimField.set(victimObject, value);
47       return;
48     } catch (IllegalArgumentException JavaDoc e) {
49       rethrow = e;
50     } catch (SecurityException JavaDoc e) {
51       rethrow = e;
52     } catch (IllegalAccessException JavaDoc e) {
53       rethrow = e;
54     } catch (NoSuchFieldException JavaDoc e) {
55       rethrow = e;
56     }
57     throw new RuntimeException JavaDoc("Unable to clobber field '" + fieldName
58         + "' from class " + victimClass.getName(), rethrow);
59   }
60
61   /**
62    * Clobbers a field on an object to which we do not have access.
63    */

64   public static void clobberFieldObjectValue(Object JavaDoc victim, String JavaDoc fieldName,
65       Object JavaDoc value) {
66     if (victim != null) {
67       clobberFieldObjectValue(victim.getClass(), victim, fieldName, value);
68     } else {
69       throw new NullPointerException JavaDoc("victim must not be null");
70     }
71   }
72
73   /**
74    * Deletes a global ref on the specified object, invalidating its handle.
75    */

76   public static void deleteGlobalRefInt(int globalRef) {
77     _deleteGlobalRefInt(globalRef);
78   }
79
80   /**
81    * Gets an environment variable. This is to replace the deprecated
82    * {@link System#getenv(java.lang.String)}.
83    */

84   public static String JavaDoc getEnv(String JavaDoc key) {
85     return _getEnv(key);
86   }
87
88   public static synchronized void init() {
89     if (!sInitialized) {
90       String JavaDoc libName = "gwt-ll";
91       try {
92         String JavaDoc installPath = Utility.getInstallPath();
93         try {
94           // try to make absolute
95
installPath = new File JavaDoc(installPath).getCanonicalPath();
96         } catch (IOException JavaDoc e) {
97           // ignore problems, failures will occur when the libs try to load
98
}
99         System.load(installPath + '/' + System.mapLibraryName(libName));
100       } catch (UnsatisfiedLinkError JavaDoc e) {
101         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc(sb.toString());
108       }
109       sInitialized = true;
110     }
111   }
112
113   /**
114    * Loads an image from the classpath.
115    */

116   public static Image loadImage(String JavaDoc name) {
117     ClassLoader JavaDoc cl = LowLevel.class.getClassLoader();
118     InputStream JavaDoc 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 JavaDoc e) {
127         }
128       }
129     } else {
130       // Bad image.
131
//
132
return new Image(null, 1, 1);
133     }
134   }
135
136   /**
137    * Creates a global ref on the specified object, returning its int handle.
138    */

139   public static int newGlobalRefInt(Object JavaDoc o) {
140     return _newGlobalRefInt(o);
141   }
142
143   /**
144    * Converts a global ref handle from {@link #newGlobalRefInt(Object)} into an
145    * Object. Use at your own risk.
146    */

147   public static Object JavaDoc objFromGlobalRefInt(int globalRef) {
148     return _objFromGlobalRefInt(globalRef);
149   }
150
151   /**
152    * Snatches a field from an object to which we do not have access.
153    */

154   public static Object JavaDoc snatchFieldObjectValue(Class JavaDoc victimClass,
155       Object JavaDoc victimObject, String JavaDoc fieldName) {
156     Throwable JavaDoc rethrow = null;
157     try {
158       Field JavaDoc victimField = victimClass.getDeclaredField(fieldName);
159       victimField.setAccessible(true);
160       return victimField.get(victimObject);
161     } catch (IllegalArgumentException JavaDoc e) {
162       rethrow = e;
163     } catch (SecurityException JavaDoc e) {
164       rethrow = e;
165     } catch (IllegalAccessException JavaDoc e) {
166       rethrow = e;
167     } catch (NoSuchFieldException JavaDoc e) {
168       rethrow = e;
169     }
170     throw new RuntimeException JavaDoc("Unable to snatch field '" + fieldName
171         + "' from class " + victimClass.getName(), rethrow);
172   }
173
174   /**
175    * Snatches a field from an object to which we do not have access.
176    */

177   public static Object JavaDoc snatchFieldObjectValue(Object JavaDoc victim, String JavaDoc fieldName) {
178     if (victim != null) {
179       return snatchFieldObjectValue(victim.getClass(), victim, fieldName);
180     } else {
181       throw new NullPointerException JavaDoc("victim must not be null");
182     }
183   }
184
185   // CHECKSTYLE_NAMING_OFF
186
private static native void _deleteGlobalRefInt(int globalRef);
187
188   private static native String JavaDoc _getEnv(String JavaDoc key);
189
190   private static native int _newGlobalRefInt(Object JavaDoc o);
191
192   private static native Object JavaDoc _objFromGlobalRefInt(int globalRef);
193   // CHECKSTYLE_NAMING_ON
194

195   /**
196    * This class is not instantiable.
197    */

198   private LowLevel() {
199   }
200
201 }
202
Popular Tags