import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.HashMap;


Reflection.java



public class Reflection {


public static void main(String[] args) {

String str = "myString";

HashMap<String, String> map = new HashMap<String, String>();

BizDto biz = new BizDto(); 

biz.setUserNm("홍길동");

biz.setUserpw("1234");

ArrayList<Object> arr = new ArrayList<Object>();

arr.add(map);

arr.add(biz);

arr.add(str);

for(Object o : arr ) {

if (o instanceof String ) {

System.out.println("String " + o.toString());

} else if (o instanceof Integer) {

System.out.println("String " + o.toString());

} else if (o instanceof HashMap) {

System.out.println("hash map");

} else {

Class<? extends Object> clazz = o.getClass();

String ClassName = clazz.getName();

Method[] methods = clazz.getMethods();

for(Method m : methods) {

String methodName = m.getName();

if(m.getName().indexOf("get") != -1 ) {

if(methodName.startsWith("get") 

&& methodName.toLowerCase().indexOf("dto") == -1 

&& methodName.toLowerCase().indexOf("entity") == -1 

&& !methodName.equalsIgnoreCase("getClass") ) {

           try {

            Object value =m.invoke(o);

            System.out.println("class : " + ClassName + ", methodName : " + methodName + ", value : " + value.toString());

           }

           catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {

               e.printStackTrace();

           }

}

}

}

} // end if

}


}


}


class BizDto implements java.io.Serializable {

private static final long serialVersionUID = 4347186401362125996L;

private String usernm = "";

private String userpw = "";

public void setUserNm(String _usernm) {

this.usernm = _usernm;

}

public String getUsernm() {

return this.usernm;

}

public void setUserpw(String _userpw) {

this.userpw = _userpw;

}

public String getUserpw() {

return this.userpw;

}


}

public void poi(HttpServletRequest req, HttpServletResponse resp) throws Exception {

try {

// 워크북 생성

HSSFWorkbook objWorkBook = new HSSFWorkbook();

// 워크시트 생성

HSSFSheet objSheet = objWorkBook.createSheet();

// 행생성

HSSFRow objRow = objSheet.createRow(0);

// 셀 생성

HSSFCell objCell = objRow.createCell(0);

HSSFCellStyle cs = objWorkBook.createCellStyle();

HSSFDataFormat df = objWorkBook.createDataFormat();

cs.setDataFormat(df.getFormat("#,##0.0")); //or cs.setDataFormat((short)7);

objCell.setCellValue(2655.32);

objCell.setCellStyle(cs);

objWorkBook.write(resp.getOutputStream());

OutputStream out = resp.getOutputStream();   

resp.setCharacterEncoding("utf-8");    

resp.setContentType("application/x-msexcel");    

resp.setHeader("Content-Disposition", "attachment;filename=test.xls");   

objWorkBook.write(out);   

out.close();

} catch(Exception e) {

}

}

+ Recent posts