WEB开发网
开发学院WEB开发Jsp java反射功能 阅读

java反射功能

 2008-01-05 09:23:25 来源:WEB开发网   
核心提示:java反射包(java.lang.reflect)为我们提供一个强大的功能,利用它可以查出一个未知类所有的:数据字段,java反射功能,方法,构造器,即可查出这个类所有的信息,package reflectframe;import javax.swing.UIManager;import java.awt.*;pub

  java反射包(java.lang.reflect)为我们提供一个强大的功能,利用它可以查出一个未知类所有的:数据字段,方法,构造器。下面这个程序就是一个利用reflect包写的一个简单的GUI程序,在文本域里输入java标准类名(如:javax.swing.JButton),按执行按钮,即可查出这个类所有的信息。

package reflectframe;

import javax.swing.UIManager;
import java.awt.*;

public class Reflect { /*main()类
boolean packFrame = false;

//ConstrUCt the application
public Reflect() {
Frame1 frame = new Frame1();
//Validate frames that have PReset sizes
/*Pack frames that have useful preferred size info, e.g. from their layout*/
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Reflect();
}
}
/*主界面类*/
package reflectframe;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
import java.lang.reflect.*;

public class Frame1 extends JFrame {
JPanel contentPane;
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
TitledBorder titledBorder1;
JLabel jLabel2 = new JLabel();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
BorderLayout borderLayout1 = new BorderLayout();
XYLayout xYLayout1 = new XYLayout();

//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
Toolkit tk=Toolkit.getDefaultToolkit();
Image img=tk.getImage("status.gif");
Cursor cu=tk.createCustomCursor(img,new Point(10,10),"stick");
this.setCursor(cu);
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),"结果");
jTextField1.setFont(new java.awt.Font("Dialog", 0, 15));
jTextField1.setSelectedTextColor(Color.white);
jTextField1.setText("");
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(450, 361));
this.setTitle("Reflect");
this.addWindowListener(new Frame1_this_windowAdapter(this));
jButton1.setText("执行");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel1.setText("类名:");
jTextArea1.setFont(new java.awt.Font("Dialog", 0, 15));
jTextArea1.setEditable(false);
jTextArea1.setText("");
jScrollPane1.setBorder(titledBorder1);
jLabel2.setText(" ");
jPanel1.setLayout(xYLayout1);
jPanel2.setLayout(borderLayout2);
jPanel1.add(jTextField1, new XYConstraints(55, 5, 304, -1));
jPanel1.add(jLabel1, new XYConstraints(16, 8, -1, -1));
jPanel1.add(jButton1, new XYConstraints(374, 6, -1, -1));
jPanel2.add(jScrollPane1);
contentPane.add(jPanel1, BorderLayout.NORTH);
contentPane.add(jPanel2, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1, null);
this.getRootPane().setDefaultButton(jButton1);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jButton1_actionPerformed(ActionEvent e) {
String className=jTextField1.getText();
StringBuffer buf=new StringBuffer();
try{
Class c = Class.forName(className);
String superName=c.getSuperclass().getName(); /*得到该类的超类*/
buf.append(className+" extends "+superName+" { ");
buf.append(" \* 字段 *\ ");
buf.append(getFields(c));
buf.append(" \* 构造器 *\ ");
buf.append(getConstructors(c));
buf.append(" \* 方法 *\ ");
buf.append(getMethods(c));
buf.append("} ");
}catch(Exception et){
JOptionPane.showMessageDialog(this,"没找到该类:"+et.getMessage());}
jTextArea1.setText(buf.toString());
}
public String getFields(Class c){ /*得到该类的所有数据字段*/
String str="";
Field[] fields=c.getDeclaredFields();
for(int i=0;i<fields.length;i++){
Field f=fields[i];
str+=Modifier.toString(f.getModifiers())+" "; /*数据字段修饰符*/
Class type=f.getType();
str+=type.getName()+" "; /*数据字段名*/
str+=f.getName()+"; ";
}
return str;
}
public String getConstructors(Class c){ /*得到构造器*/
String str="";
Constructor[] cons=c.getDeclaredConstructors();
for(int i=0;i<cons.length;i++){
Constructor c1=cons[i];
str+=Modifier.toString(c1.getModifiers())+" "; /*得到修饰符*/
str+=c1.getName()+"("; /*c1.getName()构造器名*/
Class[] cla=c1.getParameterTypes();
for(int j=0;j<cla.length;j++){ /*得到构造器参数*/
if(j>0){
if(j==cla.length-1)str += cla[j].getName();
else str += cla[j].getName() + ", ";
}
}
str+="); ";
}
return str;
}
public String getMethods(Class c){ /*得到类里所有方法*/
String str="";
Method[] m=c.getMethods();
for(int i=0;i<m.length;i++){
str+=Modifier.toString(m[i].getModifiers())+" "; /*方法修饰符*/
Class cla=m[i].getReturnType();
str+=cla.getName()+" "; /*cla.getName()是方法名*/
str+=m[i].getName()+"(";
Class[] clb=m[i].getParameterTypes();
for(int j=0;j<clb.length;j++){ /*方法所有参数
if(j>0){
if (j == clb.length - 1)str += clb[j].getName();
else str += clb[j].getName() + ", ";
}
}
str+="); ";
}
return str;
}
}

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;

Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

class Frame1_this_windowAdapter extends java.awt.event.WindowAdapter {
Frame1 adaptee;

Frame1_this_windowAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
}
反射功能在javabeans中得到最为充分的利用,对beans的能力进行查询。

Tags:java 反射 功能

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接