Forum Stats

  • 3,760,188 Users
  • 2,251,661 Discussions
  • 7,871,018 Comments

Discussions

Can you break generics?

796386
796386 Member Posts: 117
edited Jun 7, 2010 10:46AM in Generics
Hi,
Generics as we all know enforce type saftey. But, it is easy to corrupt generics when you mix generics and non-generic types. For example:
		
List<Car> list = new ArrayList<Car>();
List list2 = new ArrayList();
list2.add("tony");
list = list2;
Car car = list.get(0);    // this will throw an exception.
Is there any way you can corrupt generics by mixing one generic type with another generic type, or is that absolutely and always impossible.
Thanks.
«1

Comments

  • 791266
    791266 Member Posts: 18,005
    beginner2 wrote:
    Hi,
    Generics as we all know enforce type saftey. But, it is easy to corrupt generics when you mix generics and non-generic types. For example:
    		
    List<Car> list = new ArrayList<Car>();
    List list2 = new ArrayList();
    list2.add("tony");
    list = list2;
    Car car = list.get(0);    // this will throw an exception.
    Is there any way you can corrupt generics by mixing one generic type with another generic type, or is that absolutely and always impossible.
    Thanks.
    Eh? I don't understand the question.
  • 791266
    791266 Member Posts: 18,005
    The code that you posted will give you compilation warnings.
  • 796386
    796386 Member Posts: 117
    No it won't. It will compile and throw a runtime error. Which is my point. The hole point of generics is to generate compile time errors instead of runtime exceptions.

    You can beat generics as I should if you mix generics and non generics type. I am wondering is there any way you can trick generics into thinking everything is type safe at compile time when it is not when everything is generified.
  • 791266
    791266 Member Posts: 18,005
    beginner2 wrote:
    No it won't. It will compile and throw a runtime error. Which is my point.
    It gives compilation warnings when I compile. Note that I didn't say errors.


    The hole point of generics is to generate compile time errors instead of runtime exceptions.

    You can beat generics as I should if you mix generics and non generics type. I am wondering is there any way you can trick generics into thinking everything is type safe at compile time when it is not when everything is generified.
    Yes you can, but what's the point?
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    edited Jun 4, 2010 4:45PM
    kajbj wrote:
    beginner2 wrote:
    You can beat generics as I should if you mix generics and non generics type. I am wondering is there any way you can trick generics into thinking everything is type safe at compile time when it is not when everything is generified.
    Yes you can
    Really? How?
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    beginner2 wrote:
    You can beat generics as I should if you mix generics and non generics type. I am wondering is there any way you can trick generics into thinking everything is type safe at compile time when it is not when everything is generified.
    It's not beating it. It still warns you. It's your fault for not listening, whether that's because you silence the warnings in your IDE, from your make scripts, or through a @SuppressWarnings tag.
  • 791266
    791266 Member Posts: 18,005
    endasil wrote:
    kajbj wrote:
    beginner2 wrote:
    You can beat generics as I should if you mix generics and non generics type. I am wondering is there any way you can trick generics into thinking everything is type safe at compile time when it is not when everything is generified.
    Yes you can
    Really? How?
    Note that I didn't say that you could do it without warnings (e.g):

    This (in use with SuppressWarnings) will blow up at runtime:
    List<Long> longList = new ArrayList<Long>();
    longList.add(Long.valueOf(1));
    List<String> stringList;
    List<?> objectList;
    objectList = longList;
    stringList = (List<String>) objectList;
    System.out.println("boom! " + stringList.get(0));
    This code (that is using reflection and generics) will blow up at runtime, and you don't need a SuppressWarning.
    Class<? extends CharSequence> theClass = 
        Class.forName("java.util.ArrayList").asSubclass(CharSequence.class);		
    I'm sure that the are many other ways to also cause problems
  • 796386
    796386 Member Posts: 117
    Thanks for that. Good stuff.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    kajbj wrote:
    endasil wrote:
    kajbj wrote:
    beginner2 wrote:
    You can beat generics as I should if you mix generics and non generics type. I am wondering is there any way you can trick generics into thinking everything is type safe at compile time when it is not when everything is generified.
    Yes you can
    Really? How?
    Note that I didn't say that you could do it without warnings (e.g):

    This (in use with SuppressWarnings) will blow up...
    Ah. Then I respectfully suggest that you aren't tricking generics into thinking everything is type safe (it knows full well), but rather you're tricking yourself or your peers that everything is type safe :-).
  • 791266
    791266 Member Posts: 18,005
    endasil wrote:
    kajbj wrote:
    endasil wrote:
    kajbj wrote:
    beginner2 wrote:
    You can beat generics as I should if you mix generics and non generics type. I am wondering is there any way you can trick generics into thinking everything is type safe at compile time when it is not when everything is generified.
    Yes you can
    Really? How?
    Note that I didn't say that you could do it without warnings (e.g):

    This (in use with SuppressWarnings) will blow up...
    Ah. Then I respectfully suggest that you aren't tricking generics into thinking everything is type safe (it knows full well), but rather you're tricking yourself or your peers that everything is type safe :-).
    I think that is what the OP meant, since generics can't think :)
This discussion has been closed.