java - 關于泛型和反射的代碼錯誤(cannot select from a type variable)
問題描述
問題1:想寫個requestInfo的toString方法,把所有的成員變量都打印出來,子類就不用每次都寫個toString方法了,但是父類怎么獲取子類成員變量的值?
public class RequestInfo{ public String toString() {StringBuilder sb = new StringBuilder();Field[] fields = this.getClass().getDeclaredFields();for(Field field : fields){ sb.append(field.getName(), ' = ', (這里怎么獲取屬性值?), ';');}return ''; }}
問題2下面那個類P怎么實例化,也沒懂錯誤的原因,用P.getClass()還是不行
public abstract class AbstractService<Q extends RequestInfo, P extends ResponseInfo>{ public static final Logger LOGGER = LoggerFactory.getLogger(AbstractService.class); private String logTag; private P respBean; public P execute(Q reqBean) {init();LOGGER.info(StringUtil.appendStr('Request : {}, req = {}', logTag, reqBean.toString()));try{ if (checkInput(reqBean)) {handle(reqBean, respBean); } else {throw new Exception(StringUtil.appendStr(logTag, ' check input param invalid')); }}catch (Exception e){ LOGGER.error(StringUtil.appendStr(logTag, ' Exception: '), e);}return respBean; } protected void init() {logTag = getClass().getSimpleName();respBean =P.class.newInsance();//這里報錯,cannot select from a type variable } protected boolean checkInput(Q reqBean) {return true; } protected abstract void handle(Q reqBean, P respBean) throws Exception;}
問題解答
回答1:泛型,在編譯之后,就已經被擦除了,jvm根本看不到泛型的信息,這點是由于歷史遺留原因導致的,所以你說的p.getClass是不可能存在的
第一個問題,想法很好,但是,據我所知是無法實現的,對象是無法獲知子類的情況的,java的多態機制也只能是從父類或者父接口中查詢方法
我猜第一個問題你會覺得可以獲取子類的成員變量是覺得繼承之后子類的toString方法一執行,也會調用自己的this,這是錯誤的。在運行的時候,jvm會從父類的對象空間獲取這個方法并執行。所以,怎么搞都只是父類的成員變量
上述斜體的地方我說錯了,開了IDE測試了一下之后,對于自己之前理解的地方有誤,希望沒造成題主的困擾。下面貼的這段代碼,就可以循環獲取從子類到父類的所有變量。希望能有幫助
public String toString() {StringBuilder sb = new StringBuilder();Class clazz = this.getClass();while(clazz.getSuperclass() != null){ Field[] fields = clazz.getDeclaredFields(); try {for (Field field : fields) { field.setAccessible(true); sb.append(field.getName()).append('=').append(field.get(this)).append('n');} } catch (Exception e) {e.printStackTrace(); } clazz = clazz.getSuperclass();}return sb.toString(); }回答2:反射工具類
package cn.hylexus.app.util;import java.lang.reflect.Field;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class ReflectionUtils { public static List<Field> getFields(Class<?> clz) {List<Field> ret = new ArrayList<>();for (Class<?> c = clz; c != Object.class; c = c.getSuperclass()) { Field[] fields = c.getDeclaredFields(); ret.addAll(Arrays.asList(fields));}return ret; } /** * @param cls * 子類對應的Class * @param index * 子類繼承父類時傳入的索引,從0開始 * @return */ public static Class<?> getSuperClassGenericType(Class<?> cls, int index) {if (index < 0) return null;Type type = cls.getGenericSuperclass();if (!(type instanceof ParameterizedType)) return null;ParameterizedType parameterizedType = (ParameterizedType) type;Type[] typeArguments = parameterizedType.getActualTypeArguments();if (typeArguments == null || typeArguments.length == 0 || index > typeArguments.length - 1) return null;Type t = typeArguments[index];if (!(t instanceof Class)) { return null;}return (Class<?>) t; } public static Class<?> getSuperClassGenericType(Class<?> cls) {return getSuperClassGenericType(cls, 0); }}問題1
public class RequestInfo { @Override public String toString() {StringBuilder sb = new StringBuilder();//可以拿到多層次基礎的屬性List<Field> fields = ReflectionUtils.getFields(this.getClass());for (Field f : fields) { f.setAccessible(true); try {sb.append(f.getName()).append('=').append(f.get(this)).append('n'); } catch (Exception e) {e.printStackTrace(); }}return sb.toString(); }}問題2
@SuppressWarnings('unchecked') protected void init() {logTag = getClass().getSimpleName();try { //這里可以拿到動態綁定的Class信息 Class<?> clz = ReflectionUtils.getSuperClassGenericType(this.getClass(), 1); respBean = (P) clz.newInstance();} catch (Exception e) { e.printStackTrace();} }回答3:
第一個問題可以利用commons-beanutils做。
相關文章:
