最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

使用反射對(duì)bean的collection屬性賦值

標(biāo)簽:
JQuery

反射对bean的collection属性赋值

对collection使用反射创建时会遇到不知道具体实现类型而烦恼

比如:

     class School{                private List classes ; //班级列表                public void setClasses(List classes){                    this.classes=classes;                }                pulbic List getClasses(){                    return this classes;                }     }Class<?>  schoolClazz=Class.forName(School.class.getTypeName());schoolClazz.getMethod("setClasses",argsClass);//这里argsClass应该是具体的实现类  很明确这里是需要Arraylist 但是如果是这样代码 class School{                private Collection classes ; //班级列表                public void setClasses(Collection classes){                    this.classes=classes;                }                pulbic Collection getClasses(){                    return this classes;                }     }//惨了 不知道实现类是什么了  应该是ArrayList.class 还是 HashSet.class ?没办法了?参考下spring里面的代码:public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity) {    Assert.notNull(collectionType, "Collection type must not be null");    if (collectionType.isInterface()) {        if (Set.class == collectionType || Collection.class == collectionType) {            return new LinkedHashSet<>(capacity);        }        else if (List.class == collectionType) {            return new ArrayList<>(capacity);        }        else if (SortedSet.class == collectionType || NavigableSet.class == collectionType) {            return new TreeSet<>();        }        else {            throw new IllegalArgumentException("Unsupported Collection interface: " + collectionType.getName());        }    }    else if (EnumSet.class == collectionType) {        Assert.notNull(elementType, "Cannot create EnumSet for unknown element type");        // Cast is necessary for compilation in Eclipse 4.4.1.        return (Collection<E>) EnumSet.noneOf(asEnumType(elementType));    }    else {        if (!Collection.class.isAssignableFrom(collectionType)) {            throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName());        }        try {            return (Collection<E>) ReflectionUtils.accessibleConstructor(collectionType).newInstance();        }        catch (Throwable ex) {            throw new IllegalArgumentException(                "Could not instantiate Collection type: " + collectionType.getName(), ex);        }    }}这里返回了具体的实现类  直接使用它的返回的具体实现类

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消