Groovy problem



  • String scriptText = "int a = 12;";
    
    ImportCustomizer ic = new ImportCustomizer();
    ic.addImports("my.package.MyClass");
    
    CompilerConfiguration cc = new CompilerConfiguration();
    cc.addCompilationCustomizers(ic);
    
    Binding gb = new Binding();
    gb.setVariable("myObject", myObject);
    
    GroovyShell gsh = new GroovyShell(gb, cc);
    
    gsh.evaluate(scriptText);
    
    Object res = gsh.evaluate(scriptText);
    

    The design environment says the line

    gb.setVariable("myObject", myObject);
    

    is a mistake. How do I fix it?



  • myObect You don't have to declare yourself in a violin that you want to start, where you want to use it. myObectBut he's supposed to be in the program!

    Java

     MyClass myObject = new MyClass();
     ...
     gb.setVariable("myObject", myObject);
     ...
    

    Groovy

     ...
     myObject.method()
     myObject.prop = 123     // Все эти действия над объектом,
     println(myObject.prop)  // переданном скрипту через binding
     ...
    

    There are variables in the violin, and if one of them is def myObject, then it's not gonna be seen as a crypt, but it's gonna be seen by its own variable.

    Groovy

     ...
     def myObject // это скроет от скрипта binding "myObject"
     ...
     myObject.method()
     myObject.prop = 123    // Все эти действия уже над переменной myObject, 
     println(myObject.prop) // определённой в скрипте, а не переданной ему в binding
     ...
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2