详细介绍java的反射技术
?
反射的定义:审查元数据并收集关于它的类型信息的能力.下面介绍java的反射技术.
Lesson: 检测类examing class
1.Retrieving Class Objects
获取一个Class对象(metadata)
a,从对象的实例获取.
Class c = mystery.getClass();//(return Class)
b,从子类的实例获取
TextField t = new TextField();
Class c = t.getClass();
Class s = c.getSuperclass();
c,知道类名,则可以把.class加入到名字之后来获取.
Class c = java.awt.Button.class;
d,如果类名在编译时是未知的,则可以使用Class.forName()方法来获取.
Class c = Class.forName(classString); 2.Getting the Class Name
获取类名称
c.getName();
例如:
import java.lang.reflect.*;
import java.awt.*;
class SampleName {
public static void main(String[] args)
{
Button b = new Button();
printName(b);
}
static void printName(Object o) {
Class c = o.getClass();
String s = c.getName();
System.out.println(s);
}
}
3.Discovering Class Modifiers
检索修改符
a.通过getModifiers()方法获取一个整型标识值.
b.通过java.reflect.Modifier对象的isPublic, isAbstract, 和 isFinal方法判断此值.
例如:
import java.lang.reflect.*;
import java.awt.*;
class SampleModifier
{
public static void main(String[] args)
{
String s = new String();
printModifiers(s);
}
public static void printModifiers(Object o)
{
Class c = o.getClass();
int m = c.getModifiers();
if (Modifier.isPublic(m))
System.out.println("public");
if (Modifier.isAbstract(m))
System.out.println("abstract");
if (Modifier.isFinal(m))
System.out.println("final");
}
}
4.Finding Superclasses
检索父类
例如:
import java.lang.reflect.*;
import java.awt.*;
class SampleSuper {
public static void main(String[] args)
{
Button b = new Button();
printSuperclasses(b);
}
static void printSuperclasses(Object o)
{
Class subclass = o.getClass();
Class superclass = subclass.getSuperclass();
?
while (superclass != null)
{
String className = superclass.getName();
System.out.println(className);
subclass = superclass;
superclass = subclass.getSuperclass();
}
}
}
5.Identifying the Interfaces Implemented by a Class
检索指定类实现的接口
例如:
import java.lang.reflect.*;
import java.io.*;
class SampleInterface
{
public static void main(String[] args)
{
try
{
RandomAccessFile r = new RandomAccessFile("myfile", "r");
printInterfaceNames(r);
}
catch (IOException e) {
System.out.println(e);
}
}
static void printInterfaceNames(Object o)
{
Class c = o.getClass();
Class[] theInterfaces = c.getInterfaces();
for (int i = 0; i < theInterfaces.length; i++)
{
String interfaceName = theInterfaces[i].getName();
System.out.println(interfaceName);
}
}
}
6.Examining Interfaces
判定一个类是不是接口
import java.lang.reflect.*;
import java.util.*;
class SampleCheckInterface
{
public static void main(String[] args)
{
Class thread = Thread.class;
Class runnable = Runnable.class;
verifyInterface(thread);
verifyInterface(runnable);
}
static void verifyInterface(Class c)
{
String name = c.getName();
if (c.isInterface())
{
System.out.println(name + " is an interface.");
} else {
System.out.println(name + " is a class.");
}
}
}
如:c.isInterface()
7.Identifying Class Fields
找出指定类所有的域成员
每个数据成员可以用java.reflect.Field来封闭其名称,类型,修改符的集合.也可以通过相应的方法获取或设置到该成员的值.
如:
import java.lang.reflect.*;
import java.awt.*;
class SampleField
{
public static void main(String[] args)
{
GridBagConstraints g = new GridBagConstraints();
printFieldNames(g);
}
static void printFieldNames(Object o)
{
Class c = o.getClass();
Field[] publicFields = c.getFields();
for (int i = 0; i < publicFields.length; i++)
{
String fieldName = publicFields[i].getName();
Class typeClass = publicFields[i].getType();
String fieldType = typeClass.getName();
System.out.println("Name: " + fieldName +
", Type: " + fieldType);
?
}
}
}
8.Discovering Class Constructors
检索指定类的构造函数
当创建一个类的实例时,是通过检造方法来作的,这种方法可以被重载.
每一个构造方法可以用类Constructor来描述,,包括名称,修饰符,参数类型(Class[]),和异常列表.
可以通过一个Class的getConstructors方法获取到该类的Constructor数组.
例程:
import java.lang.reflect.*;
import java.awt.*;
class SampleConstructor {? public static void main(String[] args) {? Rectangle r = new Rectangle();
showConstructors(r);
}
static void showConstructors(Object o) {? Class c = o.getClass();
Constructor[] theConstructors = c.getConstructors();
for (int i = 0; i < theConstructors.length; i++) {? System.out.print("( ");
Class[] parameterTypes =
theConstructors[i].getParameterTypes();
for (int k = 0; k < parameterTypes.length; k ++)
{
String parameterString = parameterTypes[k].getName();
System.out.print(parameterString + " ");
}
System.out.println(")");
}
}
}
9.Obtaining Method Information
检索方法
可以找到隶属于一个类的所有方法,通过getMethods包含Method数组,进而得到该方法的返回类型,修饰符,方法名称,参数列表
步骤:
a.指定类的Class Object
b.getMethods()获取Method[]对象
c,遍历该数组对象
例程:
import java.lang.reflect.*;
import java.awt.*;
class SampleMethod {
public static void main(String[] args)
{
Polygon p = new Polygon();
showMethods(p);
}
static void showMethods(Object o)
{
Class c = o.getClass();
Method[] theMethods = c.getMethods();
for (int i = 0; i < theMethods.length; i++)
{
String methodString = theMethods[i].getName();
System.out.println("Name: " + methodString);
String returnString =
theMethods[i].getReturnType().getName();
System.out.println(" Return Type: " + returnString);
Class[] parameterTypes = theMethods[i].getParameterTypes();
System.out.print(" Parameter Types:");
for (int k = 0; k < parameterTypes.length; k ++)
{
String parameterString = parameterTypes[k].getName();
System.out.print(" " + parameterString);
}
System.out.println();
}
}
}
希望通过以上内容的介绍,能够给你带来帮助.
1 楼 liyu1006 2011-08-20 好文章,好内容