Platform.java revision dbd967a6e5c96cc1a97c5521f88dc1564ba2f81b
1/*
2 * Copyright (C) 2008 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect.testing;
18
19import java.lang.reflect.Method;
20
21/**
22 * Methods factored out so that they can be emulated differently in GWT.
23 *
24 * <p>This class is emulated in GWT.
25 *
26 * @author Hayward Chan
27 */
28class Platform {
29
30  /**
31   * Calls {@link Class#isInstance(Object)}.  Factored out so that it can be
32   * emulated in GWT.
33   *
34   * <p>This method always returns {@code true} in GWT.
35   */
36  static boolean checkIsInstance(Class<?> clazz, Object obj) {
37    return clazz.isInstance(obj);
38  }
39
40  static <T> T[] clone(T[] array) {
41    return array.clone();
42  }
43
44  // Class.cast is not supported in GWT.  This method is a no-op in GWT.
45  static void checkCast(Class<?> clazz, Object obj) {
46    clazz.cast(obj);
47  }
48
49  static String format(String template, Object... args) {
50    return String.format(template, args);
51  }
52
53  /**
54   * Wrapper around {@link System#arraycopy} so that it can be emulated
55   * correctly in GWT.
56   *
57   * <p>It is only intended for the case {@code src} and {@code dest} are
58   * different.  It also doesn't validate the types and indices.
59   *
60   * <p>As of GWT 2.0, The built-in {@link System#arraycopy} doesn't work
61   * in general case.  See
62   * http://code.google.com/p/google-web-toolkit/issues/detail?id=3621
63   * for more details.
64   */
65  static void unsafeArrayCopy(
66      Object[] src, int srcPos, Object[] dest, int destPos, int length) {
67    System.arraycopy(src, srcPos, dest, destPos, length);
68  }
69
70  static Method getMethod(Class<?> clazz, String name) {
71    try {
72      return clazz.getMethod(name);
73    } catch (Exception e) {
74      throw new IllegalArgumentException(e);
75    }
76  }
77
78  static String classGetSimpleName(Class<?> clazz) {
79    return clazz.getSimpleName();
80  }
81
82  private Platform() {}
83}
84