Reflection Basics and Class Class

xiaoxiao2021-03-06  40

The standard J2SE platform libraries include a reflection API. This API allows classes to reflect on themselves, to see their inner selves. The reflection API lets you discover the name of classes, methods, and fields. You can also invoke those methods and access arrays WITHOUT USING SQUARE BRACKETS.

The heart of the reflection API is the Class class. This class allows you to find out the name of a class, its access modifier, fields, methods, and so forth. For any instance of a class, you can get its Class class by Calling the getclass method:

Class c = aninstance.getclass ();

If you don't happen to have an instance of class (and don't class to create one), Just Attach .class to the end of the class name and you have the class instance for what class.

Class c = myclass.class;

The Same Even Works for Primitives:

Class C = int.class;

This Last One Might Seem Odd, But it is a calling method..

ONE Thing Typically Done Is Create a class by passing its string name to the forname method of class.

Class C = Class.Forname ("java.awt.button);

.............. ..

ONCE You Have a class class, you can find out the name of the class with its getname method:

Class c = java.awt.button.class;

System.out.println ("Name:" C.GetName ());

OR, You CAN Find Out ITS Superclass With ITS GetSuperClass:

System.out.println ("Super:" C.getsuperClass (). Getname ());

Moving from classes to methods takes us to the Method class, found in the java.lang.reflect package. With the Method class, you can discover all methods of a class (with getMethods) and even invoke them (with invoke) .The following Program Demostrates Getting The Mothods of a Class:

Import java.lang.reflect. *;

Public class listmethods {

Public static void main (String [] args) {

IF (args.length == 0) {

System.err.Println ("Please include!");

Return;

}

For (int i = 0, n = args.lendth; i

ListMethods (Args [i]);

}

}

Private static void listmethods (string name) {

Try {

Class C = Class.Forname (Name);

System.out.println ("----" C.GetName () "----");

Method [] methods = c.getMethods ();

For (int i = 0, n = methods.lendth; i

System.out.println (Methods [i] .Getname ());

System.out.println ("/ T", Methods [i]);

}

} catch (classnotfoundexception e) {

System.out.println ("Bad ClassName: Name);

}

}

}

Notice that the output includes all methods available through its superclass, too. To limit the output to only those methods declared in the class itself, change the getMethods call to getDeclaredMethods.

While the getMethods of Class allows you to get all the methods in a class, more typically, you want a specific method of a class. For that, there's the getMethod (String name, Class [] types) method. Once you have that method You can INVOKE IT with the Invoke (Object Instance, Object [] args) Method. For Static Methods, The Instance Argument CAN Be Null.

The value for the arguments do not matter when finding a method, only the class types. At invoke time, you pass in the actual arguments values. For primitive types, you must box them up as objects (like using Integer for int). .

StringBuffer Buffer = New StringBuffer ("HELD");

Class C = buffer.getClass ();

Class [] Types = {INT.CLASS, STRING.CLASS};

Method method = C.getMethod ("INSERT", TYPES);

Object [] THEARGS = {New Integer (2), "LLO, Wor"};

Method.Invoke (Buffer, THEARGS);

转载请注明原文地址:https://www.9cbs.com/read-47663.html

New Post(0)