-
-
Notifications
You must be signed in to change notification settings - Fork 349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get list of accessible variables at point in code #1513
Comments
Actually I think we had the contrary tool in Spoon: given a CtLocalVariable or a CtField we can give you the list of CtElements in which the variable or the field are visible. You may have a look on ctVar.map(new LocalVariableScopeFunction()).list() Maybe @pvojtechovsky can give you some insights on those functions. |
Hi Sourabh, yes, you can query for somethig like that. Have a look at documentation of LocalVariableReferenceFunction, LocalVariableScopeFunction and PotentialVariableDeclarationFunction. There is also a generic query function VariableReferenceFunction, which returns references to all kinds of variables. All of them can be used using example mentioned by Simon in the comment above. I had similar need like you when I did algorithm for renaming of local variable. You can have a look at code of CtRenameLocalVariableRefactoring. It is not easy reading - rules for visibility of local variables in Java are tricky, but Spoon knows them all! So may be this is exactly what you need - You did not described why do you need such function. Regards, |
Thank you for the reply. I want to find the influence of these variables in a code when statically analyzing the code. Hence I need this feature. package app;
public class App
{
static int test = 0;
public static void main(String[] args)
{
int local = 0; /* The iftest local variable from ifblock should not be accessible within this outer parentblock. similarly the variables "local" and "test" should be accessible within this block.*/
System.out.println("Hello world");
if(test<3){ /* The iftest local variable should be accessible within this ifblock, not outside. similarly the variables "local" and "test" should be accessible within this block as well.*/
int iftest = 10;
test += 1;
}
/* The iftest local variable from ifblock should not be accessible now since we are in the parent block*/
System.out.println("2nd line");
}
} I went through the instruction functions, you suggested. But I am not sure where to start this from. As you can see in the above example. The challenge is ignoring the inner if-block variables from outer method-block. i,e If I am at line System.out.println("2nd line"); in the code, from that position, The variable iftest declared within the if-block should be restricted only to that block. No-where else. Now from my current position in code. How do I find all variables within scope? It would help me a lot if you can advise me on how to go about this. Regards |
Start on the line System.out.println("2nd line"); and use PotentialVariableDeclarationFunction, which will return all the CtElements, which might be declaration of the variable visible on that line. |
This feature is too good. Thanks a lot. Regards |
Hello All, The earlier implementation helped me a lot. However in the below example, the non-static variable "test" should not be accessible in the static method main. Yet on calling the PotentialVariableDeclarationFunction(), the non-static variable "test" is visible in the static main method. package app; public class App
} Please let me know if you need more information |
Hi Sourabh, It looks like you found a bug. I think the problem is understandable. Would be nice if you can fix PotentialVariableDeclarationFunction and add it as new pull request. Otherwise I will have a look at it when I have some time ... |
I am trying to ffind all the possible list of CtLocalVariable or CtField which are accessible depending on the position at a point in code. A small example is give below.
Is it possible to query for something like this in spoon?
Regards
Sourabh
The text was updated successfully, but these errors were encountered: