详细介绍java的反射技术

开发 后端
本文介绍的是java的反射技术,希望对你有帮助,一起来看。

反射的定义:审查元数据并收集关于它的类型信息的能力。下面介绍java的反射技术。

Lesson: 检测类examing class

1.Retrieving Class Objects

获取一个Class对象(metadata)

a,从对象的实例获取。

  1. Class c = mystery.getClass();//(return Class) 

b,从子类的实例获取

  1. TextField t = new TextField();   
  2. Class c = t.getClass();   
  3. Class s = c.getSuperclass(); 

c,知道类名,则可以把.class加入到名字之后来获取。

  1. Class c = java.awt.Button.class

d,如果类名在编译时是未知的,则可以使用Class.forName()方法来获取.

  1. Class c = Class.forName(classString); 

2.Getting the Class Name

获取类名称

  1. c.getName(); 

例如:

  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleName {  
  4. public static void main(String[] args) {  
  5. Button b = new Button();  
  6. printName(b);  
  7. }  
  8. static void printName(Object o) {  
  9. Class c = o.getClass();  
  10. String s = c.getName();  
  11. System.out.println(s);  
  12. }  

3.Discovering Class Modifiers

检索修改符

a.通过getModifiers()方法获取一个整型标识值。

b.通过java.reflect.Modifier对象的isPublic, isAbstract, 和 isFinal方法判断此值.

例如:

  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleModifier {  
  4. public static void main(String[] args) {  
  5. String s = new String();  
  6. printModifiers(s);  
  7. }  
  8. public static void printModifiers(Object o) {  
  9. Class c = o.getClass();  
  10. int m = c.getModifiers();  
  11. if (Modifier.isPublic(m))  
  12. System.out.println("public");  
  13. if (Modifier.isAbstract(m))  
  14. System.out.println("abstract");  
  15. if (Modifier.isFinal(m))  
  16. System.out.println("final");  
  17. }  

4.Finding Superclasses 

检索父类

例如:

  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleSuper {  
  4. public static void main(String[] args) {  
  5. Button b = new Button();  
  6. printSuperclasses(b);  
  7. }  
  8. static void printSuperclasses(Object o) {  
  9. Class subclass = o.getClass();  
  10. Class superclass = subclass.getSuperclass();  
  11. while (superclass != null) {  
  12. String className = superclass.getName();  
  13. System.out.println(className);  
  14. subclass = superclass;  
  15. superclass = subclass.getSuperclass();  
  16. }  
  17. }  

5.Identifying the Interfaces Implemented by a Class

检索指定类实现的接口

例如:

  1. import java.lang.reflect.*;  
  2. import java.io.*;  
  3. class SampleInterface {  
  4. public static void main(String[] args) {  
  5. try {  
  6. RandomAccessFile r = new RandomAccessFile("myfile""r");  
  7. printInterfaceNames(r);  
  8. catch (IOException e) {  
  9. System.out.println(e);  
  10. }  
  11. }  
  12. static void printInterfaceNames(Object o) {  
  13. Class c = o.getClass();  
  14. Class[] theInterfaces = c.getInterfaces();  
  15. for (int i = 0; i < theInterfaces.length; i++) {  
  16. String interfaceName = theInterfaces[i].getName();  
  17. System.out.println(interfaceName);  
  18. }  
  19. }  

6.Examining Interfaces

判定一个类是不是接口

  1. import java.lang.reflect.*;  
  2. import java.util.*;  
  3. class SampleCheckInterface {  
  4. public static void main(String[] args) {  
  5. Class thread = Thread.class;  
  6. Class runnable = Runnable.class;  
  7. verifyInterface(thread);  
  8. verifyInterface(runnable);  
  9. }  
  10. static void verifyInterface(Class c) {  
  11. String name = c.getName();  
  12. if (c.isInterface()) {  
  13. System.out.println(name + " is an interface.");  
  14. else {  
  15. System.out.println(name + " is a class.");  
  16. }  
  17. }  

如:c.isInterface()

7.Identifying Class Fields

找出指定类所有的域成员

每个数据成员可以用java.reflect.Field来封闭其名称,类型,修改符的集合。也可以通过相应的方法获取或设置到该成员的值。

如:

  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleField {  
  4. public static void main(String[] args) {  
  5. GridBagConstraints g = new GridBagConstraints();  
  6. printFieldNames(g);  
  7. }  
  8. static void printFieldNames(Object o) {  
  9. Class c = o.getClass();  
  10. Field[] publicFields = c.getFields();  
  11. for (int i = 0; i < publicFields.length; i++) {  
  12. String fieldName = publicFields[i].getName();  
  13. Class typeClass = publicFields[i].getType();  
  14. String fieldType = typeClass.getName();  
  15. System.out.println("Name: " + fieldName +   
  16. ", Type: " + fieldType);  
  17. }  
  18. }  

8.Discovering Class Constructors

检索指定类的构造函数

当创建一个类的实例时,是通过检造方法来作的,这种方法可以被重载。

每一个构造方法可以用类Constructor来描述,,包括名称,修饰符,参数类型(Class[]),和异常列表。

可以通过一个Class的getConstructors方法获取到该类的Constructor数组。

例程:

  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleConstructor {  
  4. public static void main(String[] args) {  
  5. Rectangle r = new Rectangle();  
  6. showConstructors(r);  
  7. }  
  8. static void showConstructors(Object o) {  
  9. Class c = o.getClass();  
  10. Constructor[] theConstructors = c.getConstructors();  
  11. for (int i = 0; i < theConstructors.length; i++) {  
  12. System.out.print("( ");  
  13. Class[] parameterTypes =   
  14. theConstructors[i].getParameterTypes();  
  15. for (int k = 0; k < parameterTypes.length; k ++) {  
  16. String parameterString = parameterTypes[k].getName();  
  17. System.out.print(parameterString + " ");  
  18. }  
  19. System.out.println(")");  
  20. }  
  21. }  

9.Obtaining Method Information

检索方法

可以找到隶属于一个类的所有方法,通过getMethods包含Method数组,进而得到该方法的返回类型,修饰符,方法名称,参数列表

步骤:

a.指定类的Class Object

b.getMethods()获取Method[]对象

c,遍历该数组对象

例程:

  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleMethod {  
  4. public static void main(String[] args) {  
  5. Polygon p = new Polygon();  
  6. showMethods(p);  
  7. }  
  8. static void showMethods(Object o) {  
  9. Class c = o.getClass();  
  10. Method[] theMethods = c.getMethods();  
  11. for (int i = 0; i < theMethods.length; i++) {  
  12. String methodString = theMethods[i].getName();  
  13. System.out.println("Name: " + methodString);  
  14. String returnString =  
  15. theMethods[i].getReturnType().getName();  
  16. System.out.println(" Return Type: " + returnString);  
  17. Class[] parameterTypes = theMethods[i].getParameterTypes();  
  18. System.out.print(" Parameter Types:");  
  19. for (int k = 0; k < parameterTypes.length; k ++) {  
  20. String parameterString = parameterTypes[k].getName();  
  21. System.out.print(" " + parameterString);  
  22. }  
  23. System.out.println();  
  24. }  
  25. }  

希望通过以上内容的介绍,能够给你带来帮助。

责任编辑:于铁 来源: 互联网
相关推荐

2011-07-12 10:24:17

类加载反射

2009-06-29 14:30:27

JSF技术

2010-03-16 14:46:37

2021-11-26 07:31:43

Java反射程序

2011-07-22 16:37:01

java接口

2011-07-11 16:55:31

Java

2009-12-23 11:09:57

软交换技术

2010-03-18 17:39:30

低耗能无线技术

2011-07-11 11:02:12

JAVA集合框架

2010-03-18 14:27:53

Java Thread

2009-06-11 10:00:05

Java Socket

2011-07-22 17:41:02

java

2010-03-18 18:20:34

Java Socket

2011-07-11 15:02:54

枚举

2011-07-11 17:33:25

JAVA可移植性

2011-07-21 15:44:33

Java内部类

2011-07-21 13:51:38

java

2011-07-21 14:15:08

java

2009-12-31 16:38:56

VPN光纤接入技术

2010-09-09 14:25:32

点赞
收藏

51CTO技术栈公众号