You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Natacha Gabbamonte (JIRA)" <ji...@apache.org> on 2016/08/22 21:44:20 UTC

[jira] [Commented] (GROOVY-7903) Locally declared variables in methods are not accessible from the VariableScope

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

Natacha Gabbamonte commented on GROOVY-7903:
--------------------------------------------

Locally declared variables of methods are accessible through:

if (method.getCode() instanceof BlockStatement) {
            System.out.print(((BlockStatement) method.getCode()).getVariableScope()
                .getDeclaredVariables().values()) 
}

> Locally declared variables in methods are not accessible from the VariableScope
> -------------------------------------------------------------------------------
>
>                 Key: GROOVY-7903
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7903
>             Project: Groovy
>          Issue Type: Improvement
>          Components: ast builder
>            Reporter: Natacha Gabbamonte
>
> Let's say you add a source to a CompilationUnit ({{unit}}):
> {code:title=Test.java|borderStyle=solid}
> class Test {
>      int plusOne(int value) {
>           int one = 1;
>           return value + one;
>      }
> }
> {code}
> and call {{unit.compile()}} and then run:
> {code:title=Sample code using groovyc}
> unit.iterator().forEachRemaining(sourceUnit ->
>     sourceUnit.getAST().getClasses().forEach(clazz ->
>         clazz.getAllDeclaredMethods().forEach(method -> 
>             System.out.print(method.getVariableScope()
>                 .getDeclaredVariables().values())  
>         )
>     )
> );
> {code}
> The only declared variable you get for method {{plusOne}} is the parameter {{value}}. The local variable {{one}} is not accessible.
> Is there another way of accessing these locally declared variables?



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)