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.Expression; 23 import com.github.javaparser.ast.expr.MethodCallExpr; 24 import com.github.javaparser.ast.stmt.SwitchStmt; 25 import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration; 26 import com.github.javaparser.resolution.types.ResolvedType; 27 import com.github.javaparser.symbolsolver.javaparser.Navigator; 28 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; 29 import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; 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 EnumResolutionTest extends AbstractResolutionTest { 37 38 @Test 39 public void switchOnEnum() { 40 CompilationUnit cu = parseSample("SwitchOnEnum"); 41 com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SwitchOnEnum"); 42 MethodDeclaration method = Navigator.demandMethod(clazz, "foo"); 43 SwitchStmt switchStmt = Navigator.findSwitch(method); 44 Expression expression = switchStmt.getEntries().get(0).getLabel().get(); 45 46 SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression); 47 assertTrue(ref.isSolved()); 48 assertEquals("SwitchOnEnum.MyEnum", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName()); 49 } 50 51 @Test 52 public void enumAndStaticInitializer() { 53 CompilationUnit cu = parseSample("EnumAndStaticInitializer"); 54 com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass"); 55 MethodCallExpr call = Navigator.findMethodCall(clazz, "put").get(); 56 57 ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(call); 58 assertEquals("MyClass.Primitive", ref.describe()); 59 } 60 61 } 62