1 package com.github.javaparser.symbolsolver.resolution.javaparser; 2 3 import com.github.javaparser.JavaParser; 4 import com.github.javaparser.ParseStart; 5 import com.github.javaparser.ParserConfiguration; 6 import com.github.javaparser.ast.CompilationUnit; 7 import com.github.javaparser.ast.type.VarType; 8 import com.github.javaparser.resolution.types.ResolvedPrimitiveType; 9 import com.github.javaparser.resolution.types.ResolvedType; 10 import com.github.javaparser.symbolsolver.JavaSymbolSolver; 11 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; 12 import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl; 13 import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration; 14 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; 15 import org.junit.Test; 16 17 import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_10; 18 import static com.github.javaparser.Providers.provider; 19 import static org.junit.Assert.assertEquals; 20 21 public class VarTypeTest { 22 private final TypeSolver typeSolver = new ReflectionTypeSolver(); 23 private final JavaParser javaParser = new JavaParser(new ParserConfiguration() 24 .setLanguageLevel(JAVA_10) 25 .setSymbolResolver(new JavaSymbolSolver(typeSolver))); 26 27 @Test 28 public void resolveAPrimitive() { 29 CompilationUnit ast = javaParser.parse(ParseStart.COMPILATION_UNIT, provider("class X{void x(){var abc = 1;}}")).getResult().get(); 30 VarType varType = ast.findFirst(VarType.class).get(); 31 32 ResolvedType resolvedType = varType.resolve(); 33 34 assertEquals(ResolvedPrimitiveType.INT, resolvedType); 35 } 36 37 @Test 38 public void resolveAReferenceType() { 39 CompilationUnit ast = javaParser.parse(ParseStart.COMPILATION_UNIT, provider("class X{void x(){var abc = \"\";}}")).getResult().get(); 40 VarType varType = ast.findFirst(VarType.class).get(); 41 42 ResolvedType resolvedType = varType.resolve(); 43 44 assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver), resolvedType); 45 } 46 47 @Test(expected = IllegalStateException.class) 48 public void failResolveNoInitializer() { 49 CompilationUnit ast = javaParser.parse(ParseStart.COMPILATION_UNIT, provider("class X{void x(){var abc;}}")).getResult().get(); 50 VarType varType = ast.findFirst(VarType.class).get(); 51 52 varType.resolve(); 53 } 54 55 @Test(expected = IllegalStateException.class) 56 public void failResolveWrongLocation() { 57 CompilationUnit ast = javaParser.parse(ParseStart.COMPILATION_UNIT, provider("class X{void x(var x){};}")).getResult().get(); 58 VarType varType = ast.findFirst(VarType.class).get(); 59 60 varType.resolve(); 61 } 62 } 63