1 package com.github.javaparser.symbolsolver; 2 3 import com.github.javaparser.ast.expr.*; 4 import com.github.javaparser.resolution.types.ResolvedType; 5 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; 6 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; 7 import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest; 8 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; 9 import org.junit.jupiter.api.BeforeEach; 10 import org.junit.jupiter.api.Test; 11 12 import static com.github.javaparser.StaticJavaParser.parseExpression; 13 import static org.junit.jupiter.api.Assertions.assertEquals; 14 import static org.junit.jupiter.api.Assertions.assertThrows; 15 16 class Issue343Test extends AbstractResolutionTest{ 17 18 private TypeSolver typeResolver; 19 private JavaParserFacade javaParserFacade; 20 getExpressionType(TypeSolver typeSolver, Expression expression)21 private ResolvedType getExpressionType(TypeSolver typeSolver, Expression expression) { 22 return JavaParserFacade.get(typeSolver).getType(expression); 23 } 24 25 @BeforeEach setup()26 void setup() { 27 typeResolver = new ReflectionTypeSolver(); 28 javaParserFacade = JavaParserFacade.get(typeResolver); 29 } 30 31 @Test resolveStringLiteralOutsideAST()32 void resolveStringLiteralOutsideAST() { 33 assertEquals(javaParserFacade.classToResolvedType(String.class), getExpressionType(typeResolver, new StringLiteralExpr(""))); 34 } 35 36 @Test resolveIntegerLiteralOutsideAST()37 void resolveIntegerLiteralOutsideAST() { 38 assertEquals(javaParserFacade.classToResolvedType(int.class), getExpressionType(typeResolver, new IntegerLiteralExpr(2))); 39 } 40 41 @Test toResolveDoubleWeNeedTheAST()42 void toResolveDoubleWeNeedTheAST() { 43 assertThrows(IllegalStateException.class, () -> getExpressionType(typeResolver, parseExpression("new Double[]{2.0d, 3.0d}[1]"))); 44 } 45 46 47 @Test toResolveFloatWeNeedTheAST()48 void toResolveFloatWeNeedTheAST() { 49 assertThrows(IllegalStateException.class, () -> getExpressionType(typeResolver, parseExpression("new Float[]{2.0d, 3.0d}"))); 50 } 51 52 @Test resolveMethodCallOnStringLiteralOutsideAST()53 void resolveMethodCallOnStringLiteralOutsideAST() { 54 assertEquals(javaParserFacade.classToResolvedType(int.class), getExpressionType(typeResolver, new MethodCallExpr(new StringLiteralExpr("hello"), "length"))); 55 } 56 57 @Test resolveLocaleOutsideAST()58 void resolveLocaleOutsideAST() { 59 assertThrows(IllegalStateException.class, () -> getExpressionType(typeResolver, new FieldAccessExpr(new NameExpr("Locale"), "US"))); 60 } 61 } 62