Uploaded image for project: 'Tapestry 5'
  1. Tapestry 5
  2. TAP5-2674

Type coercer contribution - cast exception in equals

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Fixed
    • 5.7.0, 5.7.1, 5.7.2
    • 5.7.3
    • tapestry-core
    • None

    Description

      I have multiple Tapestry modules which contribute to type coercer, I can't make it work.

       

      I contribute TypeCoercer this way

       

      public static void contributeTypeCoercer(MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration) {
      
            Coercion<String, Class> coercion = path -> {
               try {
                  return Class.forName(path);
               } catch (ClassNotFoundException e) {
                  return null;
               }
      
            };
      
            CoercionTuple<String, Class> tuple = new CoercionTuple<>(String.class, Class.class, coercion);
      
            configuration.override(tuple.getKey(), tuple);
         }

       

      When I try to access a Tapestry page I get the following:

       

       

      Caused by: java.lang.ClassCastException: org.apache.tapestry5.commons.services.CoercionTuple$Key cannot be cast to org.apache.tapestry5.commons.services.CoercionTuple
      	at org.apache.tapestry5.commons.services.CoercionTuple$Key.equals(CoercionTuple.java:184)
      	at java.util.HashMap.getNode(HashMap.java:572)
      	at java.util.HashMap.get(HashMap.java:557)
      	at org.apache.tapestry5.ioc.internal.ValidatingMappedConfigurationWrapper.override(ValidatingMappedConfigurationWrapper.java:122)
      	at **********.SessionsSubModule.contributeTypeCoercer(SessionsSubModule.java:63)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.apache.tapestry5.ioc.internal.ContributionDefImpl.invokeMethod(ContributionDefImpl.java:133)
      	... 219 more
      
      

       

      The exception is raised in the equals method of CoercionTuple.Key:

      @Override
              public boolean equals(Object obj) 
              {
                  if (this == obj)
                      return true;
                  if (obj == null)
                      return false;
                  if (getClass() != obj.getClass())
                      return false;
                  // Exception is raised here
                  CoercionTuple other = (CoercionTuple) obj;
                  if (sourceType == null) 
                  {
                      if (other.sourceType != null)
                          return false;
                  } else if (!sourceType.equals(other.sourceType))
                      return false;
                  if (targetType == null) 
                  {
                      if (other.targetType != null)
                          return false;
                  } else if (!targetType.equals(other.targetType))
                      return false;
                  return true;
              }
      

      The equals is called in the HashMap#get implementation (used in ValidatingMappedConfigurationWrapper)

       

      With this part of code:

      ...
      if (getClass() != obj.getClass()) 
           return false;
      
      CoercionTuple other = (CoercionTuple) obj;
      ...
      

      the obj variable can't be a CoercionTuple, it will be a CoercionTuple.Key (which is the case in the implementation of HashMap<CoercionTupe.Key, CoercionTuple>#get).

      I might not have understood something, if it is the case, I'm sorry for the trouble.

       

       

      Attachments

        1. TAP5-2674.patch
          5 kB
          Volker Lamp

        Activity

          People

            ben-ng Ben Weidig
            cbo-itop Cédric BOONE
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: