Notification texts go here Contact Us Buy Now!

How to print all types of read and write access list to class fields for each methods of class in Java with JavaParser library

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:

  1. Parse the Java source file
    import com.github.javaparser.StaticJavaParser;
    import com.github.javaparser.ast.CompilationUnit;
    CompilationUnit cu = StaticJavaParser.parse(new File("Example.java"));
  2. Find all class declarations and iterate through them
    cu.findAll(ClassOrInterfaceDeclaration.class).forEach(classDeclaration -> {
        System.out.println("Class: " + classDeclaration.getNameAsString());
  3. Find all method declarations within the class and iterate through them
    classDeclaration.findAll(MethodDeclaration.class).forEach(methodDeclaration -> {
        System.out.println("  Method: " + methodDeclaration.getNameAsString());
  4. Find all field access expressions and print the accessed field names
    methodDeclaration.findAll(FieldAccessExpr.class).forEach(fieldAccessExpr -> {
            System.out.println("    Field access: " + fieldAccessExpr.getNameAsString());
        });
  5. 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);
            });
        });
  6. 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.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.