很熟悉的示例吧!熟悉一下反射机制以及reflect的SDK
public class ViewClassInfoFrame extends JFrame implements ActionListener{
JTextField classnameField = new JTextField();
JButton viewinfoButton = new JButton();
JLabel hintLabel = new JLabel();
JTextArea infoTextArea = new JTextArea();
JScrollPane infoScrollPane = new JScrollPane();
TitledBorder titledBorder;
JPanel upPanel = new JPanel();
JPanel centerPanel = new JPanel();
BorderLayout mainFrameBorderLayout = new BorderLayout();
BorderLayout centerPanelBorderLayout = new BorderLayout();
BorderLayout upPanelBorderLayout = new BorderLayout();
/** 构造函数 */
public ViewClassInfoFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
init();
validate();
}
/**初始化窗口*/
private void init(){
classnameField.setFont(new Font("Dialog",0,15));
classnameField.setSelectedTextColor(Color.white);
classnameField.setText("");
viewinfoButton.setFont(new Font("Dialog",0,13));
viewinfoButton.setText("查看类详细信息");
viewinfoButton.addActionListener(this);
hintLabel.setFont(new Font("Dialog",0,13));
hintLabel.setText("类路径");
infoTextArea.setFont(new Font("Dialog",0,14));
infoTextArea.setEditable(false);
infoTextArea.setText("");
titledBorder = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134,134,134)),"结果信息");
infoScrollPane.setBorder(titledBorder);
infoScrollPane.getViewport().add(infoTextArea,null);
upPanel.setLayout(upPanelBorderLayout);
centerPanel.setLayout(centerPanelBorderLayout);
upPanel.add(hintLabel,BorderLayout.NORTH);
upPanel.add(classnameField,BorderLayout.CENTER);
upPanel.add(viewinfoButton,BorderLayout.SOUTH);
centerPanel.add(infoScrollPane);
this.getContentPane().setLayout(mainFrameBorderLayout);
this.setSize(new Dimension(450,360));
this.setTitle("使用反射机制查看类信息");
this.getContentPane().add(upPanel,BorderLayout.NORTH);
this.getContentPane().add(centerPanel,BorderLayout.CENTER);
this.getRootPane().setDefaultButton(viewinfoButton);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String classname = classnameField.getText();
StringBuffer buffer = new StringBuffer();
try {
//动态加载类 获取类对象
Class c = Class.forName(classname);
buffer.append("/**类声明**/ \n");
buffer.append(getClassStatement(c));
buffer.append("\n");
buffer.append("/**字段**/");
buffer.append(getFields(c));
buffer.append("/**构造函数**/");
buffer.append(getConstructors(c));
buffer.append("/**方法**/");
buffer.append(getMethods(c));
buffer.append("} \n");
} catch (Exception e1) {
JOptionPane.showMessageDialog(this, "没有找到类:" + e1.getMessage());
}
infoTextArea.setText(buffer.toString());
}
public String getClassStatement(Class c){
StringBuffer buffer = new StringBuffer();
if(c.getName().equals("java.lang.object")){
buffer.append("public class Ojbect{");
return buffer.toString();
}else {
String supername = c.getSuperclass().getName();
buffer.append("public class").append(c.getName());
buffer.append("extends").append(supername).append(";");
}
return buffer.toString();
}
/**
* 获取类的属性
* @param c
* @return
*/
public String getFields(Class c){
StringBuffer buffer = new StringBuffer();
Field field = null;
Field [] fields = c.getFields();
for (int i = 0; i < fields.length; i++) {
field = fields[i];
buffer.append(Modifier.toString(field.getModifiers())).append(" ");
Class type = field.getType();
buffer.append(type.getName()).append(" ");
buffer.append(field.getName()).append(";\n");//获取属性名
}
return buffer.toString();
}
/**
* 获取类的构造方法
* @param c
* @return String
*/
public String getConstructors(Class c){
StringBuffer buffer = new StringBuffer();
Constructor [] constructors = c.getDeclaredConstructors();
Constructor constructor = null;
for (int i = 0; i < constructors.length; i++) {
constructor = constructors[i];
buffer.append(Modifier.toString(constructor.getModifiers())).append(" ");
buffer.append(constructor.getName()).append("(");
//获取参数类型
Class[] paramsTypes= constructor.getParameterTypes();
Class class1= null;
for (int j = 0; j < paramsTypes.length; j++) {
class1 = paramsTypes[j];
if (j==(paramsTypes.length-1)) {
buffer.append(class1.getName());
}else {
buffer.append(class1.getName()).append(",");
}
}
buffer.append(")");
//获取方法声明的异常
Class[] exceTypes = constructor.getExceptionTypes();
for (int j = 0; j < exceTypes.length; j++) {
class1 = exceTypes[j];
if (j==0) {
buffer.append("throws");
}
if(j==(exceTypes.length-1)){
buffer.append(class1.getName());
} else {
buffer.append(class1.getName()).append(", ");
}
}
buffer.append("\n");
}
return buffer.toString();
}
/**
* 获取类的方法
* @param c
* @return String
*/
public String getMethods(Class c){
StringBuffer buffer = new StringBuffer();
Method[] methods= c.getMethods();
Method method = null;
for (int i = 0; i < methods.length; i++) {
method = methods[i];
buffer.append(Modifier.toString(method.getModifiers())).append(" ");
Class returnType = method.getReturnType();
buffer.append(returnType.getName()).append(" ");
buffer.append(method.getName()).append("(");
//获取方法参数
Class[] paramTypes =method.getParameterTypes();
Class class1 = null;
for (int j = 0; j < paramTypes.length; j++) {
class1 = paramTypes[j];
if (j==(paramTypes.length-1)) {
buffer.append(class1.getName());
} else {
buffer.append(class1.getName()).append(", ");
}
}
buffer.append(")");
//获取方法声明的异常
Class[] exceTypes = method.getExceptionTypes();
for (int j = 0; j < exceTypes.length; j++) {
class1 = exceTypes[j];
if (j==0) {
buffer.append("throws");
}
if(j==(exceTypes.length-1)){
buffer.append(class1.getName());
} else {
buffer.append(class1.getName()).append(", ");
}
}
buffer.append("\n");
}
return buffer.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
ViewClassInfoFrame frame = new ViewClassInfoFrame();
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);
}
}
分享到:
相关推荐
Java反射的demo案例,适合初学者理解,包含基础的各种反射(构造器、属性、方法等),本资源仅用于学习交流。
C#反射示例 自己写的一个反射示例,动态生成方法
c#中反射示例 vs2010开发环境,初学者可以看看。
VB.NET 反射 示例Demo(项目用)VB.NET 反射 示例Demo(项目用)适合新手
Java反射机制:反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能。
最基本的c# 反射 例子 示例 demo
Flex 反射示例 代码简洁明了
一个.net反射的示例,有兴趣的朋友可以看一下
通过源码演示java中反射的用法。 下载后直接导入eclipse工程。
反射示例,简单示例。仅供参考
C#反射的示例,里面全面的介绍了反射的使用方法。值得推荐给大家。
反射的示例程序,很容易懂!
关于Java反射的示例,使用Java描写,一个相对单一简单的例子。
C# 反射工厂示例C# 反射工厂示例C# 反射工厂示例C# 反射工厂示例C# 反射工厂示例C# 反射工厂示例C# 反射工厂示例C# 反射工厂示例C# 反射工厂示例C# 反射工厂示例
本文主要介绍了java使用dom4j读取配置文件实现抽象工厂和反射的示例,在Java中也可以同Donet一样,将差异配置在配置文件里面。另外,我们采用下面的方式实现,将会更加便捷
反射是一种允许用户获得类信息的C#功能,Type对象映射它代表的底层对象; 在.Net 中, 一旦获得了Type对象,就可以使用GetMethods()方法获取此类型支持的方法列表;该方法的两种形式: MethodInfo [] GetMethods() ...
Qt的反射开发实例,根据类名生成类对象,区别于qt的插件开发,插件是单例模式,通过单例再生成新的对象,而反射不是单例模式,每次生成的都是一个新的实例。
java反射机制+代码示例.rar 看一遍我就明白这是怎么一回事了~~
.net中反射和序列化的示例演,该示例比较简单,完整,实用,易懂。
自己写的一个简单的反射机制的示例,主要用来学习如何使用反射机制