• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.github.javaparser.symbolsolver;
2 
3 import com.github.javaparser.JavaParser;
4 import com.github.javaparser.ParseResult;
5 import com.github.javaparser.ParseStart;
6 import com.github.javaparser.ParserConfiguration;
7 import com.github.javaparser.Providers;
8 import com.github.javaparser.ast.CompilationUnit;
9 import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
10 import com.github.javaparser.ast.expr.MethodCallExpr;
11 import com.github.javaparser.ast.expr.SimpleName;
12 import com.github.javaparser.resolution.UnsolvedSymbolException;
13 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
14 import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
15 import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
16 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
17 import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
18 import org.junit.jupiter.api.BeforeEach;
19 import org.junit.jupiter.api.Test;
20 
21 import java.util.List;
22 
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.junit.jupiter.api.Assertions.fail;
26 import java.time.Duration;
27 import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
28 
29 /**
30  * @author Dominik Hardtke
31  * @since 02/02/2018
32  */
33 class Issue1364Test extends AbstractResolutionTest {
34     private JavaParser javaParser;
35 
36     @BeforeEach
setup()37     void setup() {
38         ClassOrInterfaceDeclaration fakeObject = new ClassOrInterfaceDeclaration();
39         fakeObject.setName(new SimpleName("java.lang.Object"));
40 
41         TypeSolver typeSolver = new TypeSolver() {
42             @Override
43             public TypeSolver getParent() {
44                 return null;
45             }
46 
47             @Override
48             public void setParent(TypeSolver parent) {
49             }
50 
51             @Override
52             public SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name) {
53                 if ("java.lang.Object".equals(name)) {
54                     // custom handling
55                     return SymbolReference.solved(new JavaParserClassDeclaration(fakeObject, this));
56                 }
57 
58                 return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
59             }
60         };
61 
62         ParserConfiguration config = new ParserConfiguration();
63         config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
64         javaParser = new JavaParser(config);
65     }
66 
67     @Test
resolveSubClassOfObject()68     void resolveSubClassOfObject() {
69         assertTimeoutPreemptively(Duration.ofMillis(1000L), () -> {
70             String code = String.join(System.lineSeparator(), "package graph;", "public class Vertex {", "    public static void main(String[] args) {", "        System.out.println();", "    }", "}");
71         ParseResult<CompilationUnit> parseResult = javaParser.parse(ParseStart.COMPILATION_UNIT, Providers.provider(code));
72         assertTrue(parseResult.isSuccessful());
73         assertTrue(parseResult.getResult().isPresent());
74         List<MethodCallExpr> methodCallExprs = parseResult.getResult().get().findAll(MethodCallExpr.class);
75         assertEquals(1, methodCallExprs.size());
76         try {
77             methodCallExprs.get(0).calculateResolvedType();
78         fail("An UnsolvedSymbolException should be thrown");
79     } catch (UnsolvedSymbolException ignored) {
80     }
81     });
82 
83 
84 
85 }
86 }
87 
88