Skip to content

Commit

Permalink
Merge pull request #4135 from jlerbsc/master
Browse files Browse the repository at this point in the history
Fix: issue 4133 Top-level class containerType() throws an exception instead of Optional.empty()
  • Loading branch information
jlerbsc authored Aug 31, 2023
2 parents d087f19 + 4cac173 commit 49905db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.*;
import java.util.stream.Collectors;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.*;
Expand Down Expand Up @@ -157,6 +158,7 @@ public SymbolReference<ResolvedTypeDeclaration> solveType(String name) {
public Optional<ResolvedReferenceTypeDeclaration> containerType() {
return wrappedNode
.getParentNode()
.filter(parentNode -> !(parentNode instanceof CompilationUnit))
.map(node -> node.getSymbolResolver().toTypeDeclaration(node));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.github.javaparser.JavaParserAdapter;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.AccessSpecifier;
Expand All @@ -49,10 +50,10 @@
import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.AbstractSymbolResolutionTest;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionFactory;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
Expand All @@ -61,7 +62,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

class JavaParserClassDeclarationTest extends AbstractSymbolResolutionTest {
class JavaParserClassDeclarationTest extends AbstractResolutionTest {

private TypeSolver typeSolver;
private TypeSolver typeSolverNewCode;
Expand Down Expand Up @@ -957,7 +958,26 @@ void testHasInheritedAnnotation() throws IOException {

// Set<TypeDeclaration> internalTypes()

// Optional<TypeDeclaration> containerType()
// issue #4133
@Test
void testContainerType() {
String code =
"public class Foo {\n"
+ " public static class Bar {\n"
+ " public static class Baz {\n"
+ " }\n"
+ " }\n"
+ "}\n";

JavaParserAdapter parser = JavaParserAdapter.of(createParserWithResolver(defaultTypeSolver()));
CompilationUnit cu = parser.parse(code);

List<ClassOrInterfaceDeclaration> declarations = cu.findAll(ClassOrInterfaceDeclaration.class);
// top level type
assertFalse(declarations.get(0).resolve().asReferenceType().containerType().isPresent());
assertEquals("Foo", declarations.get(1).resolve().asReferenceType().containerType().get().getQualifiedName());
assertEquals("Foo.Bar", declarations.get(2).resolve().asReferenceType().containerType().get().getQualifiedName());
}


@Test
Expand Down

0 comments on commit 49905db

Please sign in to comment.