我怎么能得到Class <! - ?扩展List <AlarmRule - >&gt; instatnce?

时间:2017-05-10 10:49:00

标签: java android

我需要获得Class&lt; ?扩展名单&lt; AlarmRule&gt; &GT;实例

这是我的代码:

  public static BoundedMatcher<Object, List<AlarmRule>> setBind() {
        Class<? extends List<AlarmRule>> clazz = null; // I need to give clazz a value,but i don't know how.
        return new BoundedMatcher<Object, List<AlarmRule>>(clazz) {
            @Override
            public void describeTo(Description description) {
                description.appendText("with item content: ");
            }

            @Override
            protected boolean matchesSafely(List<AlarmRule> list) {
             return    list.stream().anyMatch(alarmRule -> test_reminder_corn.equals(alarmRule.cron));
            }
        };
    }

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

在您的情况下,您需要在构造函数中传递List.class

但是,由于java类型擦除,任何List都可以传递给匹配器,因此您需要添加其他代码以确保列表实际包含AlarmRule个对象,否则您将得到{{1}在运行时。

答案 1 :(得分:1)

像这样写

public static BoundedMatcher<Object, List> setBind() {
    return new BoundedMatcher<Object, List>(List.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("with item content: ");
        }

        @Override
        protected boolean matchesSafely(List list) {
            // convert every object in list to AlarmRule
            return  false;
        }
    };
}