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

ASF GitHub Bot commented on GROOVY-7971:
----------------------------------------

paulk-asert commented on PR #2353:
URL: https://github.com/apache/groovy/pull/2353#issuecomment-3688751632

   This seems good to me. I was previously using a testcase like below:
   
   ```
   import groovy.transform.*
   
   class Foo extends AbstractCollection {
       def peek() { 3 }
       int size() { -1 }
       void clear() { }
       Iterator iterator() { null }
   }
   
   @CompileStatic
   def method(idx) {
       def component = idx == 1 ? new ArrayDeque() : new Stack()
       if (idx == 3) component = new Foo()
       component.clear() // 'clear' in LUB (AbstractCollection or Serializable 
or Cloneable)
       if (component instanceof ArrayDeque) {
           component.addFirst(1) // 'addFirst' only in ArrayDeque
       } else if (component instanceof Stack) {
           component.addElement(2) // 'addElement' only in Stack
       }
       if (component instanceof Foo || component instanceof ArrayDeque || 
component instanceof Stack) {
           // checked duck typing
           assert component.peek() in 1..3 // 'peek' in ArrayDeque and Stack 
but not LUB
       }
   }
   method(1)
   method(2)
   method(3)
   ```
   
   Do you think that is already covered by existing cases?
   




> @CS flow typing incorrectly casting to map at runtime
> -----------------------------------------------------
>
>                 Key: GROOVY-7971
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7971
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.7
>            Reporter: Graeme Rocher
>            Assignee: Eric Milles
>            Priority: Major
>          Time Spent: 4h 10m
>  Remaining Estimate: 0h
>
> The following code:
> {code}
> import groovy.json.*
> import groovy.transform.*
> @CompileStatic
> class Bar {
>     private Writable renderTemplate(Object o, Map args) {
>     
>     }
>     private boolean isSimpleType(Class type) {
>         return type == String
>     }
>     def foo(Map map, Map arguments) {
>     
>         def writable = new Writable() {
>             @Override
>             Writer writeTo(Writer out) throws IOException {
>                 for(entry in map.entrySet()) {
>                    def value = entry.value
>                    if(isSimpleType(value.getClass()) || (value instanceof 
> Map)) {
>                         out.append(JsonOutput.toJson(value))
>                     }
>                 }
>                 return out
>             }
>         }
>     }
> }
> writable = new Bar().foo([one:'two'],[foo:'bar'])
> sw = new StringWriter()
> writable.writeTo(sw)
> println sw
> {code}
> Fails with:
> {code}
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast 
> object 'two' with class 'java.lang.String' to class 'java.util.Map'
>       at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
>       at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
>       at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
>       
> {code}
> For some reason Groovy is attempting to cast value to Map when it isn't one.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to