bounded-wildcard

Using Guice, how can I inject a bounded-wildcard class?

我的梦境 提交于 2019-12-21 23:41:00
问题 Using Guice, I want to inject a bounded-wildcard class. To be clear, I don't want to inject an object , but inject a class type . The would read: class A { Class<? extends SuperClass> a; @Inject A(Class<? extends SuperClass> a) { this.a = a.; } } How can I correctly bind the parameter? 回答1: Use this binding: bind(new TypeLiteral<Class<? extends SuperClass>>() {}) .toInstance(SubClass.class); 来源: https://stackoverflow.com/questions/8734826/using-guice-how-can-i-inject-a-bounded-wildcard-class

Java Generics with wildcard

别等时光非礼了梦想. 提交于 2019-12-21 22:29:46
问题 There is any way to fix this situation (I have try to simplyfy the scenario as much as i could): public class Test { public static void main(String[] args) { /* * HERE I would like to indicate that the CollectionGeneric can be of * something that extends Animal (but the constructor doesn't allow * wildcards) */ CollectionGeneric<? extends Animal> animalsCollectionGeneric = new CollectionGeneric<Animal>(); List<? extends Animal> animals = getAnimals(); /* Why I cannt do that? */

Mockito: Stubbing Methods That Return Type With Bounded Wild-Cards

倾然丶 夕夏残阳落幕 提交于 2019-12-20 08:04:21
问题 Consider this code: public class DummyClass { public List<? extends Number> dummyMethod() { return new ArrayList<Integer>(); } } public class DummyClassTest { public void testMockitoWithGenerics() { DummyClass dummyClass = Mockito.mock(DummyClass.class); List<? extends Number> someList = new ArrayList<Integer>(); Mockito.when(dummyClass.dummyMethod()).thenReturn(someList); //Compiler complains about this } } The compiler complains about the line that's trying to stub the behavior for

Is it possible to write a generic +1 method for numeric box types in Java?

本小妞迷上赌 提交于 2019-12-19 19:56:14
问题 This is NOT homework. Part 1 Is it possible to write a generic method, something like this: <T extends Number> T plusOne(T num) { return num + 1; // DOESN'T COMPILE! How to fix??? } Short of using a bunch of instanceof and casts, is this possible? Part 2 The following 3 methods compile: Integer plusOne(Integer num) { return num + 1; } Double plusOne(Double num) { return num + 1; } Long plusOne(Long num) { return num + 1; } Is it possible to write a generic version that bound T to only Integer

Java 8 Comparator comparing static function

倾然丶 夕夏残阳落幕 提交于 2019-12-19 06:15:23
问题 For the comparing source code in Comparator class public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); } I understand the difference between super and extends . What i dont understand is that why this method have them. Can someone give me an example on what cannot be achieved

Java bounded wildcard in return type

五迷三道 提交于 2019-12-18 05:37:19
问题 I've read in various places including here that having a bounded wildcard in a method return type is a bad idea. However, I can't find a way to avoid it with my class. Am I missing something? The situation looks something like this: class EnglishReaderOfPublications { private final Publication<? extends English> publication; EnglishReaderOfPublications(Publication<? extends English> publication) { this.publication = publication; } void readPublication() { publication.omNomNom(); } Publication

Difference of assignability with nested wildcards in Java 7/8 generics

旧巷老猫 提交于 2019-12-18 04:17:12
问题 The following compiles just fine in JDK8, but gives an incompatible types error with JDK7. List<List<? extends Number>> xs = Arrays.asList(Arrays.asList(0)); According to this answer, List<List<? extends Number>> doesn't have a supertype relationship to List<List<Integer>> . What changed in Java 8 that made this assignment work? I'm also having a hard time understanding why it doesn't work in Java 7. Both of these statements compile without type error using JDK7: List<? extends Number> xs =

Unbounded wildcards in Java

女生的网名这么多〃 提交于 2019-12-17 10:42:15
问题 Is there ever a difference between an unbounded wildcard e.g. <?> and a bounded wildcard whose bound is Object , e.g. <? extends Object> ? I recall reading somewhere that there was a difference in the early drafts of generics, but cannot find that source anymore. 回答1: As a point of pedntry, there is a difference if the class/interface/constructor/method declares a bound (other than extends Object ). interface Donkey<T extends Thing> { } ... Donkey<? extends Object> foo; // FAIL 回答2: From a

List<? extends MyType>

淺唱寂寞╮ 提交于 2019-12-17 05:04:09
问题 I have a Java question about generics. I declared a generic list: List<? extends MyType> listOfMyType; Then in some method I try instantiate and add items to that list: listOfMyType = new ArrayList<MyType>(); listOfMyType.add(myTypeInstance); Where myTypeInstance is just an object of type MyType ; it won't compile. It says: The method add(capture#3-of ? extends MyType) in the type List<capture#3-of ? extends MyType> is not applicable for the arguments (MyType) Any idea? 回答1: You cannot do a

What is the difference between bounded wildcard and type parameters?

六眼飞鱼酱① 提交于 2019-12-17 04:55:26
问题 Is there a difference between <N extends Number> Collection<N> getThatCollection(Class<N> type) and Collection<? extends Number> getThatCollection(Class<? extends Number>) 回答1: They expose different interfaces and contract for the method. The first declaration should return a collection whose elements type is the same of the argument class. The compiler infers the type of N (if not specified). So the following two statements are valid when using the first declaration: Collection<Integer> c1 =