Home | History | Annotate | Download | only in symbolsolver
      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.JavaParser;
     20 import com.github.javaparser.ParseException;
     21 import com.github.javaparser.ast.CompilationUnit;
     22 import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
     23 import com.github.javaparser.ast.body.MethodDeclaration;
     24 import com.github.javaparser.ast.expr.Expression;
     25 import com.github.javaparser.ast.expr.FieldAccessExpr;
     26 import com.github.javaparser.ast.stmt.ExpressionStmt;
     27 import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
     28 import com.github.javaparser.resolution.types.ResolvedType;
     29 import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
     30 import com.github.javaparser.symbolsolver.javaparser.Navigator;
     31 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
     32 import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
     33 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
     34 import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
     35 import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
     36 import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
     37 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
     38 import org.junit.Test;
     39 import java.io.File;
     40 import java.io.FileNotFoundException;
     41 
     42 import static org.junit.Assert.assertEquals;
     43 import static org.junit.Assert.assertNotNull;
     44 
     45 public class Issue300 extends AbstractResolutionTest {
     46 
     47     @Test
     48     public void fieldAccessIssue() throws ParseException, FileNotFoundException {
     49         String pathToSourceFile = adaptPath("src/test/resources/issue300/Issue300.java");
     50         CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile));
     51 
     52         final FieldAccessExpr fieldAccess = Navigator.findNodeOfGivenClass(cu, FieldAccessExpr.class);
     53         assertNotNull(fieldAccess);
     54 
     55         TypeSolver typeSolver = new CombinedTypeSolver(
     56                 new ReflectionTypeSolver(),
     57                 new JavaParserTypeSolver(adaptPath(new File("src/test/resources/issue300"))));
     58         final JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
     59         final SymbolReference<? extends ResolvedValueDeclaration> ref = javaParserFacade.solve(fieldAccess);
     60         assertEquals(ResolvedPrimitiveType.INT, ref.getCorrespondingDeclaration().getType().asPrimitive());
     61     }
     62 }
     63 
     64