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.resolution; 18 19 import com.github.javaparser.JavaParser; 20 import com.github.javaparser.ast.expr.MethodCallExpr; 21 import com.github.javaparser.ast.visitor.VoidVisitor; 22 import com.github.javaparser.ast.visitor.VoidVisitorAdapter; 23 import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration; 24 import com.github.javaparser.symbolsolver.AbstractTest; 25 import com.github.javaparser.symbolsolver.javaparser.Navigator; 26 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; 27 import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration; 28 import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; 29 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; 30 import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; 31 import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; 32 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; 33 import org.junit.Before; 34 import org.junit.Test; 35 36 import java.io.File; 37 38 import static org.junit.Assert.assertEquals; 39 import static org.junit.Assert.assertTrue; 40 41 /** 42 * @author Federico Tomassetti 43 */ 44 public class SymbolSolverTest extends AbstractTest { 45 46 private TypeSolver typeSolverNewCode; 47 private SymbolSolver symbolSolver; 48 49 @Before 50 public void setup() { 51 52 File srcNewCode = adaptPath(new File("src/test/test_sourcecode/javaparser_new_src/javaparser-core")); 53 CombinedTypeSolver combinedTypeSolverNewCode = new CombinedTypeSolver(); 54 combinedTypeSolverNewCode.add(new ReflectionTypeSolver()); 55 combinedTypeSolverNewCode.add(new JavaParserTypeSolver(srcNewCode)); 56 combinedTypeSolverNewCode.add(new JavaParserTypeSolver(adaptPath(new File("src/test/test_sourcecode/javaparser_new_src/javaparser-generated-sources")))); 57 typeSolverNewCode = combinedTypeSolverNewCode; 58 59 symbolSolver = new SymbolSolver(typeSolverNewCode); 60 } 61 62 @Test 63 public void testSolveSymbolUnexisting() { 64 JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolverNewCode.solveType("com.github.javaparser.ast.body.ConstructorDeclaration"); 65 66 SymbolReference<? extends ResolvedValueDeclaration> res = symbolSolver.solveSymbolInType(constructorDeclaration, "unexisting"); 67 assertEquals(false, res.isSolved()); 68 } 69 70 @Test 71 public void testSolveSymbolToDeclaredField() { 72 JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolverNewCode.solveType("com.github.javaparser.ast.body.ConstructorDeclaration"); 73 74 SymbolReference<? extends ResolvedValueDeclaration> res = symbolSolver.solveSymbolInType(constructorDeclaration, "name"); 75 assertEquals(true, res.isSolved()); 76 assertEquals(true, res.getCorrespondingDeclaration().isField()); 77 } 78 79 @Test 80 public void testSolveSymbolToInheritedPublicField() { 81 JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolverNewCode.solveType("com.github.javaparser.ast.body.ConstructorDeclaration"); 82 83 SymbolReference<? extends ResolvedValueDeclaration> res = symbolSolver.solveSymbolInType(constructorDeclaration, "NODE_BY_BEGIN_POSITION"); 84 assertEquals(true, res.isSolved()); 85 assertEquals(true, res.getCorrespondingDeclaration().isField()); 86 } 87 88 @Test 89 public void testSolveSymbolToInheritedPrivateField() { 90 JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolverNewCode.solveType("com.github.javaparser.ast.body.ConstructorDeclaration"); 91 92 SymbolReference<? extends ResolvedValueDeclaration> res = symbolSolver.solveSymbolInType(constructorDeclaration, "parentNode"); 93 assertEquals(false, res.isSolved()); 94 } 95 96 @Test 97 public void testSolvePackageLocalClass() { 98 assertTrue(typeSolverNewCode.solveType("com.github.javaparser.FooClass").isClass()); 99 } 100 } 101