1 /* 2 * Copyright 2010 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 java.lang; 17 18 import com.google.gwt.core.client.impl.Impl; 19 20 import java.io.PrintStream; 21 22 /** 23 * General-purpose low-level utility methods. GWT only supports a limited subset 24 * of these methods due to browser limitations. Only the documented methods are 25 * available. 26 */ 27 public final class System { 28 29 /** 30 * Does nothing in web mode. To get output in web mode, subclass PrintStream 31 * and call {@link #setErr(PrintStream)}. 32 */ 33 public static final PrintStream err = new PrintStream(null); 34 35 /** 36 * Does nothing in web mode. To get output in web mode, subclass 37 * {@link PrintStream} and call {@link #setOut(PrintStream)}. 38 */ 39 public static final PrintStream out = new PrintStream(null); 40 arraycopy(Object src, int srcOfs, Object dest, int destOfs, int len)41 public static void arraycopy(Object src, int srcOfs, Object dest, 42 int destOfs, int len) { 43 if (src == null || dest == null) { 44 throw new NullPointerException(); 45 } 46 47 Class<?> srcType = src.getClass(); 48 Class<?> destType = dest.getClass(); 49 if (!srcType.isArray() || !destType.isArray()) { 50 throw new ArrayStoreException("Must be array types"); 51 } 52 53 Class<?> srcComp = srcType.getComponentType(); 54 Class<?> destComp = destType.getComponentType(); 55 if (srcComp.modifiers != destComp.modifiers 56 || (srcComp.isPrimitive() && !srcComp.equals(destComp))) { 57 throw new ArrayStoreException("Array types must match"); 58 } 59 int srclen = getArrayLength(src); 60 int destlen = getArrayLength(dest); 61 if (srcOfs < 0 || destOfs < 0 || len < 0 || srcOfs + len > srclen 62 || destOfs + len > destlen) { 63 throw new IndexOutOfBoundsException(); 64 } 65 /* 66 * If the arrays are not references or if they are exactly the same type, we 67 * can copy them in native code for speed. Otherwise, we have to copy them 68 * in Java so we get appropriate errors. 69 */ 70 if ((!srcComp.isPrimitive() || srcComp.isArray()) 71 && !srcType.equals(destType)) { 72 // copy in Java to make sure we get ArrayStoreExceptions if the values 73 // aren't compatible 74 Object[] srcArray = (Object[]) src; 75 Object[] destArray = (Object[]) dest; 76 if (src == dest && srcOfs < destOfs) { 77 // TODO(jat): how does backward copies handle failures in the middle? 78 // copy backwards to avoid destructive copies 79 srcOfs += len; 80 for (int destEnd = destOfs + len; destEnd-- > destOfs;) { 81 destArray[destEnd] = srcArray[--srcOfs]; 82 } 83 } else { 84 for (int destEnd = destOfs + len; destOfs < destEnd;) { 85 destArray[destOfs++] = srcArray[srcOfs++]; 86 } 87 } 88 } else { 89 nativeArraycopy(src, srcOfs, dest, destOfs, len); 90 } 91 } 92 currentTimeMillis()93 public static long currentTimeMillis() { 94 return (long) currentTimeMillis0(); 95 }; 96 97 /** 98 * Has no effect; just here for source compatibility. 99 * 100 * @skip 101 */ gc()102 public static void gc() { 103 }; 104 identityHashCode(Object o)105 public static int identityHashCode(Object o) { 106 return (o == null) ? 0 : (!(o instanceof String)) ? Impl.getHashCode(o) 107 : String.HashCache.getHashCode((String) o); 108 } 109 setErr(PrintStream err)110 public static native void setErr(PrintStream err) /*-{ 111 @java.lang.System::err = err; 112 }-*/; 113 setOut(PrintStream out)114 public static native void setOut(PrintStream out) /*-{ 115 @java.lang.System::out = out; 116 }-*/; 117 currentTimeMillis0()118 private static native double currentTimeMillis0() /*-{ 119 return Date.now(); 120 }-*/; 121 122 /** 123 * Returns the length of an array via Javascript. 124 */ getArrayLength(Object array)125 private static native int getArrayLength(Object array) /*-{ 126 return array.length; 127 }-*/; 128 129 /** 130 * Copy an array using native Javascript. The destination array must be a real 131 * Java array (ie, already has the GWT type info on it). No error checking is 132 * performed -- the caller is expected to have verified everything first. 133 * 134 * @param src source array for copy 135 * @param srcOfs offset into source array 136 * @param dest destination array for copy 137 * @param destOfs offset into destination array 138 * @param len number of elements to copy 139 */ nativeArraycopy(Object src, int srcOfs, Object dest, int destOfs, int len)140 private static native void nativeArraycopy(Object src, int srcOfs, Object dest, int destOfs, 141 int len) /*-{ 142 // TODO(jgw): using Function.apply() blows up for large arrays (around 8k items at least). 143 if (src == dest && srcOfs < destOfs) { 144 srcOfs += len; 145 for (var destEnd = destOfs + len; destEnd-- > destOfs;) { 146 dest[destEnd] = src[--srcOfs]; 147 } 148 } else { 149 for (var destEnd = destOfs + len; destOfs < destEnd;) { 150 dest[destOfs++] = src[srcOfs++]; 151 } 152 } 153 154 // Array.prototype.splice.apply(dest, [destOfs, len].concat(src.slice(srcOfs, srcOfs + len))); 155 }-*/; 156 getProperty(String name)157 public static String getProperty(String name) { 158 if (name.equals("user.home")) { 159 return "/"; 160 } 161 return "Unknown property '" + name + "'"; 162 } 163 164 // TODO(jgw) exit(int code)165 public static native void exit(int code) /*-{ }-*/; 166 }