日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 获取_java获取类的信息

發布時間:2025/4/16 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 获取_java获取类的信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

packagecom.test;importjava.lang.reflect.Constructor;importjava.lang.reflect.Field;importjava.lang.reflect.Method;importjava.lang.reflect.Modifier;public classMain{/*獲取類的聲明*/

privateString getClassStatement(Class c){

StringBuffer buf= newStringBuffer();if(c.getName().equals("java.lang.Object")){

buf.append("public class Object {");returnbuf.toString();

}else{//得到該類的父類名

String superName =c.getSuperclass().getName();

buf.append("public class ").append(c.getName()).append(" extends ").append(superName).append(" {");returnbuf.toString();

}

}/*獲取類的屬性*/

privateString getFields(Class c){

StringBuffer buf= newStringBuffer();

Field[] fields=c.getDeclaredFields();for(Field field : fields){//獲取屬性的訪問修飾符//Modifier的一些信息http://www.it165.net/pro/html/201309/7203.html

buf.append(" ").append(Modifier.toString(field.getModifiers())).append(" ");

Class> type =field.getType();

buf.append(type.getName()).append(" ");

buf.append(field.getName()).append(";\n");

}returnbuf.toString();

}/*獲取類的所有構造方法*/

privateString getConstructors(Class c){

StringBuffer buf= newStringBuffer();//獲取類的構造方法

Constructor>[] cons =c.getDeclaredConstructors();for(Constructor con : cons){//獲取構造方法的訪問修飾符

buf.append(" ").append(Modifier.toString(con.getModifiers())).append(" ");//獲取構造方法的名稱

buf.append(con.getName()).append("(");//獲取構造方法的參數

Class>[] paramType =con.getParameterTypes();for(int i=0; i

buf.append(paramType[i].getName());

}else{

buf.append(", ").append(paramType[i].getName());

}

}

buf.append(")");//獲取方法聲明的異常

Class>[] excepTypes =con.getExceptionTypes();for(int i=0; i

buf.append(" throws ").append(excepTypes[i].getName());

}else{

buf.append(", ").append(excepTypes[i].getName());

}

}

buf.append(";\n");

}returnbuf.toString();

}privateString getMethods(Class c){

StringBuffer buf= newStringBuffer();

Method[] methods=c.getDeclaredMethods();for(Method method : methods){//獲取方法的訪問修飾符

buf.append(" ").append(Modifier.toString(method.getModifiers())).append(" ");//獲取方法的返回類型

Class> returnType =method.getReturnType();

buf.append(returnType.getName()).append(" ");

buf.append(method.getName()).append(" (");//獲取方法的名稱//獲取方法的參數類型

Class>[] paramTypes =method.getParameterTypes();for(int i=0; i

buf.append(paramTypes[i].getName());

}else{

buf.append(", ").append(paramTypes[i].getName());

}

}

buf.append(")");//獲取方法聲明的異常

Class>[] excepTypes =method.getExceptionTypes();for(int i=0; i

buf.append(" throws ").append(excepTypes[i].getName());

}else{

buf.append(", ").append(excepTypes[i].getName());

}

}

buf.append(";\n");

}returnbuf.toString();

}public voidgetClassMessage(){

StringBuffer buf= newStringBuffer();try{

Class> c = Class.forName("com.test.Main");

buf.append("/*類的聲明*/\n");

buf.append(getClassStatement(c));

buf.append("\n");

buf.append(" /*字段*/\n");

buf.append(getFields(c));

buf.append(" /*構造器*/\n");

buf.append(getConstructors(c));

buf.append(" /*方法*/\n");

buf.append(getMethods(c));

buf.append("}\n");

System.out.println(buf.toString());

}catch(ClassNotFoundException e) {

e.printStackTrace();

}

}public static void main(String[] args) throwsException{newMain().getClassMessage();

}

}

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的java 获取_java获取类的信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。