当前位置: 首页 > 图文教程 > 网络编程 > JSP > java反射技术(一)

JSP
Java 创建cookie和删除cookie
jsp 从web.xml读取连接数据库的参数
jsp 不支持EL表达式,解决办法
jsp 获取客户端的浏览器和操作系统信息
struts2 session 解读
struts2 spring整合fieldError问题
jsp 生成验证码代码
搭建java WEB开发环境和应用
JSP 自定义标签
Java 区分文本中的中英文字符函数
通用JSP页面 jsp入门级文章
jsp struts1 标签实例详解
一个jdbc 测试程序代码
SSH整合中 hibernate托管给Spring得到SessionFactory
jsp SmartUpload 实现上传功能代码
jsp Unsupported encoding: gb2312 错误原因
java Struts2 在拦截器里的跳转问题
jsp 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)
Java 项目生成静态页面的代码
jdk与jre的区别 很形象,很清晰,通俗易懂

JSP 中的 java反射技术(一)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-04   浏览: 411 ::
收藏到网摘: n/a

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();
}
}
}