最近項目中遇到了Json數據自動獲取的功能,不然令人想起java的反射,已經很長時間沒復習java了正好一塊連java的這一塊內容一起過一遍。java中的反射無疑就相當於java開發者的春天,在眾多的框架中也能看到它的身影,可以在運行時檢查類,接口、變量和方法等信息,可以實例化調用方法以及設置變量值等。本文主要以代碼的形式直接將反射,泛型的運用展現出來。
java中的反射###
首先新建一個基礎類Author。
package bean;
/**
*
* @author Super~me
* Description: 基礎類
*
*/
public class Author {
private static String TAG="Big";
private String name;
private int age;
public Author(){}
public Author(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Author [name=" + name + ", age=" + age + "]";
}
private String pMethod(String t){
String result=t+" Private Method";
return result;
}
}
然后新建一個反射類,運用反射方法對上面的類進行訪問.包括對私有方法的訪問,對私有屬性的訪問等。其中常用的一些方法以及解釋:
//對象的創建
public static void reflectNewInstance(){
try {
Class<?> authorclass=Class.forName(path_reflectfrom);
Object object =authorclass.newInstance();
Author author=(Author) object;
author.setName("周大亨");
author.setAge(89);
System.out.println("author: "+author.toString());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//對私有的方法進行反射
public static void reflectPrivateConstructor(){
try {
Class<?> authorclass =Class.forName(path_reflectfrom);
Constructor<?> declaredConstructor =authorclass.getDeclaredConstructor(String.class,int.class);
declaredConstructor.setAccessible(true);
Object object=declaredConstructor.newInstance("lida",88);
Author author=(Author) object;
System.out.println( "Author: "+author.toString());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//反射私有的屬性
public static void reflectPrivateField(){
try {
Class<?> authorclass =Class.forName(path_reflectfrom);
Object authorobject=authorclass.newInstance();
Field field=authorclass.getDeclaredField("TAG");
field.setAccessible(true);
String tag=(String)field.get(authorobject);
System.out.println( "private field Tag:"+tag);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//反射獲取私有的方法
private static void reflectMethod(){
try {
Class<?> authorclass=Class.forName(path_reflectfrom);
Object authorobject=authorclass.newInstance();
Method authormethod=authorclass.getDeclaredMethod("pMethod", String.class);
authormethod.setAccessible(true);
String string=(String)authormethod.invoke(authorobject, TAG);
System.out.println( "private Method: "+string);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
通過控制台打印以上信息:查看運用結果
ReflectClass.reflectNewInstance();
ReflectClass.reflectPrivateField();
ReflectClass.reflectPrivateConstructor();
ReflectClass.reflectMethod();
運行結果:
泛型的運用###
對於泛型其實很好理解,通俗點講就是我們將類型也當成了參數進行傳值,這樣做代碼的安全性很大的被提升了,也為較大的優化帶來可能。泛型可以使編譯器知道一個對象的限定類型是什么,這樣編譯器就可以在一個高的程度上驗證這個類型消除了強制類型轉換 使得代碼可讀性好,減少了很多出錯的機會。但是也要記住泛型的規范,比如靜態的變量和方法不能引用泛型變量,我們也不能利用instanceof等方法對泛型的類型進行判斷,當然這樣做也毫無意義,重要的一點是泛型類不能繼承Exception或者Throwable。泛型的繼承中,不論子類是否為泛型類,所繼承和實現的父類接口都需要被指定。
常用的泛型類型變量:
E:元素(Element)
K:關鍵字(Key)
N:數字(Number)
T:類型(Type)
V:值(Value)
另外泛型界定的概念主要是指對泛型類型進行一個限定。比如:
public static <T extends String> T add(T str1, T str2) { return "";}
利用泛型和反射實現對json數據的保存###
//利用反射獲取json數據到java類
private static void getJson(){
try {
String json = "{\"name\":\"Miss王\",\"age\":79}";
JSONObject source=JSONObject.parseObject(json);
Class<?> aClass = Class.forName("bean.Author");
Object obj = aClass.newInstance();
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
System.out.println(source.getString(field.getName()));
if (field.getGenericType().toString().equals(String.class.toString())) {
field.set(obj, source.getString(field.getName()));
} else if (field.getGenericType().toString().equals(int.class.toString())) {
field.set(obj, source.getInteger(field.getName()));
}
}
Author author = (Author) obj;
System.out.print(author);
} catch (Exception e) {
e.printStackTrace();
}
}
我們想把以上的實現封裝起來,這時就用了泛型。
//泛型+反射實現json數據讀取到java類
public static <T> T getJsonClass(String json, Class<T> beanclass) {
try {
JSONObject jsonObject = JSONObject.parseObject(json);
Object obj = beanclass.newInstance();
//拿到所以元素
Field[] declaredFields = beanclass.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
if (field.getGenericType().toString().equals(String.class.toString())) {
String value=jsonObject.getString(field.getName());
if(value!=null){
field.set(obj,value);
System.out.println(value);
}
} else if (field.getGenericType().toString().equals(int.class.toString())) {
if(jsonObject.getInteger(field.getName())!=null)
field.set(obj,jsonObject.getInteger(field.getName()));
}
}
return (T) obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
調用實現:
public static void main(String[] args) {
// TODO Auto-generated method stub
String json = "{\"name\":\"李先生\",\"age\":82}";
//ReflectJson.getJson();
//解析json然后換成實體類
Author author=getJsonClass(json, Author.class);
System.out.print( author.toString());
}
運行結果: