11 Replies Latest reply: Jun 4, 2010 3:46 PM by 843793 RSS

    Can you break generics?

    796386
      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. Re: Can you break generics?
          791266
          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.
          • 2. Re: Can you break generics?
            791266
            The code that you posted will give you compilation warnings.
            • 3. Re: Can you break generics?
              796386
              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.
              • 4. Re: Can you break generics?
                791266
                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?
                • 5. Re: Can you break generics?
                  843793
                  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?
                  • 6. Re: Can you break generics?
                    843793
                    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.
                    • 7. Re: Can you break generics?
                      791266
                      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
                      • 8. Re: Can you break generics?
                        796386
                        Thanks for that. Good stuff.
                        • 9. Re: Can you break generics?
                          843793
                          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 :-).
                          • 10. Re: Can you break generics?
                            791266
                            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 :)
                            • 11. Re: Can you break generics?
                              843793
                              kajbj wrote:
                              I think that is what the OP meant, since generics can't think :)
                              Heh, that's true. But my point was that if you're going to suppress warnings, then that's the equivalent of asking "Can you trick a smoke alarm into thinking there's no fire underneath it?" and answering "Yes, just plug your ears and scream 'LA LA LA LA' as loud as you can..."

                              The Reflection example is something to watch out for, but doesn't really fall under type safety in my book since it's the Javadoc that declares that asSubclass can throw a ClassCastException. It's more that it's reusing ClassCastException as an exception, not that an "authentic" ClassCastException actually occurred. Since ClassCastExceptions can be publicly instantiatied, if that's not type safe, neither is this:
                              public Integer myInt = getObject();
                              
                              public Integer getObject() {
                                 throw new ClassCastException();
                              }