Details
-
Bug
-
Status: In Progress
-
Major
-
Resolution: Unresolved
-
2.4.7
-
None
-
None
-
None
Description
The following 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
Fails with:
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)
For some reason Groovy is attempting to cast value to Map when it isn't one.
Attachments
Issue Links
- is duplicated by
-
GROOVY-10668 Reference to method is ambiguous if block in "if" with "instanceof"
- Closed
-
GROOVY-10096 STC: accepts incorrect program with flow typing
- Closed
-
GROOVY-10769 Invalid code generated after instanceof test
- Closed
-
GROOVY-11008 incorrect type inference after (instanceof || instanceof)
- Closed
- relates to
-
GROOVY-10903 Cannot cast object 'A a' with class 'A' to class 'B'. Build file try to cast when use 'instanceof' and 'or' operator (||) in 'if' condition
- Closed
-
GROOVY-8965 instanceof with || inserts wrong cast
- Closed
-
GROOVY-11250 Compilation fails when accessing getter with shortcut notation on Java class
- Closed
-
GROOVY-11137 Improve support for union and intersection types
- Open
- links to