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

record in an interface throw IllegalArgumentException #4303

Open
Fioooooooo opened this issue Jan 31, 2024 · 7 comments
Open

record in an interface throw IllegalArgumentException #4303

Fioooooooo opened this issue Jan 31, 2024 · 7 comments

Comments

@Fioooooooo
Copy link

Fioooooooo commented Jan 31, 2024

My Environment

javaparser 3.25.8

Current Behavior

When try to resolve a MethodCallExpr got an exception.

Here is my test code for reproduce.

@Test
public void testStaticMethod() {
    CompilationUnit compilationUnit = StaticJavaParser.parse("""
            public interface IUtil {
                record WrapperRecord(String name){}
            }
            
            public class Util implements IUtil {
                public static Util create(String key) {
                    return new Util();
                }
            }
                            
            public class Test {
                            
                public void test() {
                    Util.create("foo");
                }
                            
            }
            """);

    for (MethodDeclaration method : compilationUnit.findAll(MethodDeclaration.class)) {
        for (MethodCallExpr call : method.findAll(MethodCallExpr.class)) {
            String qualifiedSignature = call.resolve().getQualifiedSignature();
            log.info("QualifiedSignature: {}", qualifiedSignature);
        }
    }
}
  • Here is an interface named IUtil, and it has a record member.
  • A class named Util implement IUtil.
  • A test class Test has a method call Util.create().

The test code will throw an exception

java.lang.IllegalArgumentException: Cannot get a reference type declaration from com.github.javaparser.ast.body.RecordDeclaration

	at com.github.javaparser.symbolsolver.JavaSymbolSolver.toTypeDeclaration(JavaSymbolSolver.java:426)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getTypeDeclaration(JavaParserFacade.java:698)
	at com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeAdapter.internalTypes(JavaParserTypeAdapter.java:196)
	at com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration.internalTypes(JavaParserInterfaceDeclaration.java:366)
	at com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration.hasInternalType(ResolvedTypeDeclaration.java:61)
	at com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration.isAncestor(JavaParserClassDeclaration.java:396)
	at com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration.getAncestors(JavaParserClassDeclaration.java:373)
	at com.github.javaparser.symbolsolver.javaparsermodel.contexts.JavaParserTypeDeclarationAdapter.solveMethod(JavaParserTypeDeclarationAdapter.java:251)
	at com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext.solveMethod(ClassOrInterfaceDeclarationContext.java:106)
	at com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration.solveMethod(JavaParserClassDeclaration.java:329)
	at com.github.javaparser.resolution.logic.MethodResolutionLogic.solveMethodInType(MethodResolutionLogic.java:887)
	at com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethod(MethodCallExprContext.java:161)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:273)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:134)
	at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:172)
	at com.github.javaparser.ast.expr.MethodCallExpr.resolve(MethodCallExpr.java:319)




I tried to debug JP. I found when resolve the method call(Util.create()), JP will get Util ancestors and try get internal types.

In JavaParserTypeAdapter, the record WrapperRecord in IUtil is instance of RecordDeclaration, and then exception occurred.

public Set<ResolvedReferenceTypeDeclaration> internalTypes() {
        // Use a special Set implementation that avoids calculating the hashCode of the node,
        // since this can be very time-consuming for big node trees, and we are sure there are
        // no duplicates in the members list.
        Set<ResolvedReferenceTypeDeclaration> res = Collections.newSetFromMap(new IdentityHashMap<>());
        for (BodyDeclaration<?> member : this.wrappedNode.getMembers()) {
            if (member instanceof TypeDeclaration) {
                res.add(JavaParserFacade.get(typeSolver).getTypeDeclaration((TypeDeclaration) member));
            }
        }
        return res;
    }

My Question

I want to know what did i wrong if i try to get the method qualified signature? Or dose JP not support parsing record in interfaces?

@jlerbsc
Copy link
Collaborator

jlerbsc commented Jan 31, 2024

It seems to be a bug in the symbol solver. Records resolution is not fully supported in Symbol Solver.

@Fioooooooo
Copy link
Author

It seems to be a bug in the symbol solver. Records resolution is not fully supported in Symbol Solver.

Thank you for the reply, will JP fix the issue next version?

@jlerbsc
Copy link
Collaborator

jlerbsc commented Feb 1, 2024

The integration of Records resolution is a fairly lengthy process. It is very likely that this issue will not be corrected in the next version.

@Fioooooooo
Copy link
Author

The integration of Records resolution is a fairly lengthy process. It is very likely that this issue will not be corrected in the next version.

Thank you for your reply.

@jlerbsc
Copy link
Collaborator

jlerbsc commented Feb 2, 2024

Related to #3556 (comment)

@secanalyzer
Copy link

The integration of Records resolution is a fairly lengthy process. It is very likely that this issue will not be corrected in the next version.

I'm not a developer of JP, but if I want to fix this, can anyone give me some guidance on how to do it and an estimate of how much time it takes?

@jlerbsc
Copy link
Collaborator

jlerbsc commented Apr 25, 2024

Thank you for your offer, but it's currently under development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants