package com.resoft.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
/**
* 显示一个类的方法、字段、构造函数
* @author zxs_9999
*
*/
public class ShowClassDetail {
private static final Logger logger =Logger.getLogger(ShowClassDetail.class);
public ShowClassDetail() {
// TODO Auto-generated constructor stub
}
public Object[] getDeclaredMethods(String className){
Class reflectionClass=null;
try {
reflectionClass=Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (Object[])reflectionClass.getDeclaredMethods();
}
public void showDeclaredMethods(String className){
int length=getDeclaredMethods(className).length;
Method methods[] =(Method[]) getDeclaredMethods(className);
for(int i=0;i<length;i++){
System.out.println("className:"+className+" "+methods[i].toString());
}
}
public Object[] getDeclaredMethods(Object object){
Class reflectionClass=object.getClass();
return reflectionClass.getDeclaredMethods();
}
public void showDeclaredMethods(Object object){
int length=getDeclaredMethods(object).length;
Method methods[] =(Method[]) getDeclaredMethods(object);
for(int i=0;i<length;i++){
System.out.println("className:"+object.toString()+" "+methods[i].toString());
}
}
public Object[] getConstructors(String className){
Class reflectionClass=null;
try {
reflectionClass=Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (Object[])reflectionClass.getConstructors();
}
public void showConstructors(String className){
int length=getConstructors(className).length;
Constructor constructor[] =(Constructor[]) getConstructors(className);
for(int i=0;i<length;i++){
System.out.println("className:"+className+" "+constructor[i].toString());
}
}
public Object[] getConstructors(Object object){
Class reflectionClass=object.getClass();
return reflectionClass.getConstructors();
}
public void showConstructors(Object object){
int length=getConstructors(object).length;
Constructor constructor[] =(Constructor[]) getConstructors(object);
for(int i=0;i<length;i++){
System.out.println("className:"+object.toString()+" "+constructor[i].toString());
}
}
public Object[] getDeclaredConstructor(String className)throws Exception{
Class reflectionClass=null;
try {
reflectionClass=Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (Object[]) (reflectionClass.getDeclaredConstructor(new Class[]{}).newInstance(new Object[]{}));
}
public void showDeclaredConstructor(String className) throws Exception{
int length=getDeclaredConstructor(className).length;
Constructor constructor[] =(Constructor[]) getDeclaredConstructor(className);
for(int i=0;i<length;i++){
System.out.println("className:"+className+" "+constructor[i].toString());
}
}
public Object[] getFields(String className){
Class reflectionClass=null;
try {
reflectionClass=Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (Object[])reflectionClass.getFields();
}
public void showFields(String className){
int length=getFields(className).length;
Field field[] =(Field[]) getFields(className);
for(int i=0;i<length;i++){
System.out.println("className:"+className+" "+field[i].toString());
}
}
public Object[] getFields(Object object){
Class reflectionClass=object.getClass();
return reflectionClass.getFields();
}
public void showFields(Object object){
int length=getFields(object).length;
Field field[] =(Field[]) getFields(object);
for(int i=0;i<length;i++){
System.out.println("className:"+object.toString()+" "+field[i].toString());
}
}
public Object[] getDeclaredFields(String className){
Class reflectionClass=null;
try {
reflectionClass=Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (Object[])reflectionClass.getDeclaredFields();
}
public void showgetDeclaredFields(String className){
int length=getDeclaredFields(className).length;
Field field[] =(Field[]) getDeclaredFields(className);
for(int i=0;i<length;i++){
System.out.println("className:"+className+" "+field[i].toString());
}
}
public Object[] getDeclaredFields(Object object){
Class reflectionClass=object.getClass();
return reflectionClass.getDeclaredFields();
}
public void showgetDeclaredFields(Object object){
int length=getDeclaredFields(object).length;
Field field[] =(Field[]) getDeclaredFields(object);
for(int i=0;i<length;i++){
System.out.println("className:"+object.toString()+" "+field[i].toString());
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ShowClassDetail().showDeclaredMethods(java.lang.Long.class);
new ShowClassDetail().showDeclaredMethods("java.lang.Long");
}
}