Repository: javaparser/javaparser

Issue #3111: Unable to resolve Parameter in LambdaExpr due to NoSuchElementException
Created: 2021-02-23T19:21:46Z
Given the following example:
class A {
  Consumer<Integer> consumer = item -> {};
}
I would like to resolve the item parameter. When I try to execute Parameter::resolve() on it, a  NoSuchElementException is thrown.
I think the issue may be related with the following lines, more specific with line 241. The logic to solve the parameter tries to find a callable declaration that is implemented by ContructorDeclaration and MethodDeclaration but LambdaExpr don't implement it.

  
    
      javaparser/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/JavaSymbolSolver.java
    
    
        Lines 238 to 254
      in
      c579b8d
    
  
  
    

        
          
           if (node instanceof Parameter) { 
        

        
          
               if (ResolvedParameterDeclaration.class.equals(resultClass)) { 
        

        
          
                   Parameter parameter = (Parameter) node; 
        

        
          
                   CallableDeclaration callableDeclaration = node.findAncestor(CallableDeclaration.class).get(); 
        

        
          
                   ResolvedMethodLikeDeclaration resolvedMethodLikeDeclaration; 
        

        
          
                   if (callableDeclaration.isConstructorDeclaration()) { 
        

        
          
                       resolvedMethodLikeDeclaration = callableDeclaration.asConstructorDeclaration().resolve(); 
        

        
          
                   } else { 
        

        
          
                       resolvedMethodLikeDeclaration = callableDeclaration.asMethodDeclaration().resolve(); 
        

        
          
                   } 
        

        
          
                   for (int i = 0; i < resolvedMethodLikeDeclaration.getNumberOfParams(); i++) { 
        

        
          
                       if (resolvedMethodLikeDeclaration.getParam(i).getName().equals(parameter.getNameAsString())) { 
        

        
          
                           return resultClass.cast(resolvedMethodLikeDeclaration.getParam(i)); 
        

        
          
                       } 
        

        
          
                   } 
        

        
          
               } 
        

        
          
           } 
        
    
  


Stack trace
java.util.NoSuchElementException: No value present

	at java.util.Optional.get(Optional.java:135)
	at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:241)
	at com.github.javaparser.ast.body.Parameter.resolve(Parameter.java:323)
	at com.github.javaparser.Issue.testIssue(Issue.java:198)

Unit test
@Test
void testIssue() {
  // Source code
  String sourceCode = "import java.util.function.Consumer;" +
		      "class A {" +
		      "Consumer<Integer> consumer = item -> {};" +
		      "}";
  
  // Setup symbol solver
  ParserConfiguration configuration = new ParserConfiguration()
		  .setSymbolResolver(new JavaSymbolSolver(new CombinedTypeSolver(new ReflectionTypeSolver())));
  
  // Setup parser
  JavaParser parser = new JavaParser(configuration);
  CompilationUnit cu = parser.parse(sourceCode).getResult().get();
  
  // Test
  ResolvedParameterDeclaration param = Navigator.demandNodeOfGivenClass(cu, Parameter.class)
		  .resolve();
  assertTrue(param.isParameter());
}

Duplicate issue #4240: Calling resolve on catch block parameter throws exception. 
Created: 2023-11-28T23:36:07Z
The parsed java file is as below:
package com.examples.input;
public class Test1 {
    void test() {
        try{
            throw new CustomException("This is a custom Exception");
        } catch (CustomException e) {
            System.out.println(e.getMessage());
        }
    }
}

In ModifierVisitor, when trying to resolve the Parameter in method
@Override
 public Visitable visit(Parameter n, Void arg) {
        n.resolve();  // ---> exception thrown here
        return super.visit(n, arg);  
 }

an exception is thrown. Stack trace:
Exception in thread "main" java.lang.UnsupportedOperationException: Unable to find the declaration of type ResolvedParameterDeclaration from Parameter
	at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:279)
	at com.github.javaparser.ast.body.Parameter.resolve(Parameter.java:322)
	at com.examples.input.ExceptionVisitor.visit(ExceptionVisitor.java:10)
	at com.examples.input.ExceptionVisitor.visit(ExceptionVisitor.java:7)
	at com.github.javaparser.ast.body.Parameter.accept(Parameter.java:114)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:210)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.stmt.CatchClause.accept(CatchClause.java:78)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:1133)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.NodeList.accept(NodeList.java:275)
	at com.github.javaparser.ast.visitor.ModifierVisitor.modifyList(ModifierVisitor.java:1172)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:951)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.stmt.TryStmt.accept(TryStmt.java:137)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:1133)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.NodeList.accept(NodeList.java:275)
	at com.github.javaparser.ast.visitor.ModifierVisitor.modifyList(ModifierVisitor.java:1172)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:171)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:72)
	at com.github.javaparser.ast.visitor.ModifierVisitor.lambda$visit$60(ModifierVisitor.java:674)
	at java.base/java.util.Optional.map(Optional.java:260)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:674)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:100)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:1133)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.NodeList.accept(NodeList.java:275)
	at com.github.javaparser.ast.visitor.ModifierVisitor.modifyList(ModifierVisitor.java:1172)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:246)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:97)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:1133)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:53)
	at com.github.javaparser.ast.NodeList.accept(NodeList.java:275)
	at com.github.javaparser.ast.visitor.ModifierVisitor.modifyList(ModifierVisitor.java:1172)
	at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:310)
	at com.examples.input.TestExecutorClass.main(TestExecutorClass.java:31)


Since the CustomException class is a part of the source code, I believe that should be resolved successfully. Here is the reproducible sample repository
The resolve method here is causing the exception.
Interestingly, if I change the java file like below
package com.examples.input;
public class Test1 {
    void test(CustomException arg) {
        
    }
}

The CustomException arg is successfully resolved in the public Visitable visit(Parameter n, Void arg) method. This give me a sense that problem might be associated with the catch block parameter.
Please let me know I am doing it incorrectly. Any suggestions is appreciated.

Closing comment (2023-11-29T08:52:36Z): Thank you for reporting this anomaly. Indeed, parameter resolution is currently only carried out on methods and constructors.
Related to #3111