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

UnsolvedSymbolException ... cannot be resolved in context ... MethodCallExprContext #4339

Open
ond1 opened this issue Mar 12, 2024 · 2 comments

Comments

@ond1
Copy link

ond1 commented Mar 12, 2024

Hi,

The following exception is thrown from a simple sample Java code below:

Exception in thread "main" UnsolvedSymbolException{context='null', name='Method 'key' cannot be resolved in context key(KEYWORD_LBRA, RPAR) (line: 35) MethodCallExprContext{wrapped=key(KEYWORD_LBRA, RPAR)}. Parameter types: [PrimitiveTypeUsage{name='int'}, PrimitiveTypeUsage{name='int'}]', cause='null'}
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:651)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:324)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:64)
at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:118)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:493)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.lambda$getType$5(JavaParserFacade.java:380)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:379)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:640)
at com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext.solveSymbolAsValue(LambdaExprContext.java:74)
at com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserParameterDeclaration.getType(JavaParserParameterDeclaration.java:63)
at com.github.javaparser.resolution.model.Value.from(Value.java:45)

`public class Sample {

private static final Map<Long, ExpressionMaker> patterns = new HashMap<>();

public static final int KEYWORD_LBRA = 69;
public static final int RPAR = 204;

public Sample() {
}


private static long key(Integer... tokens) {
    long key = 0L;
    for (int token : tokens) {
        key = (key << 8) + token;
    }
    return key;
}

@FunctionalInterface
private interface ExpressionMaker {
    String make(List<String> tokens, String env) ;
}

static {


    patterns.put(
            key(KEYWORD_LBRA, RPAR),
            (values, env) -> {
                String kind = values.get(0);

                return kind;
            });

}

}`

Run it with the following:

`
CompilationUnit cu = StaticJavaParser.parse(filename);

System.err.println("Processing " + filename);
List nes = cu.findAll(NameExpr .class);
nes.forEach(ne -> {
System.out.println(( ne.calculateResolvedType().describe()));
});`

There is some type resolution problem with the argument Integer ... token in the key method

@ond1
Copy link
Author

ond1 commented Mar 12, 2024

The problem occurs in JavaParser version 3.25.9. It works fine in version 3.17.0

@jlerbsc
Copy link
Collaborator

jlerbsc commented Mar 12, 2024

You are right. JP cannot resolve the key(Integer... tokens) method from key(KEYWORD_LBRA, RPAR) because it considers that int[] cannot be assigned to Integer[].
Probably related to https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.3 (Invocation Contexts#Loose invocation contexts) or
https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html (15.12.2. Compile-Time Step 2: Determine Method Signature ) -> Second phase

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

2 participants