Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Unsafe cast

  1. #1
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Unsafe cast

    Hi all.

    I have this observer pattern based logger in my program. In it I have a method that allows you get get a listener depending on its class. Thought it would be a simple solution, since it is all I need at the moment and it didnt take much effort to implement. I use generics to make so the caller wont have to care that much about casting. However, I get a compiler warning about an unsafe cast.
    public synchronized <T extends LogListener> T findListener(Class<T> logClass)
    {
    	T found = null;
    	if (logClass != null)
    	{
    		for (LogListener l : mListeners) // mListeners is just a list of listeners
    		{
    			if ( logClass.isInstance( l ) )
    			{
    				found = (T) l; // This is where the warning comes in
    				break;
    			}
    		}
    	}
    	return found;
    }
    The code itself runs just fine. No exceptions are thrown during runtime (in fact I am just using the method once, to avoid sending the object along side with the logger). Is this warning something I should worry about?

    Take care,
    Kerr.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Unsafe cast

    As far as I can tell, there's nothing unsafe about it. You're explicitly checking the type before performing the cast, however you're not using the usual instanceof operator (because it wouldn't work here) so Java's going to complain.

  3. #3
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Unsafe cast

    Ok, thanks for clearing it up .

Similar Threads

  1. Unchecked or Unsafe Operations
    By saxophonemaster in forum Collections and Generics
    Replies: 2
    Last Post: April 12th, 2011, 04:40 AM
  2. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  3. A lot of annoying cast errors.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 16th, 2010, 10:54 PM
  4. Could someboyd please help me make my cast errors go away?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 16th, 2010, 10:23 PM
  5. unsafe operations note??
    By bookface in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 22nd, 2010, 09:58 AM