7 Replies Latest reply: Apr 9, 2007 4:47 PM by 3004 RSS

    unchecked cast *sigh*

    807599
      Here's my code:
      @Test
          public void testPrivateMethod ()
          {
              VccParser parser = new VccParser ();
              ArrayList<String> list = null;
              
              final Object params[] = {"NoSuchFile"};
              final Method trim[] = VccParser.class.getDeclaredMethods ();
              boolean found = false;
              
              for (int i = 0; i < trim.length && false == found; i++)
              {
                  String name = trim.getName ();
      if (name.equals ("trim"))
      {
      trim[i].setAccessible (true);
      try
      {
      list = (ArrayList<String>) trim[i].invoke (parser, params);
      found = true;
      }
      catch (IllegalArgumentException ex)
      {
      ex.printStackTrace();
      }
      catch (InvocationTargetException ex)
      {
      ex.printStackTrace();
      }
      catch (IllegalAccessException ex)
      {
      ex.printStackTrace();
      }
      }
      }
      assertEquals(true, list.isEmpty());
      }



      Here's my warning:

      warning: [unchecked] unchecked cast
      found : java.lang.Object
      required: java.util.ArrayList<java.lang.String>
      list = (ArrayList<String>) trim.invoke (parser, params);


      Any ideas on how to get rid of the warning?

      Thanks.
        • 1. Re: unchecked cast *sigh*
          807599
          @SuppressWarnings("unchecked")
          @Test
          public void testPrivateMethod ()   {
              //...
          }
          ~
          • 2. Re: unchecked cast *sigh*
            807599
            "How do I get rid of this warning?"

            "Just suppress it!"

            :O
            • 3. Re: unchecked cast *sigh*
              807599
              > "Just suppress it!"

              sigh

              As you wish...
              // ArrayList<String> list = null;
              List list = null;
              
              //...
              
              // list = (ArrayList<String>) trim.invoke (parser, params);
              list = (List) trim[i].invoke(parser, params);


              Either suppress the warning (which is perfectly acceptable), or don't cast to a typed list. Options abound.

              ~
              • 4. Re: unchecked cast *sigh*
                807599
                > "Just suppress it!"

                sigh

                As you wish...
                // ArrayList<String> list = null;
                List list = null;
                
                //...
                
                // list = (ArrayList<String>) trim.invoke (parser,
                params);
                list = (List) trim[i].invoke(parser, params);


                Either suppress the warning (which is perfectly
                acceptable), or don't cast to a typed list. Options
                abound.

                ~
                1. I can't supress the warning. 2.  I must use generics. ;-) So NOW how do I resolve this issue.  I've managed to cast them away previously.  I'm not sure why this one is unhappy.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                • 5. Re: unchecked cast *sigh*
                  3004
                  1. I can't supress the warning.

                  2. I must use generics. ;-)

                  So NOW how do I resolve this issue. I've managed to
                  cast them away previously. I'm not sure why this one
                  is unhappy.
                  Those are your only choices here, since Method returns Object.

                  You could cast to plain old ArrayList or List or whatever, without the <Whatever>, but then you'll just push this warning to other areas of the code.
                  • 6. Re: unchecked cast *sigh*
                    807599
                    > > 1. I can't supress the warning.
                    >>
                    2. I must use generics. ;-)

                    You're SOL, I'm afraid. ;o)

                    ~
                    • 7. Re: unchecked cast *sigh*
                      3004
                      As usual, yawmark's answer is much more succinct than mine. :-)