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

How to print all types of read and write access list to class fields for each methods of class in Java with JavaParser library
1. Code Overview
The JavaParser library is a powerful tool for parsing and manipulating Java source code. It can be used to extract useful information from Java code, such as the list of fields accessed in a method. In this demonstration, we'll utilize JavaParser to print all types of read and write access list to class fields for each method of a class.
2. Import Necessary Libraries
```java 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.AssignExpr; import com.github.javaparser.ast.expr.FieldAccessExpr; import com.github.javaparser.ast.expr.NameExpr; import java.io.File; import java.io.FileNotFoundException; import java.util.List; ```
3. Parse the Java Source File
```java File sourceFile = new File("Example.java"); CompilationUnit cu = StaticJavaParser.parse(sourceFile); ```
4. Iterate Over Class Declarations
```java cu.findAll(ClassOrInterfaceDeclaration.class).forEach(classDeclaration -> { ```
5. Iterate Over Method Declarations
```java classDeclaration.findAll(MethodDeclaration.class).forEach(methodDeclaration -> { ```
6. Process Assignment Statements
Assignment statements (AssignExpr) are used to assign values to fields. We'll extract the field names accessed in these statements.
```java List assignmentStatements = methodDeclaration.findAll(AssignExpr.class); assignmentStatements.forEach(assignExpr -> { assignExpr.findAll(FieldAccessExpr.class).forEach(fieldAccessExpr -> { System.out.println("Field Access (Read): " + fieldAccessExpr.getNameAsString()); }); assignExpr.findAll(NameExpr.class).forEach(nameExpr -> { System.out.println("Field Access (Write): " + nameExpr.getNameAsString()); }); }); ```
7. Process Method Call Statements
Method call statements (MethodCallExpr) are used to invoke methods on objects. We'll extract the field names passed as arguments to these method calls.
```java List methodCallStatements = methodDeclaration.findAll(MethodCallExpr.class); methodCallStatements.forEach(methodCallExpr -> { methodCallExpr.getArguments().forEach(arg -> { if (arg instanceof FieldAccessExpr) { System.out.println("Field Access (Read): " + ((FieldAccessExpr) arg).getNameAsString()); } else if (arg instanceof NameExpr) { System.out.println("Field Access (Write): " + ((NameExpr) arg).getNameAsString()); } }); }); ```
8. Process Return Statements
Return statements (ReturnStmt) are used to return values from methods. We'll extract the field names accessed in these statements.
```java List returnStatements = methodDeclaration.findAll(ReturnStmt.class); returnStatements.forEach(returnStatement -> { returnStatement.findAll(FieldAccessExpr.class).forEach(fieldAccessExpr -> { System.out.println("Field Access (Read): " + fieldAccessExpr.getNameAsString()); }); returnStatement.findAll(NameExpr.class).forEach(nameExpr -> { System.out.println("Field Access (Write): " + nameExpr.getNameAsString()); }); }); ```
9. Process Unary Statements
Unary statements (UnaryExpr) are used to perform unary operations on variables. We'll extract the field names accessed in these statements.
```java List unaryStatements = methodDeclaration.findAll(UnaryExpr.class); unaryStatements.forEach(unaryStatement -> { unaryStatement.findAll(NameExpr.class).forEach(nameExpr -> { System.out.println("Field Access (Write): " + nameExpr.getNameAsString()); }); }); ```
10. Print Results
After processing all the statements, we print the list of field accesses for the current method.
```java System.out.println("*** Method: " + methodDeclaration.getNameAsString() + " ***"); ```
This approach covers various scenarios of field access, including read and write operations, in different types of statements within a method.

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.