Java?????????????????
???????????? ???????[ 2017/5/31 11:09:52 ] ??????????????????? Java
????????????????????????????????????????????????????????????????????
??????? ????????
???????????????????
??????? ????????
????????Java????????????????????????????????????????????δ???classes?????仰???Java???????????????????????????class???????????????(????????methods????)???????????????塢?????fields???????????methods??
??????????????Android Developer???????????????????÷???Ч???????????“??”???????????????????????????????????????????”?”???
???????????????????
????1???????????
??????????????е???????????Object????Object????hashCode()??equals()??clone()??toString()??getClass()???????getClass()???????Class ???????????????????Class???????C???д??????????????·???????Class????
????Class.forName(“?????????”) (??????????????????????????+????)
????????.class
???????????.getClass()
??????Class????????????????????
????public T newInstance() throws InstantiationException?? IllegalAccessException {
????return newInstanceImpl();
????}
????private native T newInstanceImpl() throws IllegalAccessException?? InstantiationException;
??????Api???????????newInstace?????????????class???????????????????????С??Demo
public class Reflection {
public static void main(String[] args) {
// ?????????????
People p1 = new People();
System.out.println(p1.getName());
// ???÷???????????
Class clazz = People.class;
// ???÷??????????????????????·????
// Class clazz = Class.forName("reflection.People");
// Class clazz = p1.getClass();
try {
People p2 = (People) clazz.newInstance();
System.out.println(p2.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class People {
private String name = "????";
private int 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;
}
}
????????????
????????
????????
????2?????????????????
??????????????????????????С?????JSON?????JavaBaen????????y??????????£???????????????
private void analysisDate(JSONObject response) throws JSONException {
int announceid = response.getInt("announceid");
String title = response.getString("title");
String hits = response.getString("hits");
String addtime = response.getString("addtime");
NewsNotifyItem newsNotifyItem = new NewsNotifyItem(announceid??
title?? hits?? addtime);
}
}
??????????????????????????????????javabean??????????????????????ν??е????????????????????????????????????????????????????????????????????÷?????????????Json?????????
????????????д?????JsonObject?????JavaBean?????????????????????JSON??key????????????????£???????·
???????????????????JavaBean?е???????????????
??????????set????
?????????????????Json??key??????????????????Json?е?????????????????
public class Json2BeanUtils {
public static <T> T jsonToBean(JSONObject response?? Class<T> clazz) {
try {
// ??????????
Object object = Class.forName(clazz.getName()).newInstance();
// ??????е????г??????
Field[] fields = object.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
//???????
fields[i].setAccessible(true);
// ?????ε?????
String fieldName = fields[i].getName();
// ?????UID
if (fieldName.endsWith("serialVersionUID")) {
continue;
}
// ?????ε?????
String fieldType = fields[i].getGenericType().toString();
// ????JavaBean?е?set???? ??????????? ???潲??
String methodName = "set"
+ fieldName.substring(0?? 1).toUpperCase()
+ fieldName.substring(1);
try {
// ?ж????????
if (fieldType.endsWith("class java.lang.String")) {
// ?????set????
Method m = object.getClass().getMethod(methodName??
String.class);
String value = null;
try {
// ??JsonObj???????????
value = response.getString(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = "";
}
if (TextUtils.isEmpty(value)) {
value = "";
} else if (value.endsWith("null")) {
value = "";
}
// ???
m.invoke(object?? value);
} else if (fieldType.endsWith("int")
|| fieldType.endsWith("class java.lang.Integer")) {
// int ????
System.out.println();
Method m = object.getClass().getMethod(methodName??
int.class);
int value = -1;
try {
value = response.getInt(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = -1;
}
m.invoke(object?? value);
} else if (fieldType.endsWith("boolean")
|| fieldType
.endsWith("fieldType:class java.lang.Boolean")) {
// boolean ????
Method m = object.getClass().getMethod(methodName??
boolean.class);
boolean value = false;
try {
value = response.getBoolean(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = false;
}
m.invoke(object?? value);
} else if (fieldType.endsWith("double")
|| fieldType
.endsWith("fieldType:class java.lang.Double")) {
// double ????
Method m = object.getClass().getMethod(methodName??
double.class);
double value = -1D;
try {
value = response.getDouble(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = -1D;
}
m.invoke(object?? value);
} else if (fieldType.endsWith("char")) {
// char???? JSONObject ???char
Method m = object.getClass().getMethod(methodName??
String.class);
String value = "";
try {
value = response.getString(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = "";
}
m.invoke(object?? value);
} else if (fieldType.endsWith("float")
|| fieldType
.endsWith("fieldType:class java.lang.Float")) {
// float????
Method m = object.getClass().getMethod(methodName??
double.class);
double value = -1D;
try {
value = response.getDouble(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = -1D;
}
m.invoke(object?? value);
} else if (fieldType.endsWith("short")
|| fieldType
.endsWith("fieldType:class java.lang.Short")) {
// short
Method m = object.getClass().getMethod(methodName??
short.class);
int value = -1;
try {
value = response.getInt(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = -1;
}
m.invoke(object?? value);
} else if (fieldType.endsWith("byte")
|| fieldType
.endsWith("fieldType:class java.lang.Byte")) {
Method m = object.getClass().getMethod(methodName??
byte.class);
int value = -1;
try {
value = response.getInt(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = -1;
}
m.invoke(object?? value);
} else if (fieldType.endsWith("long")
|| fieldType
.endsWith("fieldType:class java.lang.Long")) {
Method m = object.getClass().getMethod(methodName??
long.class);
Long value = -1L;
try {
value = response.getLong(fieldName);
} catch (Exception e) {
e.printStackTrace();
value = -1L;
}
m.invoke(object?? value);
}
} catch (Exception e) {
// TODO: handle exception
}
}
return (T) object;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11