反射练习

本文介绍了一个Java反射API的实战案例,展示了如何通过反射获取类的公共成员变量、构造函数及方法,并提供了完整的代码示例。适合希望深入了解Java反射机制的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

郁闷,浪费人感情,好不容易写了满满一篇,提交失败,重新再写!?

下面的例子是我做反射练习的一个例子,用来从一个类中获得public的成员变量,构造函数以及方法,

如果你想获得更private或protected的方法,请自己在代码中修改。

该类提供了2个接口,一个用来指定类名,如“java.util.ArrayList”

另一个就是执行的方法。public String getDatail()

(英文注释有点烂,望谅解,我这是个日文系统,没法写中文注释)

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * This class is used to find the Fields and Methods declared in
 * the underline class.
 * 
 * 
@author Administrator
 *
 
*/

public class FindClassInfo {
    
    
//store the outPut info
    private StringBuffer strInfo = null;
    
    
//store the class name
    private String strClassName = null;
    
    
private String strSpace = " ";
    
    
private String strNextLine = " ";
    
    
    
/**
     * Set the name of class,which will be showed datail info.
     * 
     * 
@param str
     
*/

    
public void setClassName(String str){
        strClassName 
= str;
    }

    
    
public String getDatail(){
        
if(strClassName == null){
            
return "Please set the Name of Class!";
        }

        Class c 
= null;
        
try {
            c 
= Class.forName(strClassName);
        }
 catch (ClassNotFoundException e) {
            
return "The Class can`t been find by the name!";
        }

        strInfo 
= new StringBuffer();
        strInfo.append(
"----------------------detail of Class:"+strClassName +"----------------------");
        strInfo.append(strNextLine);
        getFields(c);
        getConstructor(c);
        getMethods(c);
        getSuperClssInfo(c);
        
return strInfo.toString();
    }

    
    
/**
     * Get public Fields of Class.
     * 
@param c
     
*/

    
private void getFields(Class c){
        Field[] arrFields 
= c.getDeclaredFields();
        
for(Field tempField:arrFields){
            
//Scape the Private,Default and Protected Field
            if(!Modifier.isPublic(tempField.getModifiers())){
                
continue;
            }

            
            strInfo.append(Modifier.toString(tempField.getModifiers()));
            strInfo.append(strSpace);
            strInfo.append(forTypeName(tempField.getType().getName()));
            strInfo.append(strSpace);
            strInfo.append(forName(tempField.getName()));
            strInfo.append(strNextLine);
        }

    }


    
/**
     * Get public  Constructor.
     * 
@param c
     
*/

    
private void getConstructor(Class c){
        Constructor[] arrConstructor 
= c.getDeclaredConstructors();
        
for(Constructor tempCons : arrConstructor){
            
//Scape the Private,Default and Protected Constructor
            if(!Modifier.isPublic(tempCons.getModifiers())){
                
continue;
            }

            strInfo.append(Modifier.toString(tempCons.getModifiers()));
            strInfo.append(strSpace);
            strInfo.append(forName(tempCons.getName()));
            strInfo.append(
"(");
            Class[] arrParaTypes 
= tempCons.getParameterTypes();
            
for(int i=0;i<arrParaTypes.length;i++)