18

In a class without generic types I want to declare a rather complex generic field similar to these:

public class Client {
    private Map<Class<T extends Serializable>, List<Consumer<S extends T>>> classToConsumerTry1;

    private <T extends Serializable, S extends T> Map<Class<T>, List<Consumer<S>>> classToConsumerTry2;
}

promblem is the java compiler won't let me :)

So my question is how do I correctly introduce T and S without adding types to my class Client.

My goal is to enforce the Class being a subtype of Serializable and the Consumer being a subtype of the class you chose for Class.

succcubbus
  • 874
  • 1
  • 7
  • 24
  • Why don't you want to extend your class declaration? – Smutje Aug 14 '15 at 11:17
  • Because my `Map` should be able to contain different values for T and S not only the ones defined in the `Client` declaration. – succcubbus Aug 14 '15 at 11:23
  • Where are your Map members initialized? In a constructor? In some setter method? – Eran Aug 14 '15 at 11:26
  • 1
    I don't really understand your requirement - it is Java notion that you set generic field types on object creation, what do you want to achieve? – Smutje Aug 14 '15 at 11:28
  • Im recieving Serializables over a Socket and want to process them nicely like this: `classToConsumer.get(serial.getClass()).get(i).accept(serial);` – succcubbus Aug 14 '15 at 11:37
  • Besides the answers that state that you can't have a generic field in a non-generic class, `S extends T` is illegal and doesn't compile. And it doesn't make any sense, either. Why would you want a `Consumer` of something that extends something that extends `Serializable`? Just use `T` for your consumer and you'll be done. – fps Aug 14 '15 at 12:53

5 Answers5

11

You can't. Your only option is to declare the generic type parameters in your Client class declaration. If your Client class has no generic type parameters, its members can't be generic. You must use actual types in the declaration of your class members.

Eran
  • 387,369
  • 54
  • 702
  • 768
6

You have to somewhere introduce the type-parameter, so that you can use them in the definition for your class members.

Introducing a type-parameter can be done only on a class-level, or on a method-level. In your case, it should be on class-level:

public class Client<T extends Serializable, S extends T> {
    private Map<Class<T>, List<Consumer<S>>> classToConsumerTry1;

    private Map<Class<T>, List<Consumer<S>>> classToConsumerTry2;
}

This, however, implies that for both members (classToConsumerTry1 and classToConsumerTry2), T and S are the same. If you want them to be different, the you will have to get these two values from two different classes, both of which are parameterized with separate type-parameters.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Also, as additional step, this class can be declared as `protected class ClientImpl` and to hide generics, an exposed `public class Client extends ClientImpl` can be declared – semplar May 10 '19 at 21:13
4

You can not do it directly on the field, but you can maintain your types policy by generic methods (they can exist even in non-generic class):

@SuppressWarnings("rawtypes")
private Map<Class, List> map;

public <T extends Serializable> void put(Class<T> key, List<Consumer<? extends T>> value) {
    map.put(key, value);
}

@SuppressWarnings("unchecked")
public <T extends Serializable> List<Consumer<? extends T>> get(Class<T> key) {
    return map.get(key);
}
Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
2

As a note it is possible to declare a class type as a field (since google sends me here for this unrelated question...)

  Class<? extends MyInterface> myCurrentClass = MyImplementation.class;
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
1

You can't the way you want it done. But you can set as an Object and cast. Other hacky answers include using a List of <? extends Serializable> (but this only works for one extends) and then add() your item or an internal class that does something like:

private class Foo<T> {
        T obj;
}

so you can access/mutate obj internally.

AllDayAmazing
  • 2,383
  • 1
  • 24
  • 25