Eliminating GWT ActivityMapper boilerplate

前端 未结 6 1118
盖世英雄少女心
盖世英雄少女心 2021-02-05 16:26

I am using the GWT Activities and Places framework to structure my application and it is turning out nicely. One thing that annoys me though is that the ActivityMapper

6条回答
  •  悲&欢浪女
    2021-02-05 17:06

    First of all I created an issue on GWT issuesfor this so please star it and or comment on it. Here's how I do it:

       public abstract class PlaceWithActivity extends Place {
            public Activity getActivity();
       }
    

    Then in your ActivityMapper:

    Public Activity get Activity(Place newPlace) {
         return ((PlaceWithActivity) newPlace).getActivity();
     }
    

    All of your places should extend PlaceWithActivity. The only issue is the down-casting which risks a ClassCastException. Place had the getActivity() then you wouldn't have to downcast but it doesn't so you have to downcast it to a class that does.

    What I don't like about it is that you have to do casting and make the PlaceWithActivity class. This wouldn't be necessary, if GWT would add support for what I'm doing. If they included a PlaceWithActivity class you wouldn't have to make it and if the ActivityManager would just call the getActivity() method of the PlaceWithActivity class then you would not only not have to down cast, but you wouldn't even need to write the ActivityMapper!

提交回复
热议问题