generics

Why can't you have multiple interfaces in a bounded wildcard generic?

穿精又带淫゛_ 提交于 2020-01-26 11:23:48
问题 I know there's all sorts of counter-intuitive properties of Java's generic types. Here's one in particular that I don't understand, and which I'm hoping someone can explain to me. When specifying a type parameter for a class or interface, you can bound it so that it must implement multiple interfaces with public class Foo<T extends InterfaceA & InterfaceB> . However, if you're instantiating an actual object, this doesn't work anymore. List<? extends InterfaceA> is fine, but List<? extends

Why can't you have multiple interfaces in a bounded wildcard generic?

a 夏天 提交于 2020-01-26 11:23:37
问题 I know there's all sorts of counter-intuitive properties of Java's generic types. Here's one in particular that I don't understand, and which I'm hoping someone can explain to me. When specifying a type parameter for a class or interface, you can bound it so that it must implement multiple interfaces with public class Foo<T extends InterfaceA & InterfaceB> . However, if you're instantiating an actual object, this doesn't work anymore. List<? extends InterfaceA> is fine, but List<? extends

error in Dao class when using generics

主宰稳场 提交于 2020-01-25 23:22:22
问题 I am try centralize common methods used by my Dao classes in one Generic class, listed below: public class Dao<E> { private final E entity; @Autowired SessionFactory sessionFactory; protected Session getCurrentSession(){ return sessionFactory.getCurrentSession(); } public Dao(E entity) { this.entity = entity; } public E getEntity() { return this.entity; } @Transactional public boolean persist(E transientInstance) { try { sessionFactory.getCurrentSession().persist(transientInstance); return

Spring Batch reader using generics

拈花ヽ惹草 提交于 2020-01-25 18:24:27
问题 I'm just new in the spring architecture and I am wondering if it is possible to use a generic T (o what you want) in an ItemStreamReader . Something like this: public class Reader implements ItemStreamReader<T extends SomeClass>{ public T read() { ....... } public void open() { ....... } public void update() { ....... } public void close() { ....... } } So I pass to the reader various objects that extends SomeClass . 回答1: this should work: public class Reader<T extends SomeClass> implements

Limit generic type in subinterface in Java

喜夏-厌秋 提交于 2020-01-25 11:42:52
问题 Say I have this interface structure: public interface ItemRequestConverter<IR extends ItemRequest> { Target convert(IR request); } public interface CreateItemRequestConverter<CIR extends CreateItemRequest> extends ItemRequestConverter<ItemRequest> { } public class CreatePartRequestConverter implements CreateItemRequestConverter<CreatePartRequest> { @Override public Target convert(CreatePartRequest request) { ... } } Now I wondering whether this is possible at all. I doubt that. When I create

Limit generic type in subinterface in Java

与世无争的帅哥 提交于 2020-01-25 11:41:06
问题 Say I have this interface structure: public interface ItemRequestConverter<IR extends ItemRequest> { Target convert(IR request); } public interface CreateItemRequestConverter<CIR extends CreateItemRequest> extends ItemRequestConverter<ItemRequest> { } public class CreatePartRequestConverter implements CreateItemRequestConverter<CreatePartRequest> { @Override public Target convert(CreatePartRequest request) { ... } } Now I wondering whether this is possible at all. I doubt that. When I create

How to cast type safety a Object to JComboBox<String>?

断了今生、忘了曾经 提交于 2020-01-25 10:13:05
问题 I have this code: @Override public void itemStateChanged(ItemEvent evt) { if (evt.getStateChange() == ItemEvent.SELECTED) { Object sourceObject = evt.getSource(); if (sourceObject instanceof JComboBox<?>) { JComboBox<String> jComboBox = (JComboBox<String>) sourceObject; } } } What is the best and correct casting for generics type safety and avoiding suppress the warning? 回答1: You nailed it. Runtime instanceof checks don't check generic parameters so the way you have shown it is the standard

How to cast type safety a Object to JComboBox<String>?

二次信任 提交于 2020-01-25 10:12:27
问题 I have this code: @Override public void itemStateChanged(ItemEvent evt) { if (evt.getStateChange() == ItemEvent.SELECTED) { Object sourceObject = evt.getSource(); if (sourceObject instanceof JComboBox<?>) { JComboBox<String> jComboBox = (JComboBox<String>) sourceObject; } } } What is the best and correct casting for generics type safety and avoiding suppress the warning? 回答1: You nailed it. Runtime instanceof checks don't check generic parameters so the way you have shown it is the standard

Convert number type to corresponding string one

帅比萌擦擦* 提交于 2020-01-25 08:53:39
问题 I have some type with concrete number, for example type Num = 42; Now I want to make converting generic type NumberToString<N> = ... // N as string instead of number So that type A = NumberToString<Num>; // "42" type B = NumberToString<1>; // "1" How can I write such converter? 回答1: I don't think that's possible for arbitrary numbers. There's an open issue ostensibly dedicated to doing math in the type system, but nothing much is happening there as far as I can see. If you're okay limiting it

Determine class type of Generic typed field

旧城冷巷雨未停 提交于 2020-01-25 07:51:05
问题 I am getting NoSuchFieldException for the following piece of code: public class MultipleSorting<T> extends Observable { private SelectItem[] criteria1; private SelectItem[] order1; private SelectItem[] criteria2; private SelectItem[] order2; private SelectItem[] criteria3; private SelectItem[] order3; private T criteriaType; private T selectedCriteria1; private SortOrder selectedOrder1; private T selectedCriteria2; private SortOrder selectedOrder2; private T selectedCriteria3; private