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

Volker Lamp commented on TAP5-2674:
-----------------------------------

As Cédric had already pointed out the implementation of 
CoercionTuple.Key.equals() is what's causing the problem. The inner class Key 
can access to other members of the enclosing class CoercionTuple only within 
its own instance (this), not another instance. The fix I'm proposing is to add 
to the Key class protected getters for the sourceType and targetFields of the 
enclosing class. 

The attached [^TAP5-2674.patch] contains the test case as well as the test 
configuration.

> Type coercer contribution - cast exception in  equals
> -----------------------------------------------------
>
>                 Key: TAP5-2674
>                 URL: https://issues.apache.org/jira/browse/TAP5-2674
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-core
>    Affects Versions: 5.7.2
>            Reporter: Cédric BOONE
>            Priority: Blocker
>         Attachments: TAP5-2674.patch
>
>
> I have multiple Tapestry modules which contribute to type coercer, I can't 
> make it work.
>  
> I contribute TypeCoercer this way
>  
> {code:java}
> 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);
>    }{code}
>  
> When I try to access a Tapestry page I get the following:
>  
>  
> {code:java}
> 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
> {code}
>  
> The exception is raised in the equals method of CoercionTuple.Key:
> {code:java}
> @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;
>         }
> {code}
> The equals is called in the HashMap#get implementation (used in 
> ValidatingMappedConfigurationWrapper)
>  
> With this part of code:
> {code:java}
> ...
> if (getClass() != obj.getClass()) 
>      return false;
> CoercionTuple other = (CoercionTuple) obj;
> ...
> {code}
> 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.
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to