天天看點

java 反射 枚舉 執行個體_java中反射,枚舉實際應用例子

對于java中反射,枚舉的例子我們相信許多的朋友不怎麼清楚了,對于這個小編就來為各位介紹java中反射,枚舉實際應用例子,希望例子能夠幫助到各位。

遇到的問題

一個List的清單。。

清單中有枚舉類型

枚舉類型在頁面顯示的 時候就不能通過。屬性來取資料

用反射

根據名字去比對對應的類名 進而擷取對應的屬性值(反射)

jsp頁面

tld

/p>

PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"

"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

1.0

2.4

m

/tlds/MethodUtil.tld

This Tag Library makes user develope JSP with method Value component easily.

value

com.yq1012.MethodUtilTag

empty

className

true

true

className

method

true

true

method

paramType

false

true

paramType

param

false

true

param

path

false

true

反射幫助類

import java.io.IOException;

import java.lang.reflect.Method;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.SimpleTagSupport;

@SuppressWarnings("unused")

public class MethodUtilTag extends SimpleTagSupport {

private String className;

private String method;

private String paramType;

private String param;

private String path;

@Override

@SuppressWarnings("unchecked")

public void doTag() throws JspException, IOException {

JspWriter out = getJspContext().getOut();

Class[] parasClass = null;

Object[] args = null;

if(StringUtil.isNotEmpty(param)) {

if(param.contains(",")) {

String[] params = StringUtil.split(param, ",", false);

parasClass = new Class[params.length];

args = new Object[params.length];

for (int i = 0; i < parasClass.length; i ) {

parasClass[i] = String.class;

args[i] = params[i];

}

}

}

Class classType = String.class;

String value = "";

if("int".equals(paramType) || "Integer".equals(paramType)) {

classType = Integer.class;

} else if("double".equals(paramType) || "Double".equals(paramType)) {

classType = Double.class;

}

if(StringUtil.isNotEmpty(className) && StringUtil.isNotEmpty(method)) {

try {

String packagePath = this.className;

if(StringUtil.isNotEmpty(path)) {

packagePath = this.path "." packagePath;

} else {

packagePath = "這裡是類的包名字." packagePath;

}

Class clazz = Class.forName(packagePath);

Object newInstance = clazz.newInstance();

Method m = null;

Object object = null;

if(parasClass != null) {

m = clazz.getMethod(method, parasClass);

object = m.invoke(newInstance, args);

} else {

m = clazz.getMethod(method, classType);

object = m.invoke(newInstance, new Object[]{param});

}

value = object.toString();

} catch (Exception e) {

value = "";

e.printStackTrace();

}

}

out.print(value);

}

public String getClassName() {

return className;

}

public void setClassName(String className) {

this.className = className;

}

public String getMethod() {

return method;

}

public void setMethod(String method) {

this.method = method;

}

public String getParam() {

return param;

}

public void setParam(String param) {

this.param = param;

}

public String getParamType() {

return paramType;

}

public void setParamType(String paramType) {

this.paramType = paramType;

}

public String getPath() {

return path;

}

public void setPath(String path) {

this.path = path;

}

}

反射轉換實體類

public class xxxUtil {

public String  convert(String value){

if ("yq1012".equals(value)) {

return 枚舉.getName();//枚舉的要導入對應的包名字

}  else {

return null;

}

}

}