• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Federico Tomassetti
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.github.javaparser.symbolsolver;
18 
19 import com.github.javaparser.ParseException;
20 import com.github.javaparser.ast.CompilationUnit;
21 import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
22 import com.github.javaparser.ast.body.MethodDeclaration;
23 import com.github.javaparser.resolution.declarations.ResolvedDeclaration;
24 import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
25 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
26 import com.github.javaparser.resolution.types.ResolvedType;
27 import com.github.javaparser.symbolsolver.javaparser.Navigator;
28 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
29 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
30 import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
31 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
32 import com.google.common.collect.ImmutableSet;
33 import org.junit.Test;
34 
35 import java.util.stream.Collectors;
36 
37 import static org.junit.Assert.assertEquals;
38 
39 public class JavaSymbolSolverTest extends AbstractResolutionTest {
40 
41     @Test
resolveMethodDeclaration()42     public void resolveMethodDeclaration() {
43         TypeSolver typeSolver = new ReflectionTypeSolver();
44 
45         CompilationUnit cu = parseSample("SymbolResolverExample");
46         new JavaSymbolSolver(typeSolver).inject(cu);
47 
48         MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMethods().get(0);
49         ResolvedMethodDeclaration resolvedMethodDeclaration = methodDeclaration.resolve();
50         assertEquals("foo", resolvedMethodDeclaration.getName());
51         assertEquals("A[]", resolvedMethodDeclaration.getReturnType().describe());
52         assertEquals("java.lang.String[]", resolvedMethodDeclaration.getParam(0).getType().describe());
53         assertEquals("int[]", resolvedMethodDeclaration.getParam(1).getType().describe());
54     }
55 
56     @Test
resolveArrayType()57     public void resolveArrayType() {
58         TypeSolver typeSolver = new ReflectionTypeSolver();
59 
60         CompilationUnit cu = parseSample("SymbolResolverExample");
61         new JavaSymbolSolver(typeSolver).inject(cu);
62 
63         MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMethods().get(0);
64         ResolvedType resolvedType = methodDeclaration.getType().resolve();
65         assertEquals("A[]", resolvedType.describe());
66     }
67 }
68