To print all types of read and write access list to class fields for each method of a class in Java with JavaParser library, follow these steps:
- Parse the Java source file
import com.github.javaparser.StaticJavaParser; import com.github.javaparser.ast.CompilationUnit;
CompilationUnit cu = StaticJavaParser.parse(new File("Example.java"));
- Find all class declarations and iterate through them
cu.findAll(ClassOrInterfaceDeclaration.class).forEach(classDeclaration -> { System.out.println("Class: " + classDeclaration.getNameAsString());
- Find all method declarations within the class and iterate through them
classDeclaration.findAll(MethodDeclaration.class).forEach(methodDeclaration -> { System.out.println(" Method: " + methodDeclaration.getNameAsString());
- Find all field access expressions and print the accessed field names
methodDeclaration.findAll(FieldAccessExpr.class).forEach(fieldAccessExpr -> { System.out.println(" Field access: " + fieldAccessExpr.getNameAsString()); });
- Find all method call expressions and print the arguments
methodDeclaration.findAll(MethodCallExpr.class).forEach(methodCallExpr -> { System.out.println(" Method call: " + methodCallExpr.getNameAsString()); methodCallExpr.getArguments().forEach(arg -> { System.out.println(" Argument: " + arg); }); });
- Find all assignment expressions and print the left-hand side (LHS) and right-hand side (RHS) expressions
methodDeclaration.findAll(AssignExpr.class).forEach(assignExpr -> { System.out.println(" Assignment: " + assignExpr); System.out.println(" LHS: " + assignExpr.getTarget()); System.out.println(" RHS: " + assignExpr.getValue()); });
Here's an example of the Java code that implements these steps:
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.*;
import java.io.File;
import java.util.List;
public class FieldAccessList {
public static void main(String[] args) {
try {
CompilationUnit cu = StaticJavaParser.parse(new File("Example.java"));
cu.findAll(ClassOrInterfaceDeclaration.class).forEach(classDeclaration -> {
System.out.println("Class: " + classDeclaration.getNameAsString());
classDeclaration.findAll(MethodDeclaration.class).forEach(methodDeclaration -> {
System.out.println(" Method: " + methodDeclaration.getNameAsString());
List<FieldAccessExpr> fieldAccessExprs = methodDeclaration.findAll(FieldAccessExpr.class);
System.out.println(" Field access expressions:");
fieldAccessExprs.forEach(expr -> System.out.println(" " + expr.getNameAsString()));
List<MethodCallExpr> methodCallExprs = methodDeclaration.findAll(MethodCallExpr.class);
System.out.println(" Method call expressions:");
methodCallExprs.forEach(expr -> System.out.println(" " + expr.getNameAsString()));
List<AssignExpr> assignExprs = methodDeclaration.findAll(AssignExpr.class);
System.out.println(" Assignment expressions:");
assignExprs.forEach(expr -> System.out.println(" " + expr));
});
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
When you run this code, it will print the field access, method call, and assignment expressions along with their details for each method in the specified Java file.