第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

使用反射對bean的collection屬性賦值

標簽:
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);        }    }}这里返回了具体的实现类  直接使用它的返回的具体实现类

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

若覺得本文不錯,就分享一下吧!

評論

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

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

100積分直接送

付費專欄免費學

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

立即參與 放棄機會
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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

舉報

0/150
提交
取消