Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6821153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:30:40+00:00 2026-05-26T21:30:40+00:00

However strange it sounds I need to modify some Groovy code by modifying how

  • 0

However strange it sounds I need to modify some Groovy code by modifying how variables are being created.

I have previously solved some other related problem with ASTTransformation when I needed to “mimic” the Use/Category-functionality by modifying the code to invoke my custom special method.

What I need now to do is this:

I need the following type of code:

def a = "X";
Integer b = 1;

to behave as this code:

a = "X";
b = 1;

The difference is that the second example is “stored” in the binding-scope.

Some context:

I’m writing a groovy template engine, where I have some code-fragment (provided by the user) that gets placed into a method in a groovy class / file, which I then compile. When I run the code-fragment/method I need its created variables to be accessible via the Binding-mechanisme.

I hope someone can help me in the correct direction..

Thanks in advance.

UPDATE:

(thanks for the great answer)

I have another question:

I think it can work when I modify all declarations, but it would be better if I only modified the ones in the most outer block. An example:

Object aMethod() {
  def a = 1; //should be modified
  def b = 2; //should be modified
  if (expression) {
    def c = 2; //does not need to be modified
  }
}

Do you know how it is possible to detect if the declaration is in the most outmost block (main block) in the method body?

-Morten

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:30:40+00:00Added an answer on May 26, 2026 at 9:30 pm

    You will have to replace the DeclarationExpressions in the AST with a BinaryExpression. You will notice the DeclarationExpression is a BinaryExpression, so you simply have to copy over everything. Also don’t forget to call setSourcePosition on the new node using the old node to ensure the line numbers are right. The Expressions are stored in ExpressionStatements, making it not too difficult to replace them.

    As for in which phase to operate… I strongly recommend to do this before the variable scopes are set, then you have less work and you won’t have to delete the many references to the variables in AST later. You could do this for example in CONVERSION.

    One more word on the semantic changes you introduce with this:

    (1) String a = 1; a = 2; in this code example you will get two times a String being stored in a. This is essentially because for every assignment Groovy will do a Groovy style cast and that includes the transformation to String here. This is done because the variable is typed. If it is not typed such transformations will not happen. That is the case for “def”, but also for variables in the binding. Meaning your binding would store the integers 1 and 2, not the Strings.

    (2) The other point is scopes. If I understand you right, then you want to make all variables global. Programs written with the context of having only a block wide scope for the variable may then behave strange. This can happen especially for variables in Groovy Closures if the Groovy Closures are executed not synchronously, but for example a later, more detached point in your program

    (3) The last point is a change in the usage of the MOP. The get the value of a local variable we don’t use the MOP, instead we do it directly. If you move the local variables into the global scope, you will have to go through the complete MOP to get the values. If at some point you have a program element capturing the call and repeating on its own, you will have get a different program.

    Maybe that doesn’t matter in your case… I just wanted you to be aware of these things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange situation where the following code works however XCode warns it
I have a strange problem whereby the ValidationSummary is not being displayed. However, the
So strange! Please have a look the code first: public class A {} public
This is strange I have a constant defined as such: define(RJ_FILES_PATH, RJ_SITE_DIRECTORY.\assets\files\\); However when
I thought this problem may be due to some strange path issue; however, the
I want to make a simple photo gallery function. However, there are some strange
I have an existing UITableViewController that was previously being used in a UINavigationController. I
I need to get 2 summed figures however im having issues as one will
hi im trying to using flashVars however for some reason there not getting sent
I only have experience with log4net however that does not mean that there are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.