Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7903

Locally declared variables in methods are not accessible from the VariableScope

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Not A Problem
    • None
    • None
    • ast builder
    • None

    Description

      Let's say you add a source to a CompilationUnit (unit):

      Test.java
      class Test {
           int plusOne(int value) {
                int one = 1;
                return value + one;
           }
      }
      

      and call unit.compile() and then run:

      Sample code using groovyc
      unit.iterator().forEachRemaining(sourceUnit ->
          sourceUnit.getAST().getClasses().forEach(clazz ->
              clazz.getAllDeclaredMethods().forEach(method -> 
                  System.out.print(method.getVariableScope()
                      .getDeclaredVariables().values())  
              )
          )
      );
      

      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?

      Attachments

        Activity

          People

            Unassigned Unassigned
            natgabb Natacha Gabbamonte
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: