Skip to content

Commit

Permalink
Merge pull request #4430 from apiiro/fix-4427-resolution-of-extends-a…
Browse files Browse the repository at this point in the history
…nd-implements

Make resoltuion of implements and extends types start with the parent…
  • Loading branch information
jlerbsc committed May 22, 2024
2 parents df6de91 + 0c20417 commit 1009e8e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,26 @@ private ResolvedReferenceType toReferenceType(ClassOrInterfaceType classOrInterf
// look for the qualified name (for example class of type Rectangle2D.Double)
className = classOrInterfaceType.getScope().get().toString() + "." + className;
}
SymbolReference<ResolvedTypeDeclaration> ref = solveType(className);

// Since this is used to resolve reference to "extended" and "implemented" types, and since these type references
// should not be resolved against member types of the current type, we resolve based on the context containing
// the class declaration.
SymbolReference<ResolvedTypeDeclaration> ref = getContext().getParent()
.orElseThrow(() -> new RuntimeException("Parent context unexpectedly empty."))
.solveType(className);

// If unable to solve by the class name alone, attempt to qualify it.
if (!ref.isSolved()) {
Optional<ClassOrInterfaceType> localScope = classOrInterfaceType.getScope();
if (localScope.isPresent()) {
String localName = localScope.get().getName().getId() + "." + classOrInterfaceType.getName().getId();
ref = solveType(localName);
ref = getContext().getParent()
.orElseThrow(() -> new RuntimeException("Parent context unexpectedly empty."))
.solveType(localName);
}
}


// If still unable to resolve, throw an exception.
if (!ref.isSolved()) {
throw new UnsolvedSymbolException(classOrInterfaceType.getName().getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.javaparser.symbolsolver;

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class Issue4427Test extends AbstractSymbolResolutionTest {
@Test
public void testIssue4427() throws IOException {
Path issueResourcesPath = adaptPath("src/test/resources/issue4427");
ReflectionTypeSolver rts = new ReflectionTypeSolver();
JavaParserTypeSolver jpts = new JavaParserTypeSolver(issueResourcesPath);
CombinedTypeSolver cts = new CombinedTypeSolver();
cts.add(rts);
cts.add(jpts);
ParserConfiguration pc = new ParserConfiguration()
.setSymbolResolver(new JavaSymbolSolver(cts));
StaticJavaParser.setConfiguration(pc);
CompilationUnit cu = StaticJavaParser.parse(issueResourcesPath.resolve("DerivedClass.java"));

// We shouldn't throw a mismatched symbol
assertDoesNotThrow(() -> cu.findAll(NameExpr.class).stream()
.map(NameExpr::resolve)
.findAny().get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public abstract class BaseClass {

protected int x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class DerivedClass extends BaseClass {

void f() {
x = x + 1;
}


static class BaseClass extends SecondBaseClass {

}
}

0 comments on commit 1009e8e

Please sign in to comment.