Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FieldAccessExpr does not print list of fields accesses of class #4315

Closed
GholamaliIrani opened this issue Feb 7, 2024 · 4 comments
Closed
Labels
Question (AST) Questions about nodes and relationships within the AST (not about how to use JavaParser)

Comments

@GholamaliIrani
Copy link

I want to print all Fields access into all methods of a class.
I try this:

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.FieldAccessExpr;
import java.io.File;
import java.io.IOException;

public class M907 {

    public static void main(String[] args) throws IOException {
        // Replace with the path to your Java file
        File sourceFile = new File("d:/test1/LargeNumber.java");

        CompilationUnit cu = StaticJavaParser.parse(sourceFile);

        cu.findAll(ClassOrInterfaceDeclaration.class).forEach(classDeclaration -> {
            System.out.println("Class: " + classDeclaration.getNameAsString());

            classDeclaration.findAll(MethodDeclaration.class).forEach(methodDeclaration -> {
                System.out.println("  Method: " + methodDeclaration.getNameAsString());

                methodDeclaration.findAll(FieldAccessExpr.class).forEach(fieldAccessExpr -> {
                    System.out.println("    Field Access: " + fieldAccessExpr.getNameAsString());
                });
            });
        });
    }
}

But its not working for my class.

@jlerbsc
Copy link
Collaborator

jlerbsc commented Feb 7, 2024

Why does this not work? What does this LargeNumber.java class contain?

@GholamaliIrani
Copy link
Author

GholamaliIrani commented Feb 7, 2024

this is my LargeNumber.java class:

public class LargeNumber {
    private String number;

    public LargeNumber(String number) {
        this.number = number;
    }

    public String getNumber() {
        return number;
    }

    public String add(String operand) {
        StringBuilder result = new StringBuilder();
        int carry = 0;
        int i = number.length() - 1;
        int j = operand.length() - 1;

        while (i >= 0 || j >= 0 || carry != 0) {
            int digit1 = (i >= 0) ? Character.getNumericValue(number.charAt(i)) : 0;
            int digit2 = (j >= 0) ? Character.getNumericValue(operand.charAt(j)) : 0;

            int sum = digit1 + digit2 + carry;
            int newDigit = sum % 10;
            carry = sum / 10;

            result.insert(0, newDigit);

            i--;
            j--;
        }

        return result.toString();
    }

    public String subtract(String operand) {
        StringBuilder result = new StringBuilder();
        int borrow = 0;

        int i = number.length() - 1;
        int j = operand.length() - 1;

        while (i >= 0 || j >= 0) {
            int digit1 = (i >= 0) ? Character.getNumericValue(number.charAt(i)) : 0;
            int digit2 = (j >= 0) ? Character.getNumericValue(operand.charAt(j)) : 0;

            int diff = digit1 - digit2 - borrow;

            if (diff < 0) {
                diff += 10;
                borrow = 1;
            } else {
                borrow = 0;
            }

            result.insert(0, diff);

            i--;
            j--;
        }
		number="12313";
        return result.toString();
    }

    public String multiply(String operand) {
        int n = number.length();
        int m = operand.length();
        int[] result = new int[n + m];

        for (int i = n - 1; i >= 0; i--) {
            int digit1 = Character.getNumericValue(number.charAt(i));
            int carry = 0;

            for (int j = m - 1; j >= 0; j--) {
                int digit2 = Character.getNumericValue(operand.charAt(j));

                int product = digit1 * digit2 + carry + result[i + j + 1];
                int newDigit = product % 10;
                carry = product / 10;

                result[i + j + 1] = newDigit;
            }

            result[i] = carry;
        }

        StringBuilder resultString = new StringBuilder();
        for (int digit : result) {
            resultString.append(digit);
        }

        // حذف ارقام صفر اضافی در ابتدا
        int i = 0;
        while (i < resultString.length() - 1 && resultString.charAt(i) == '0') {
            i++;
        }
        resultString.delete(0, i);
	number++;
        return resultString.toString();
    }

    public String divide(String operand) {
        if (operand.equals("0")) {
            throw new ArithmeticException("تقسیم بر صفر ممکن نیست");
        }

        LargeNumber dividend = new LargeNumber(number);
        LargeNumber divisor = new LargeNumber(operand);

        LargeNumber quotient = new LargeNumber("0");

        while (dividend.isGreaterThanOrEqual(divisor)) {
            dividend = dividend.subtract(divisor.getNumber());
            quotient = quotient.add("1");
        }

        return quotient.getNumber();
    }
}

and this is output:

Class: LargeNumber
  Method: getNumber
  Method: add
  Method: subtract
  Method: multiply
  Method: divide

I use JavaParser 3.25.8,

another example, for Example.java:

public class Example {

    private int field1;
    private String field2;

    public void method1() {
        field1 = 10;
        System.out.println(field2);
    }

    public void method2() {
        field2 = "Hello";
    }
}

output should be :

Class: Example
  Method: method1
    Field Access: field1
    Field Access: field2
  Method: method2
    Field Access: field2

but the output is:

Class: Example
  Method: method1
    Field Access: out
  Method: method2

@jlerbsc
Copy link
Collaborator

jlerbsc commented Feb 8, 2024

The result is correct. The expression below is an assignment (AssignExpr).

 field2 = "Hello";

@jlerbsc jlerbsc added the Question (AST) Questions about nodes and relationships within the AST (not about how to use JavaParser) label Feb 8, 2024
@jlerbsc
Copy link
Collaborator

jlerbsc commented Feb 9, 2024

If we've answered your question, could you close this issue? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question (AST) Questions about nodes and relationships within the AST (not about how to use JavaParser)
Projects
None yet
Development

No branches or pull requests

2 participants