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 2 of 2

Thread: Proper way to cast generics?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Proper way to cast generics?

    Every once in awhile you run into that bit of generics code that no matter what you do you can't get the code to behave without a forced cast. This is often due to the fact that generics are implemented through type erasure and don't exist at runtime. What I was wondering is why do my casts sometimes come up as syntax error requiring me to remove generic types from them?

    For example; (These are all static members of a class with no generic type)

    private static final Map<Class<? extends Event>, Collection<EventHandler<? extends Event>>> EVENT_HANDLERS = ...
     
    static <T extends Event> Collection<EventHandler<T>> getHandlers(Class<T> eventType) {
    	return (Collection) EVENT_HANDLERS.get(eventType);
    }

    The cast above is of type (Collection). I want it to actually be of type (Collection<EventHandler<T>>) but when I add that generic info on it becomes a syntax error. Why is this?

    Also is there a way to achieve the desired relationship here without resorting to casts? That relationship being that the map EVENT_HANDLERS contains a bunch of entries, each entry having a key that is a class subtype of event and a corresponding value of an EventHandler of that same event type.

    I.E. <T extends Event> Map<Class<T>, Collection<EventHandler<T>>> *only that the T is a different subclass of Event for every entry.

    As far as I'm aware the Java generic system is not powerful enough to express this concept directly.


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Proper way to cast generics?

    The problem is that the type of the value of the map is the wildcarded type Collection<EventHandler<? extends Event>>. To this type you can cast. When you write your method, it returns a concrete type, the T which is contracted by the parameter. But java cannot determine if the value fetched by the key-value Class<T> would have the appropriate type. There exists no correlation between the key and the value. If you use wildcards and you want retrieve concrete types from any wildcarded type you must always cast. The bounded wildcard is for writing a common contract like an abstract class or an interface.

    Look also at this and this and this.

    A way to implement your relationship would be a structure like this:

    	class Example<E extends Event> {
    		private Class<E> clazz;
    		private Collection<EventHandler<E>> collection;
    	}

    Also you can first use an upper common type and then make a cast to concrete type:

    	static <T extends Event, E extends T> Collection<EventHandler<T>> getHandlers(Class<T> eventType) {
     
    		Collection<?> col =  EVENT_HANDLERS.get(eventType);
    		return (Collection<EventHandler<T>>)col;
    	}

    Actual it's not a solution for your problem

Similar Threads

  1. [SOLVED] getting the proper input from file
    By mia_tech in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 18th, 2012, 08:11 AM
  2. Proper way to get rid of breaks.
    By Massaslayer in forum Loops & Control Statements
    Replies: 6
    Last Post: December 23rd, 2011, 07:53 PM
  3. Unsafe cast
    By Kerr in forum Collections and Generics
    Replies: 2
    Last Post: April 19th, 2011, 06:49 AM
  4. Need proper Java Lingo
    By Deceptiron in forum Java Theory & Questions
    Replies: 4
    Last Post: November 22nd, 2010, 09:39 PM
  5. making a sentence proper
    By dvsumosize in forum Algorithms & Recursion
    Replies: 5
    Last Post: February 3rd, 2010, 11:01 AM

Tags for this Thread