All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.emul.java.lang.Class Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package java.lang;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * Generally unsupported. This class is provided so that the GWT compiler can
 * choke down class literal references.
 * 
 * @param  the type of the object
 */
public final class Class {

  private static final int PRIMITIVE = 0x00000001;
  private static final int INTERFACE = 0x00000002;
  private static final int ARRAY = 0x00000004;
  private static final int ENUM = 0x00000008;

  /**
   * Create a Class object for an array.
   * 
   * @skip
   */
  static  Class createForArray(String packageName, String className,
      String seedName, Class componentType) {
    // Initialize here to avoid method inliner
    Class clazz = new Class();
    setName(clazz, packageName, className, seedName);
    clazz.modifiers = ARRAY;
    clazz.superclass = Object.class;
    clazz.componentType = componentType;
    return clazz;
  }

  /**
   * Create a Class object for a class.
   * 
   * @skip
   */
  static  Class createForClass(String packageName, String className,
      String seedName, Class superclass) {
    // Initialize here to avoid method inliner
    Class clazz = new Class();
    setName(clazz, packageName, className, seedName);
    clazz.superclass = superclass;
    return clazz;
  }

  /**
   * Create a Class object for an enum.
   * 
   * @skip
   */
  static  Class createForEnum(String packageName, String className,
      String seedName, Class superclass,
      JavaScriptObject enumConstantsFunc, JavaScriptObject enumValueOfFunc) {
    // Initialize here to avoid method inliner
    Class clazz = new Class();
    setName(clazz, packageName, className, seedName);
    clazz.modifiers = (enumConstantsFunc != null) ? ENUM : 0;
    clazz.superclass = clazz.enumSuperclass = superclass;
    clazz.enumConstantsFunc = enumConstantsFunc;
    clazz.enumValueOfFunc = enumValueOfFunc;
    return clazz;
  }

  /**
   * Create a Class object for an interface.
   * 
   * @skip
   */
  static  Class createForInterface(String packageName, String className) {
    // Initialize here to avoid method inliner
    Class clazz = new Class();
    setName(clazz, packageName, className, null);
    clazz.modifiers = INTERFACE;
    return clazz;
  }

  /**
   * Create a Class object for a primitive.
   * 
   * @skip
   */
  static Class createForPrimitive(String packageName, String className,
      String seedName) {
    // Initialize here to avoid method inliner
    Class clazz = new Class();
    setName(clazz, packageName, className, seedName);
    clazz.modifiers = PRIMITIVE;
    return clazz;
  }

  static boolean isClassMetadataEnabled() {
    // This body may be replaced by the compiler
    return true;
  }

  static void setName(Class clazz, String packageName, String className,
      String seedName) {
    if (clazz.isClassMetadataEnabled()) {
      clazz.typeName = packageName + className;
    } else {
      /*
       * The initial "" + in the below code is to prevent clazz.hashCode() from
       * being autoboxed. The class literal creation code is run very early
       * during application start up, before class Integer has been initialized.
       */
      clazz.typeName = "Class$"
          + (seedName != null ? seedName : "" + clazz.hashCode());
    }
  }

  JavaScriptObject enumValueOfFunc;

  int modifiers;

  private Class componentType;

  @SuppressWarnings("unused")
  private JavaScriptObject enumConstantsFunc;

  private Class enumSuperclass;

  private Class superclass;

  private String typeName;

  /**
   * Not publicly instantiable.
   * 
   * @skip
   */
  private Class() {
  }

  public boolean desiredAssertionStatus() {
    // This body is ignored by the JJS compiler and a new one is
    // synthesized at compile-time based on the actual compilation arguments.
    return false;
  }

  public Class getComponentType() {
    return componentType;
  }

  public native T[] getEnumConstants() /*-{
    return [email protected]::enumConstantsFunc
        && ([email protected]::enumConstantsFunc)();
  }-*/;

  public String getName() {
    return typeName;
  }

  public Class getSuperclass() {
    if (isClassMetadataEnabled()) {
      return superclass;
    } else {
      return null;
    }
  }

  public boolean isArray() {
    return (modifiers & ARRAY) != 0;
  }

  public boolean isEnum() {
    return (modifiers & ENUM) != 0;
  }

  public boolean isInterface() {
    return (modifiers & INTERFACE) != 0;
  }

  public boolean isPrimitive() {
    return (modifiers & PRIMITIVE) != 0;
  }

  public String toString() {
    return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
        + getName();
  }

  /**
   * Used by Enum to allow getSuperclass() to be pruned.
   */
  Class getEnumSuperclass() {
    return enumSuperclass;
  }
}