Skip to content
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

Closed
sourabhsparkala opened this issue Sep 1, 2017 · 7 comments
Closed

Get list of accessible variables at point in code #1513

sourabhsparkala opened this issue Sep 1, 2017 · 7 comments
Labels

Comments

@sourabhsparkala
Copy link

sourabhsparkala commented Sep 1, 2017

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?

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");
	}
}

Regards
Sourabh

@surli
Copy link
Collaborator

surli commented Sep 2, 2017

Hi @sourabhsparkala

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

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 LocalVariableScopeFunction and FieldScopeFunction. Those functions are designed to be used inside a query, like in this snippet:

ctVar.map(new LocalVariableScopeFunction()).list()

Maybe @pvojtechovsky can give you some insights on those functions.

@surli surli added the question label Sep 2, 2017
@pvojtechovsky
Copy link
Collaborator

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,
Pavel

@sourabhsparkala
Copy link
Author

sourabhsparkala commented Sep 4, 2017

@pvojtechovsky @surli

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
Sourabh

@pvojtechovsky
Copy link
Collaborator

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.

@sourabhsparkala
Copy link
Author

This feature is too good.

Thanks a lot.

Regards
Sourabh

@sourabhsparkala
Copy link
Author

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
{

int test = 0;
public static void main(String[] args)
{
	int local = 0; 
	System.out.println("Hello world");
	
	if(test<3){   
		
		int iftest = 10;
		test += 1;
	}
	System.out.println("2nd line");
}

}

Please let me know if you need more information

@pvojtechovsky
Copy link
Collaborator

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 ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants