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

SymbolSolver is unable to .resolve() #3816

Closed
burcualper opened this issue Dec 26, 2022 · 9 comments
Closed

SymbolSolver is unable to .resolve() #3816

burcualper opened this issue Dec 26, 2022 · 9 comments
Labels
Need test case Question (JP usage) How to use JP to inspect/modify the AST - please close your question once answered!

Comments

@burcualper
Copy link

burcualper commented Dec 26, 2022

Hi
I want to find the number of immediate subclasses subordinated to a class hierarchy. Basically, I want to find the NOC metric.

For this I have created a visitor as below;

public class NOCVisitor extends VisitorAdapter<Void> {
	private ResolvedTypeDeclaration resolvedType;
    private List<TypeDeclaration<?>> immediateSubclasses;
    
	 // Override the visit method for ClassOrInterfaceDeclaration nodes
    @Override
    public void visit(ClassOrInterfaceDeclaration cid, Void arg) {
    	// Get the superclass of the class
    	
        NodeList<ClassOrInterfaceType> extendedTypes = cid.getExtendedTypes();
        if (extendedTypes.isEmpty()) {
            return;
        }
        ResolvedType extendedType = extendedTypes.get(0).resolve();//?????
        if (extendedType.isTypeVariable()) {
            return;
        }
        ClassOrInterfaceType superclassType = extendedTypes.get(0);

        // Find all the classes that extend the superclass
        CompilationUnit cu = (CompilationUnit) cid.getParentNode().get();
        List<ClassOrInterfaceDeclaration> subclassDeclarations = cu.findAll(ClassOrInterfaceDeclaration.class, c -> c.getExtendedTypes().stream().anyMatch(t -> t.equals(superclassType)));

        // Print the name of the superclass and the number of classes that extend it
        System.out.println("Superclass " + extendedType.asReferenceType().getQualifiedName() + " has " + subclassDeclarations.size() + " immediate subclasses");

        super.visit(cid, arg);
    }

to call the visitor I have used CompilationUnit and I have parsed it. then ı have called the visitor. as below;

File dir = new File(path);
			JavaParser javaParser = new JavaParser();

			// Iterate over all the files in the directory
			for (File file : dir.listFiles()) {
				collector= new ClassStatCollector();
				// Skip files that are not Java source code files
				if (!file.getName().endsWith(".java")) {
					continue;
				}
				className=file.getName().substring(0, file.getName().indexOf("."));

				cu = javaParser.parse(file).getResult().orElse(null);

				
				//find the num of immediate children -- NOC
				
				NOCVisitor nocVisitor = new NOCVisitor();
				cu.accept(nocVisitor, null);
				
			}

I am actually using a collector to keep the values but this was only to check if it works. I have done the same thing with wmc and dit metrics. they are working perfectly.
However I am getting an error when ı use the solver. like below

Exception in thread "main" java.lang.IllegalStateException: Symbol resolution not configured: to configure consider setting a SymbolResolver in the ParserConfiguration
	at com.github.javaparser.ast.Node.lambda$getSymbolResolver$7(Node.java:790)
	at java.base/java.util.Optional.map(Optional.java:260)
	at com.github.javaparser.ast.Node.getSymbolResolver(Node.java:786)
	at com.github.javaparser.ast.type.ClassOrInterfaceType.resolve(ClassOrInterfaceType.java:326)
	at com.project.visitors.NOCVisitor.visit(NOCVisitor.java:30)
	at com.project.visitors.NOCVisitor.visit(NOCVisitor.java:1)
	at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:98)
	at com.github.javaparser.ast.visitor.VoidVisitorAdapter.lambda$visit$43(VoidVisitorAdapter.java:175)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at com.github.javaparser.ast.NodeList.forEach(NodeList.java:288)
	at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:175)
	at com.project.visitors.VisitorAdapter.visit(VisitorAdapter.java:25)
	at com.github.javaparser.ast.CompilationUnit.accept(CompilationUnit.java:133)
	at com.project.sysClasses.MainSys.analyzeProjectStats(MainSys.java:71)
	at com.project.GUI.Main.main(Main.java:35)

So I will use this resolve a lot as I understand because I have a bunch of metrics to calculate. And I have skipped it since it has an error but the same problem occurred when I try to find NOC and RFC metrics. I will find this metrics for a project so there will be more than one java class.
What ı am missing here, I am literally get blind while I am searching.

@jlerbsc
Copy link
Collaborator

jlerbsc commented Dec 27, 2022

To resolve anything in JavaParser you have to configure the symbol solver. This link may be able to help you understand how JP works. You can also watch symbol solver test classes for inspiration on how to implement it. If this is not enough, do not hesitate to post your questions on this issue.

https://github.com/javaparser/javasymbolsolver

@jlerbsc
Copy link
Collaborator

jlerbsc commented Dec 31, 2022

Did this answer answer your question?

@jlerbsc jlerbsc added the Question (JP usage) How to use JP to inspect/modify the AST - please close your question once answered! label Dec 31, 2022
@burcualper
Copy link
Author

@jlerbsc Unfortunately, I could not figure it out the error.

@jlerbsc
Copy link
Collaborator

jlerbsc commented Jan 1, 2023

Do you still have the same error when initializing the symbol solver?

@burcualper
Copy link
Author

burcualper commented Jan 2, 2023

Yes, unfortunately. I am just parsing my project then ı am using the visitor. I have changed the visitor as below but still have the same error.

// Get the resolved type declaration for the class
        ResolvedReferenceTypeDeclaration rtd = cid.resolve();

        // Get the superclass of the class
        ResolvedReferenceTypeDeclaration superClass = (ResolvedReferenceTypeDeclaration) rtd.getAllAncestors();
        if (superClass != null && superClass.equals(rtd)) {
            // Increment the counter if the superclass is equal to the class being visited
            collector.incrementImmediateChildNodes(superClass.getClassName());
        }
        super.visit(cid, collector); 

@burcualper burcualper reopened this Jan 2, 2023
@jlerbsc
Copy link
Collaborator

jlerbsc commented Jan 2, 2023

Your code snippet does not show symbol solver initialization.

@burcualper
Copy link
Author

Aaa yes I have used symbol solver. I have used the previous code here. But with the symbol solver, I am using ResolvedType and ResolvedReferenceTypeDeclaration to reach the subclasses. The error came from there and still could not fix it.

@jlerbsc
Copy link
Collaborator

jlerbsc commented Jan 4, 2023

Can you provide us with a simplified test case that reproduces your issue?

@burcualper
Copy link
Author

Hi sorry for the late response. I am in a hurry, that's why ı have tried another class to solve it and it worked. Thanks a lot for your help :)

@jlerbsc jlerbsc closed this as completed Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Need test case Question (JP usage) How to use JP to inspect/modify the AST - please close your question once answered!
Projects
None yet
Development

No branches or pull requests

2 participants