Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 202
نمايش فراداده

Class<T>java.lang

Java 1.0serializable

This class represents a Java type. There is one Class object for each class that is loaded into the Java Virtual Machine, and, as of Java 1.1, there are special Class objects that represent the Java primitive types. The TYPE constants defined by Boolean, Integer, and the other primitive wrapper classes hold these special Class objects. Array types are also represented by Class objects in Java 1.1.

There is no constructor for this class. You can obtain a Class object by calling the getClass( ) method of any instance of the desired class. In Java 1.1 and later, you can also refer to a Class object by appending .class to the name of a class. Finally, and most interestingly, a class can be dynamically loaded by passing its fully qualified name (i.e., package name plus class name) to the static Class.forName( ) method. This method loads the named class (if it is not already loaded) into the Java interpreter and returns a Class object for it. Classes can also be loaded with a ClassLoader object.

The newInstance( ) method creates an instance of a given class; this allows you to create instances of dynamically loaded classes for which you cannot use the new keyword. Note that this method works only when the target class has a no-argument constructor. See newInstance( ) in java.lang.reflect.Constructor for a more powerful way to instantiate dynamically loaded classes. In Java 5.0, Class is a generic type and the type variable T specifies the type that is returned by the newInstance( ) method.

getName( ) returns the name of the class. getSuperclass( ) returns its superclass. isInterface( ) tests whether the Class object represents an interface, and getInterfaces( ) returns an array of the interfaces that this class implements. In Java 1.2 and later, getPackage( ) returns a Package object that represents the package containing the class. getProtectionDomain( ) returns the java.security.ProtectionDomain to which this class belongs. The various other get( ) and is( ) methods return other information about the represented class; they form part of the Java Reflection API, along with the classes in java.lang.reflect.

Java 5.0 adds a number of methods to support the new language features it defines. isAnnotation( ) tests whether a type is an annotation type. Class implements java.lang.reflect.AnnotatedElement in Java 5.0 and the getAnnotation( ) and related methods allow the retrieval of annotations (with runtime retention) on the class. isEnum( ) tests whether a Class object represents an enumerated type and getEnumConstants( ) returns an array of the constants defined by an enumerated type. getTypeParameters( ) returns the type variables declared by a generic type. getGenericSuperclass( ) and getGenericInterfaces( ) are the generic variants of the getSuperclass( ) and getInterfaces( ) methods, returning the generic type information that appears in the extends and implements clause of the class declaration. See java.lang.reflect.Type for more information.

Java 5.0 also adds methods that are useful for reflection on inner classes. isMemberClass( ) , isLocalClass( ), and isAnonymousClass( ) determine whether a Class represents one of these kinds of nested types. getEnclosingClass( ) , getEnclosingMethod( ), and getEnclosingConstructor( ) return the type, method, or constructor that an inner class is nested within. Finally, getSimpleName( ) returns the name of a type as it would appear in Java source code. This is typically more useful than the Java VM formatted names returned by getName( ).

Figure 10-10. java.lang.Class<T>

public final class

Class<T> implements Serializable, java.lang.reflect.GenericDeclaration, java.lang.reflect.Type, java.lang.reflect.AnnotatedElement { // No Constructor // Public Class Methods public static Class<?>

forName (String

className ) throws ClassNotFoundException;

1.2 public static Class<?>

forName (String

name , boolean

initialize , ClassLoader

loader ) throws ClassNotFoundException; // Public Instance Methods

5.0 public <U> Class<? extends U>

asSubclass (Class<U>

clazz );

5.0 public T

cast (Object

obj );

1.4 public boolean

desiredAssertionStatus ( );

5.0 public String

getCanonicalName ( );

1.1 public Class[ ]

getClasses ( ); public ClassLoader

getClassLoader ( );

1.1 public Class<?>

getComponentType ( ); native

1.1 public java.lang.reflect.Constructor<T>

getConstructor (Class ...

parameterTypes ) throws NoSuchMethodException, SecurityException

1.1 public java.lang.reflect.Constructor[ ]

getConstructors ( ) throws SecurityException;

1.1 public Class[ ]

getDeclaredClasses ( ) throws SecurityException;

1.1 public java.lang.reflect.Constructor<T>

getDeclaredConstructor (Class ...

parameterTypes ) throws NoSuchMethodException, SecurityException;

1.1 public java.lang.reflect.Constructor[ ]

getDeclaredConstructors ( ) throws SecurityException;

1.1 public java.lang.reflect.Field

getDeclaredField (String

name ) throws NoSuchFieldException, SecurityException;

1.1 public java.lang.reflect.Field[ ]

getDeclaredFields ( ) throws SecurityException;

1.1 public java.lang.reflect.Method

getDeclaredMethod (String

name , Class...

parameterTypes ) throws NoSuchMethodException, SecurityException;

1.1 public java.lang.reflect.Method[ ]

getDeclaredMethods ( ) throws SecurityException;

1.1 public Class<?>

getDeclaringClass ( ); native

5.0 public Class<?>

getEnclosingClass ( );

5.0 public java.lang.reflect.Constructor<?>

getEnclosingConstructor ( );

5.0 public java.lang.reflect.Method

getEnclosingMethod ( );

5.0 public T[ ]

getEnumConstants ( );

1.1 public java.lang.reflect.Field

getField (String

name ) throws NoSuchFieldException, SecurityException;

1.1 public java.lang.reflect.Field[ ]

getFields ( ) throws SecurityException;

5.0 public java.lang.reflect.Type[ ]

getGenericInterfaces ( );

5.0 public java.lang.reflect.Type

getGenericSuperclass ( ); public Class[ ]

getInterfaces ( ); native

1.1 public java.lang.reflect.Method

getMethod (String

name , Class...

parameterTypes ) throws NoSuchMethodException, SecurityException;

1.1 public java.lang.reflect.Method[ ]

getMethods ( ) throws SecurityException;

1.1 public int

getModifiers ( ); native public String

getName ( );

1.2 public Package

getPackage ( );

1.2 public java.security.ProtectionDomain

getProtectionDomain ( );

1.1 public java.net.URL

getResource (String

name );

1.1 public java.io.InputStream

getResourceAsStream (String

name );

1.1 public Object[ ]

getSigners ( ); native

5.0 public String

getSimpleName ( ); public Class<? super T>

getSuperclass ( ); native

5.0 public boolean

isAnnotation ( );

5.0 public boolean

isAnonymousClass ( );

1.1 public boolean

isArray ( ); native

1.1 public boolean

isAssignableFrom (Class<?>

cls ); native

5.0 public boolean

isEnum ( );

1.1 public boolean

isInstance (Object

obj ); native public boolean

isInterface ( ); native

5.0 public boolean

isLocalClass ( );

5.0 public boolean

isMemberClass ( );

1.1 public boolean

isPrimitive ( ); native

5.0 public boolean

isSynthetic ( ); public T

newInstance ( ) throws InstantiationException, IllegalAccessException; // Methods Implementing AnnotatedElement

5.0 public <A extends java.lang.annotation.Annotation> A

getAnnotation (Class<A>

annotationClass );

5.0 public java.lang.annotation.Annotation[ ]

getAnnotations ( );

5.0 public java.lang.annotation.Annotation[ ]

getDeclaredAnnotations ( );

5.0 public boolean

isAnnotationPresent (Class<? extends java.lang.annotation. Annotation>

annotationClass ); // Methods Implementing GenericDeclaration

5.0 public java.lang.reflect.TypeVariable<Class<T>>[ ]

getTypeParameters ( ); // Public Methods Overriding Object public String

toString ( ); }

Passed To

Too many methods to list.

Returned By

Too many methods to list.

Type Of

Boolean.TYPE, Byte.TYPE, Character.TYPE, Double.TYPE, Float.TYPE, Integer.TYPE, Long.TYPE, Short.TYPE, Void.TYPE