www.pudn.com > j2me_cldc-1_1-fcs-src-winunix.rar > Class.java
/* * Copyright © 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */ package java.lang; /** * Instances of the classClassrepresent classes and interfaces * in a running Java application. Every array also belongs to a class that is * reflected as aClassobject that is shared by all arrays with * the same element type and number of dimensions. * *
Classhas no public constructor. InsteadClass* objects are constructed automatically by the Java Virtual Machine as classes * are loaded. * *The following example uses a
Classobject to print the * class name of an object: * *
* * @author unascribed * @version 12/17/01 (CLDC 1.1) * @since JDK1.0, CLDC 1.0 */ public final class Class { /* * Constructor. Only the Java Virtual Machine creates Class * objects. */ private Class() {} /** * Converts the object to a string. The string representation is the * string "class" or "interface", followed by a space, and then by the * fully qualified name of the class in the format returned by ** void printClassName(Object obj) { * System.out.println("The class of " + obj + * " is " + obj.getClass().getName()); * } *getName. If thisClassobject represents a * primitive type, this method returns the name of the primitive type. If * thisClassobject represents void this method returns * "void". * * @return a string representation of this class object. */ public String toString() { return (isInterface() ? "interface " : "class ") + getName(); } /** * Returns theClassobject associated with the class * with the given string name. Given the fully-qualified name for * a class or interface, this method attempts to locate, load and * link the class. ** For example, the following code fragment returns the runtime *
Classdescriptor for the class named *java.lang.Thread: *
* Class t = Class.forName("java.lang.Thread")
* Class object for the class with the
* specified name.
* @exception ClassNotFoundException if the class could not be found.
* @exception Error if the function fails for any other reason.
* @since JDK1.0
*/
public static native Class forName(String className)
throws ClassNotFoundException;
/**
* Creates a new instance of a class.
*
* @return a newly allocated instance of the class represented by this
* object. This is done exactly as if by a new
* expression with an empty argument list.
* @exception IllegalAccessException if the class or initializer is
* not accessible.
* @exception InstantiationException if an application tries to
* instantiate an abstract class or an interface, or if the
* instantiation fails for some other reason.
* @since JDK1.0
*/
public native Object newInstance()
throws InstantiationException, IllegalAccessException;
/**
* Determines if the specified Object is assignment-compatible
* with the object represented by this Class. This method is
* the dynamic equivalent of the Java language instanceof
* operator. The method returns true if the specified
* Object argument is non-null and can be cast to the
* reference type represented by this Class object without
* raising a ClassCastException. It returns false
* otherwise.
*
* Specifically, if this Class object represents a
* declared class, this method returns true if the specified
* Object argument is an instance of the represented class (or
* of any of its subclasses); it returns false otherwise. If
* this Class object represents an array class, this method
* returns true if the specified Object argument
* can be converted to an object of the array class by an identity
* conversion or by a widening reference conversion; it returns
* false otherwise. If this Class object
* represents an interface, this method returns true if the
* class or any superclass of the specified Object argument
* implements this interface; it returns false otherwise. If
* this Class object represents a primitive type, this method
* returns false.
*
* @param obj the object to check
* @return true if obj is an instance of this class
*
* @since JDK1.1
*/
public native boolean isInstance(Object obj);
/**
* Determines if the class or interface represented by this
* Class object is either the same as, or is a superclass or
* superinterface of, the class or interface represented by the specified
* Class parameter. It returns true if so;
* otherwise it returns false. If this Class
* object represents a primitive type, this method returns
* true if the specified Class parameter is
* exactly this Class object; otherwise it returns
* false.
*
*
Specifically, this method tests whether the type represented by the
* specified Class parameter can be converted to the type
* represented by this Class object via an identity conversion
* or via a widening reference conversion. See The Java Language
* Specification, sections 5.1.1 and 5.1.4 , for details.
*
* @param cls the Class object to be checked
* @return the boolean value indicating whether objects of the
* type cls can be assigned to objects of this class
* @exception NullPointerException if the specified Class parameter is
* null.
* @since JDK1.1
*/
public native boolean isAssignableFrom(Class cls);
/**
* Determines if the specified Class object represents an
* interface type.
*
* @return true if this object represents an interface;
* false otherwise.
*/
public native boolean isInterface();
/**
* Determines if this Class object represents an array class.
*
* @return true if this object represents an array class;
* false otherwise.
* @since JDK1.1
*/
public native boolean isArray();
/**
* Returns the fully-qualified name of the entity (class, interface, array
* class, primitive type, or void) represented by this Class
* object, as a String.
*
*
If this Class object represents a class of arrays, then
* the internal form of the name consists of the name of the element type
* in Java signature format, preceded by one or more "["
* characters representing the depth of array nesting. Thus:
*
*
* (new Object[3]).getClass().getName()
*
*
* returns "[Ljava.lang.Object;" and:
*
*
* (new int[3][4][5][6][7][8][9]).getClass().getName()
*
*
* returns "[[[[[[[I". The encoding of element type names
* is as follows:
*
*
* B byte
* C char
* D double
* F float
* I int
* J long
* Lclassname; class or interface
* S short
* Z boolean
*
*
* The class or interface name classname is given in fully
* qualified form as shown in the example above.
*
* @return the fully qualified name of the class or interface
* represented by this object.
*/
public native String getName();
/**
* Finds a resource with a given name in the application's
* JAR file. This method returns
* null if no resource with this name is found
* in the application's JAR file.
* * The resource names can be represented in two * different formats: absolute or relative. *
* Absolute format: *
/packagePathName/resourceName* Relative format: *
resourceName* In the absolute format, the programmer provides a fully * qualified name that includes both the full path and the * name of the resource inside the JAR file. In the path names, * the character "/" is used as the separator. *
* In the relative format, the programmer provides only
* the name of the actual resource. Relative names are
* converted to absolute names by the system by prepending
* the resource name with the fully qualified package name
* of class upon which the getResourceAsStream
* method was called.
*
* @param name name of the desired resource
* @return a java.io.InputStream object.
*/
public java.io.InputStream getResourceAsStream(String name) {
try {
if (name.length() > 0 && name.charAt(0) == '/') {
/* Absolute format */
name = name.substring(1);
} else {
/* Relative format */
String className = this.getName();
int dotIndex = className.lastIndexOf('.');
if (dotIndex >= 0) {
name = className.substring(0, dotIndex + 1).replace('.', '/')
+ name;
}
}
return new com.sun.cldc.io.ResourceInputStream(name);
} catch (java.io.IOException x) {
return null;
}
}
/*
* This private function is used during virtual machine initialization.
* The user does not normally see this function.
*/
private static void runCustomCode() {}
}