反射是java中常见的技术,主要用于获取类中的field, method等。
User.java
package com.augmentum.bean;
import java.util.Date;
public class User {
private int id;
private int age;
private String name;
private Date bith;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
System.out.println("调用了该方法");
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBith() {
return bith;
}
public void setBith(Date bith) {
this.bith = bith;
}
public void mutilProperty(String str, int i){
System.out.println("这是调用了多参数");
}
}
TestBean.java
package com.augmentum.test;
import com.augmentum.bean.User;
public class TestBean {
public static void main(String[] args) {
User user = new User();
System.out.println(user.getClass());
System.out.println();
}
}
TestField.java
package com.augmentum.test;
import java.lang.reflect.Field;
import java.util.Date;
import com.augmentum.bean.User;
public class TestField {
public static void show(@SuppressWarnings("rawtypes") Class clazz) {
Field field[] = clazz.getDeclaredFields();
for (Field field2 : field) {
System.out.println(field2.getName());
System.out.println(field2.getType());
System.out.println();
}
}
@SuppressWarnings("rawtypes")
public static void showPropertyValue(Object obj) {
Class clazz = obj.getClass();
Field field[] = clazz.getDeclaredFields();
try {
for (Field field2 : field) {
field2.setAccessible(true);
System.out.println(field2.getName());
System.out.println(field2.get(obj));
System.out.println();
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
User user = new User();
user.setId(1);
user.setName("wangyan");
user.setBith(new Date());
user.setAge(23);
show(user.getClass());
//showPropertyValue(user);
}
}
TestMethod.java
package com.augmentum.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Date;
import com.augmentum.bean.User;
public class TestMetod {
@SuppressWarnings({ "rawtypes" })
public static void show(Object obj){
Class clazz = obj.getClass();
Method method[] = clazz.getDeclaredMethods();
for (Method method2 : method) {
System.out.println(method2.getName()); // 方法名
System.out.println(Modifier.toString(method2.getModifiers()));// 方
法修饰符
System.out.println(method2.getReturnType());// 方法返回值类型
Class parameterType[] = method2.getParameterTypes();
for (Class class1 : parameterType) {
System.out.println(class1.getName());// 方法参数列表
}
System.out.println();
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void operateMethod(Object obj){
Class clazz = obj.getClass();
try {
Method method = clazz.getDeclaredMethod("getName", null);
Method method2 = clazz.getDeclaredMethod("setName",
String.class);// 操作setName方法
Class multiClass[] = {String.class, int.class};
Method methodMutil = clazz.getDeclaredMethod("mutilProperty",
multiClass);// 操作了多参数方法
try {
method.invoke(obj, new Object[0]);
method2.invoke(obj, "lele");
Object objMe[] = {"str", 1};
methodMutil.invoke(obj, objMe);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
User user = new User();
user.setId(1);
user.setName("wangyan");
user.setBith(new Date());
user.setAge(23);
//show(user);
operateMethod(user);
System.out.println(user.getName());
}
}
Excel的导入导出
package com.augmentum.excel;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import com.augmentum.bean.User;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExcelUtils {
public static void excelOut(ArrayList arr, String str) throws
IllegalArgumentException, IllegalAccessException, RowsExceededException, WriteException,
IOException{
WritableWorkbook book = null;
try {
book = Workbook.createWorkbook(new File(str));
WritableSheet sheet = book.createSheet("sheet", 0);
for (int i=0;i<arr.size();i++) {
Object obj = arr.get(i);
Class clazz = obj.getClass();
Field fields[] = clazz.getDeclaredFields();
for(int j=0; j<fields.length; j++){
fields[j].setAccessible(true);
jxl.write.Label la = new jxl.write.Label(j, i,
String.valueOf(fields[j].get(obj)));
sheet.addCell(la);
}
}
book.write();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
book.close();
}
}
public static ArrayList<User> excelIn(Class clazz, String str) throws
InstantiationException {
ArrayList<User> userLists = new ArrayList<User>();
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File(str));
Sheet sheet = workbook.getSheet(0);
int rows = sheet.getRows();
Field fields[] = clazz.getDeclaredFields();
for(int i=0; i<rows; i++){
Object obj = clazz.newInstance();
for (int j=0;j<fields.length;j++) {
fields[j].setAccessible(true);
String cotent = sheet.getCell(j, i).getContents();
if(fields[j].getType().toString().equals("class
java.lang.String")){
fields[j].set(obj, cotent);
}
else if(fields[j].getType().toString().equals
("int")){
fields[j].setInt(obj, Integer.valueOf
(cotent));
}
else if(fields[j].getType().toString().equals
("class java.lang.Integer")){
fields[j].setInt(obj, Integer.valueOf
(cotent));
}
//class java.util.Date
if(fields[j].getType().toString().equals("class
java.util.Date")){
fields[j].set(obj, cotent+"");
}
}
userLists.add((User) obj);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
workbook.close();
}
return userLists;
}
public static void main(String[] args) throws RowsExceededException,
IllegalArgumentException, IllegalAccessException, WriteException, IOException,
InstantiationException {
// ArrayList<User> userLists = new ArrayList<User>();
// User user1 = new User();
// User user2 = new User();
// user1.setId(1);
// user1.setAge(21);
// user1.setName("wangyan");
// user1.setBith(new Date());
// user2.setId(1);
// user2.setAge(21);
// user2.setName("wangyan");
// user2.setBith(new Date());
// userLists.add(user1);
// userLists.add(user2);
// excelOut(userLists, "d:/test.xls");
User user = new User();
ArrayList<User> userLists = excelIn(user.getClass(), "d:/test.xls");
for (User user2 : userLists) {
System.out.println(user2.getName());
}
}
}