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.
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
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
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
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
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.