how can i use java parser to get the information that which method has dependency on self object member field , and which member fields .
I'd probably :
1) get a set of field names that are in the parent container (Class,Enum) S1
(Note: this can be difficult if inheritance is used)
2) get a set of all variable names declared within the method or parameters S2
3) create a unique set of member variable names (subtract S2 from S1 to get S3)
4) walk/find nodes that have the name of any member of the Unique Set (S3) and add them to result set R1
5) walk/find nodes that have the nodes that match "this.(any from S1)" and add them to the result set R2
6) combine intersect (R1 and R2) and you'll have all the names
I'd probably :
- get a set of field names that are in the parent container (Class,Enum) S1
(Note: this can be difficult if inheritance is used)- get a set of all variable names declared within the method or parameters S2
- create a unique set of member variable names (subtract S2 from S1 to get S3)
- walk/find nodes that have the name of any member of the Unique Set (S3) and add them to result set R1
- walk/find nodes that have the nodes that match "this.(any from S1)" and add them to the result set R2
- combine intersect (R1 and R2) and you'll have all the names
what do you mean for S1 , using FieldDeclaration ? in my opinion , it seems FieldDeclaration will only get the member fields .
I mean that you can have this scenario:
class T{
public int a=100;
public int b=200;
void m( int a ){
System.out.println(this.a); //this is accessing the member field a;
System.out.println(a); //this is accessing the parameter a:
}
}
//also what I mean about inheritance:
class U extends T{
void n( int a) {
System.out.println(this.a); //this is accessing the member field a from base class T;
System.out.println(a); //this is accessing the parameter a:
}
}
basically you need to account for all VarDeclarators (member fields and local fields) and Parameters
to discern which are member accesses because (if you have a variable access like "a=1;" we could be
1) updating a member field (on this class)"a"
2) updating a member field (on a super class) "a"
3) updating a parameter "a"
4) updatuing local variable "a"
I mean that you can have this scenario:
class T{ public int a=100; public int b=200; void m( int a ){ System.out.println(this.a); //this is accessing the member field a; System.out.println(a); //this is accessing the parameter a: } }//also what I mean about inheritance:
class U extends T{ void n( int a) { System.out.println(this.a); //this is accessing the member field a from base class T; System.out.println(a); //this is accessing the parameter a: } }basically you need to account for all
VarDeclarators (member fields and local fields) and Parameters
to discern which are member accesses because (if you have a variable access like "a=1;" we could be
- updating a member field (on this class)"a"
- updating a member field (on a super class) "a"
- updating a parameter "a"
- updatuing local variable "a"
OK ,thanks
FYI if you are new to JavaParser, this is handy for understanding what the nodes are in any AST:
node.walk( n -> System.out.println(n.getClass()+" : " + n) );
it's important to know which node types you need to "look at" when querying
also look at:
https://javaparser.org/inspecting-an-ast/
and check out the book
https://leanpub.com/javaparservisited
...you can get it for free
Most helpful comment
FYI if you are new to JavaParser, this is handy for understanding what the nodes are in any AST:
it's important to know which node types you need to "look at" when querying
also look at:
https://javaparser.org/inspecting-an-ast/
and check out the book
https://leanpub.com/javaparservisited
...you can get it for free