Skip to content

Commit

Permalink
Additional test case for extends rather than implements, and fix;
Browse files Browse the repository at this point in the history
  • Loading branch information
MysterAitch committed May 4, 2019
1 parent c53cc4d commit 03bc9d8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
Expand Up @@ -91,8 +91,14 @@ public Optional<ResolvedType> solveGenericType(String name) {
@Override @Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name) { public SymbolReference<ResolvedTypeDeclaration> solveType(String name) {
// First attempt to resolve against implemented classes - cf. issue #2195 // First attempt to resolve against implemented classes - cf. issue #2195
for (ClassOrInterfaceType classOrInterfaceType : wrappedNode.getImplementedTypes()) { for (ClassOrInterfaceType implementedType : wrappedNode.getImplementedTypes()) {
if (classOrInterfaceType.getName().getId().equals(name)) { if (implementedType.getName().getId().equals(name)) {
return JavaParserFactory.getContext(wrappedNode.getParentNode().orElse(null), typeSolver).solveType(name);
}
}

for (ClassOrInterfaceType extendedType : wrappedNode.getExtendedTypes()) {
if (extendedType.getName().getId().equals(name)) {
return JavaParserFactory.getContext(wrappedNode.getParentNode().orElse(null), typeSolver).solveType(name); return JavaParserFactory.getContext(wrappedNode.getParentNode().orElse(null), typeSolver).solveType(name);
} }
} }
Expand Down
Expand Up @@ -15,13 +15,14 @@


import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;


public class TypeResolutionWithSameNameTest extends AbstractResolutionTest { public class TypeResolutionWithSameNameTest extends AbstractResolutionTest {


@Test @Test
void testTypesWithSameNameInPackageAndNested() throws IOException { void testTypesWithSameNameInPackageAndNested_directImplements() throws IOException {
Path srcRootPath = adaptPath("src/test/resources/TypeResolutionWithSameNameTest"); Path srcRootPath = adaptPath("src/test/resources/TypeResolutionWithSameNameTest/src2");
Path extendingTypePath = adaptPath("src/test/resources/TypeResolutionWithSameNameTest/testresource/A.java"); Path extendingTypePath = adaptPath("src/test/resources/TypeResolutionWithSameNameTest/src2/testresource/A.java");


JavaParserTypeSolver javaParserTypeSolver = new JavaParserTypeSolver(srcRootPath); JavaParserTypeSolver javaParserTypeSolver = new JavaParserTypeSolver(srcRootPath);
StaticJavaParser StaticJavaParser
Expand All @@ -32,6 +33,7 @@ void testTypesWithSameNameInPackageAndNested() throws IOException {
ClassOrInterfaceDeclaration extendingTypeClass = Navigator.demandClass(cu, "A"); ClassOrInterfaceDeclaration extendingTypeClass = Navigator.demandClass(cu, "A");


// Attempt to resolve `DuplicateTypeName` from `class ExtendingType implements **DuplicateTypeName**` // Attempt to resolve `DuplicateTypeName` from `class ExtendingType implements **DuplicateTypeName**`
assumeTrue(extendingTypeClass.getImplementedTypes().size() > 0);
ClassOrInterfaceType implementedType = extendingTypeClass.getImplementedTypes(0); ClassOrInterfaceType implementedType = extendingTypeClass.getImplementedTypes(0);
ResolvedReferenceType resolvedImplementedType = implementedType.resolve(); ResolvedReferenceType resolvedImplementedType = implementedType.resolve();


Expand All @@ -41,4 +43,29 @@ void testTypesWithSameNameInPackageAndNested() throws IOException {
assertEquals("testresource.DuplicateTypeName", qualifiedName, "Error - not resolved to interface in separate file."); assertEquals("testresource.DuplicateTypeName", qualifiedName, "Error - not resolved to interface in separate file.");
assertNotEquals("testresource.ExtendingType.DuplicateTypeName", qualifiedName, "Error - resolved to nested class."); assertNotEquals("testresource.ExtendingType.DuplicateTypeName", qualifiedName, "Error - resolved to nested class.");
} }

@Test
void testTypesWithSameNameInPackageAndNested_directExtends() throws IOException {
Path srcRootPath = adaptPath("src/test/resources/TypeResolutionWithSameNameTest/src1");
Path extendingTypePath = adaptPath("src/test/resources/TypeResolutionWithSameNameTest/src1/testresource/A.java");

JavaParserTypeSolver javaParserTypeSolver = new JavaParserTypeSolver(srcRootPath);
StaticJavaParser
.getConfiguration()
.setSymbolResolver(new JavaSymbolSolver(javaParserTypeSolver));

CompilationUnit cu = StaticJavaParser.parse(extendingTypePath);
ClassOrInterfaceDeclaration extendingTypeClass = Navigator.demandClass(cu, "A");

// Attempt to resolve `DuplicateTypeName` from `class ExtendingType implements **DuplicateTypeName**`
assumeTrue(extendingTypeClass.getExtendedTypes().size() > 0);
ClassOrInterfaceType implementedType = extendingTypeClass.getExtendedTypes(0);
ResolvedReferenceType resolvedImplementedType = implementedType.resolve();

// Verify qualified name matches the non-nested class in the same package.
// Note verbose assertions show both the "correct" expected value, and the erroneous value to be avoided.
String qualifiedName = resolvedImplementedType.getQualifiedName();
assertEquals("testresource.DuplicateTypeName", qualifiedName, "Error - not resolved to interface in separate file.");
assertNotEquals("testresource.ExtendingType.DuplicateTypeName", qualifiedName, "Error - resolved to nested class.");
}
} }
@@ -0,0 +1,7 @@
package testresource;

public class A extends DuplicateTypeName {
class DuplicateTypeName extends A {

}
}
@@ -0,0 +1,5 @@
package testresource;

public abstract class DuplicateTypeName {

}

0 comments on commit 03bc9d8

Please sign in to comment.