Home | History | Annotate | Download | only in resolution
      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.ParseException;
     20 import com.github.javaparser.ast.CompilationUnit;
     21 import com.github.javaparser.ast.body.MethodDeclaration;
     22 import com.github.javaparser.ast.expr.MethodCallExpr;
     23 import com.github.javaparser.ast.expr.NameExpr;
     24 import com.github.javaparser.resolution.MethodUsage;
     25 import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
     26 import com.github.javaparser.symbolsolver.javaparser.Navigator;
     27 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
     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.ReflectionTypeSolver;
     31 import org.junit.Test;
     32 
     33 import static org.junit.Assert.assertEquals;
     34 import static org.junit.Assert.assertTrue;
     35 
     36 public class StatementContextResolutionTest extends AbstractResolutionTest {
     37 
     38     @Test
     39     public void resolveLocalVariableInParentOfParent() {
     40         CompilationUnit cu = parseSample("LocalVariableInParent");
     41         com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
     42         MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo1");
     43         NameExpr nameExpr = Navigator.findNameExpression(method, "s").get();
     44 
     45         SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(new ReflectionTypeSolver()).solve(nameExpr);
     46         assertTrue(ref.isSolved());
     47         assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
     48     }
     49 
     50     @Test
     51     public void resolveLocalVariableInParent() {
     52         CompilationUnit cu = parseSample("LocalVariableInParent");
     53         com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
     54         MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo3");
     55         NameExpr nameExpr = Navigator.findNameExpression(method, "s").get();
     56 
     57         SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(new ReflectionTypeSolver()).solve(nameExpr);
     58         assertTrue(ref.isSolved());
     59         assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
     60     }
     61 
     62     @Test
     63     public void resolveLocalVariableInSameParent() {
     64         CompilationUnit cu = parseSample("LocalVariableInParent");
     65         com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
     66         MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo2");
     67         NameExpr nameExpr = Navigator.findNameExpression(method, "s").get();
     68 
     69         SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(new ReflectionTypeSolver()).solve(nameExpr);
     70         assertTrue(ref.isSolved());
     71         assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
     72     }
     73 
     74     @Test
     75     public void resolveLocalAndSeveralAnnidatedLevels() {
     76         CompilationUnit cu = parseSample("LocalVariableInParent");
     77         com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
     78         MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo4");
     79         MethodCallExpr call = Navigator.findMethodCall(method, "add").get();
     80 
     81         TypeSolver typeSolver = new ReflectionTypeSolver();
     82 
     83         SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(typeSolver).solve(call.getScope().get());
     84         assertTrue(ref.isSolved());
     85         assertEquals("java.util.List<Comment>", ref.getCorrespondingDeclaration().getType().describe());
     86 
     87         MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
     88         assertEquals("add", methodUsage.getName());
     89     }
     90 
     91     @Test
     92     public void resolveMethodOnGenericClass() {
     93         CompilationUnit cu = parseSample("LocalVariableInParent");
     94         com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
     95         MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo5");
     96         MethodCallExpr call = Navigator.findMethodCall(method, "add").get();
     97 
     98         TypeSolver typeSolver = new ReflectionTypeSolver();
     99 
    100         SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(typeSolver).solve(call.getScope().get());
    101         assertTrue(ref.isSolved());
    102         assertEquals("java.util.List<Comment>", ref.getCorrespondingDeclaration().getType().describe());
    103 
    104         MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
    105         assertEquals("add", methodUsage.getName());
    106     }
    107 
    108 }
    109