You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2019/09/24 10:58:00 UTC

[jira] [Commented] (GROOVY-9258) GroovyShell fails to import statically a Hamcrest matcher Class

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

Paul King commented on GROOVY-9258:
-----------------------------------

If you want the GroovyShell to have access to the Grabbed classes, you should change your shell assignment to something like this:
{code}
def shell = new GroovyShell(getClass().classLoader, configuration)
{code}


> GroovyShell fails to import statically a Hamcrest matcher Class
> ---------------------------------------------------------------
>
>                 Key: GROOVY-9258
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9258
>             Project: Groovy
>          Issue Type: Bug
>          Components: GroovyScriptEngine
>    Affects Versions: 2.5.8, 3.0.0-beta-3
>         Environment: Windows 10 Pro. JDK1.8.0_131-win64 or JRE1.8.0_211-win64 or JRE1.7-0_80. 
>            Reporter: Juaneda
>            Priority: Major
>         Attachments: GROOVY-JIRA-x.png
>
>
> Evaluation of a Hamcrest assertion fails with GroovyShell when importing dependencies(static import).
> The same code behaves normally when run straight from groovy script level.
> See attachement for failure details.
>  
> Tested with hamcrest 2.1 and hamcrest-all 1.3.
> Also tested with hamcrest-core/hamcrest-library 2.1.
>  
> {code:java}
> @Grapes([
>   @Grab('org.hamcrest:hamcrest:2.1')]
> )
> import static org.hamcrest.Matchers.*
> import static org.hamcrest.MatcherAssert.*
> import org.codehaus.groovy.control.customizers.ImportCustomizer
> import org.codehaus.groovy.control.CompilerConfiguration
> def key = 7
> assertThat key, allOf(is(key), equalTo(key))
> def importCustomizer = new ImportCustomizer()
> importCustomizer.addStaticStars 'org.hamcrest.MatcherAssert', 'org.hamcrest.Matchers'
> def configuration = new CompilerConfiguration()
> configuration.addCompilationCustomizers importCustomizer
> def shell = new GroovyShell(configuration)
> def value = "allOf(is($key), equalTo($key))"
> def script = """
> assertThat $key, $value
> """
> def p = shell.evaluate script
> println 'OK'
>  {code}
>  
>  



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