Javaparser: get method that dependency object member field

Created on 23 Dec 2019  路  5Comments  路  Source: javaparser/javaparser

how can i use java parser to get the information that which method has dependency on self object member field , and which member fields .

Question (JP usage)

Most helpful comment

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

All 5 comments

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 :

  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

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

  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"

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huangwaylon picture huangwaylon  路  4Comments

sangram82 picture sangram82  路  3Comments

nerzid picture nerzid  路  4Comments

ronak111091 picture ronak111091  路  3Comments

matozoid picture matozoid  路  3Comments