[ 
https://issues.apache.org/jira/browse/WICKET-3468?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12998088#comment-12998088
 ] 

Richard Emberson commented on WICKET-3468:
------------------------------------------

Maybe better, since we know the class of the converter  (IConverter<Date>):

 public <C> IConverter<C> getConverter(Class<C> type) {
    if (Date.class.isAssignableFrom(type)) return (IConverter<C>)converter;
    else return super.getConverter(type);
  } 

For those Components with converters whose type is known, this is a type safe
mechanism.

If one has a Component with a type parameter and whose
converter is passed in as a construction
parameter, then one can not have compile-time type safety:

  public class SomeComponent<T> extends Component {
    private final IConverter<T> converter;
    public SomeComponent(IConverter<T> converter) {
      // ....
      this.converter = converter;
    }

    public <C> IConverter<C> getConverter(Class<C> type) {
      // can not safely return local converter because of
      // type erasure
    }
  }

And for completeness, since Scala provides a mechanism to get
around the JVM's type erasure, one can also have type safe type 
in the above case:

  class SomeComponent[T](val converter: IConverter<T>)(implicit m: Manifest[T])
        extends Component {

    def getConverter(tpe: Class[C]): IConverter[C] =
      if (m.erasure.isAssignableFrom(tpe) converter
      else super.getConverter(tpe)
  }


> org/apache/wicket/extensions/markup/html/form/DateTextField converter never 
> null
> --------------------------------------------------------------------------------
>
>                 Key: WICKET-3468
>                 URL: https://issues.apache.org/jira/browse/WICKET-3468
>             Project: Wicket
>          Issue Type: Improvement
>          Components: wicket-extensions
>    Affects Versions: 1.5-RC1
>         Environment: all
>            Reporter: Richard Emberson
>            Priority: Trivial
>
> As far as I can see the 
> org/apache/wicket/extensions/markup/html/form/DateTextField
> converter instance variable is never null, so the getConverter can be 
> re-written
>   public <C> IConverter<C> getConverter(Class<C> type)
>   {
>     if (converter == null)
>     {
>       return super.getConverter(type);
>     }
>     else
>     {
>       return (IConverter<C>)converter;
>     }
>   }
> to 
>   public <C> IConverter<C> getConverter(Class<C> type)
>   {
>       return (IConverter<C>)converter;
>   }
> And as an aside, here is another case where the Wicket code is saved by the 
> fact that
> the JVM does generic type erasure. The converter instance variable is of
> type IConverter<Date>. If someone came out with a JVM that did not do type 
> erasure,
> then the getConverter method would throw a cast exception for all but the 
> Date class
> as a parameter. Everywhere in Wicket that the getConverter method appears has 
> such
> a problem. But, saved by Java's poor generic implementation (though, the cast 
> exception would probably appear in later code).

-- 
This message is automatically generated by JIRA.
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to