[ 
https://issues.apache.org/jira/browse/GROOVY-11119?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King closed GROOVY-11119.
------------------------------

> String and Pattern conflict
> ---------------------------
>
>                 Key: GROOVY-11119
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11119
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.5.22, 3.0.18, 4.0.13
>            Reporter: A. Lepe
>            Assignee: Eric Milles
>            Priority: Minor
>             Fix For: 4.0.14, 3.0.19
>
>
> I'm not sure if its a bug or I am doing something wrong, but it is a little 
> bit strange:
> Consider the following class:
>  
> {code:java}
> // code placeholder
> @CompileStatic
> static class Search {
>     private String keyword
>     private Pattern pattern
>     private int code = 0
>     void setCriteria(String keyword) {
>         this.keyword = keyword
>         code = 1
>     }
>     void setCriteria(Pattern pattern) {
>         this.pattern = pattern
>         code = 2
>     }
>     int getCode() {
>         return code
>     }
> }
>  {code}
> Now if you execute:
> {code:java}
> // code placeholder
> String kw = "something"
> assert new Search(
>      criteria: kw
> ).code == 1        
> Pattern p = ~/\w+/
> assert p instanceof Pattern
> assert new Search(
>      criteria: p
> ).code == 2  // fails as value returned is 1{code}
> It fails as it is calling the `setCriteria(String)` instead of 
> `setCriteria(Pattern)`. I'm not sure if that is the expected behavior.
>  
> One thing to note is that the above code works without issues when compiling 
> dynamically. 
> Another strange behavior is that if `setCriteria(Pattern)` is declared before 
> `setCriteria(String)`, it fails to compile:
>  
> {code:java}
> // code placeholder
> @CompileStatic
> static class Search {
>     private String keyword
>     private Pattern pattern
>     private int code = 0
>     // Pattern before String:
>     void setCriteria(Pattern pattern) {
>         this.pattern = pattern
>         code = 2
>     }
>     void setCriteria(String keyword) {
>         this.keyword = keyword
>         code = 1
>     }
>     int getCode() {
>         return code
>     }
> } 
> new Search(
>      criteria: "hello"  // <---- will fail here 
> ){code}
> The error thrown is: 
> {noformat}
> Cannot cast object 'hello' with class 'java.lang.String' to class 
> 'java.util.regex.Pattern'{noformat}
> Again, without `@CompileStatic` works fine.
>  
> It seems to happen in all Groovy versions (2.X, 3.X and 4.X).
> NOTE: I maintain a library which is developed with Groovy, but that can be 
> used from Java or Kotlin, for that reason I need to support `Pattern` class 
> outside Groovy.
> I have created a testing code in case you need it:
> [https://gitlab.com/intellisrc/groovy-bugs/-/tree/string-pattern]



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

Reply via email to