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.ParseException;
     20 import com.github.javaparser.ast.CompilationUnit;
     21 import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
     22 import com.github.javaparser.resolution.declarations.ResolvedDeclaration;
     23 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
     24 import com.github.javaparser.symbolsolver.javaparser.Navigator;
     25 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
     26 import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
     27 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
     28 import com.google.common.collect.ImmutableSet;
     29 import org.junit.Test;
     30 
     31 import java.util.stream.Collectors;
     32 
     33 import static org.junit.Assert.assertEquals;
     34 
     35 public class FindingAllFields extends AbstractResolutionTest {
     36 
     37     @Test
     38     public void findAllInheritedFields() {
     39         CompilationUnit cu = parseSample("AClassWithFields");
     40         ClassOrInterfaceDeclaration classC = Navigator.demandClass(cu, "C");
     41         ResolvedReferenceTypeDeclaration typeDeclaration = JavaParserFacade.get(new ReflectionTypeSolver()).getTypeDeclaration(classC);
     42         assertEquals(3, typeDeclaration.getAllFields().size());
     43         assertEquals(ImmutableSet.of("a", "b", "c"),
     44                 typeDeclaration.getAllFields().stream().map(ResolvedDeclaration::getName).collect(Collectors.toSet()));
     45     }
     46 
     47     @Test
     48     public void findAllInheritedFieldsAndGenerics() {
     49         CompilationUnit cu = parseSample("AClassWithFieldsAndGenerics");
     50         ClassOrInterfaceDeclaration classC = Navigator.demandClass(cu, "C");
     51         ResolvedReferenceTypeDeclaration typeDeclaration = JavaParserFacade.get(new ReflectionTypeSolver()).getTypeDeclaration(classC);
     52         assertEquals(3, typeDeclaration.getAllFields().size());
     53         assertEquals(ImmutableSet.of("a", "b", "c"),
     54                 typeDeclaration.getAllFields().stream().map(ResolvedDeclaration::getName).collect(Collectors.toSet()));
     55         assertEquals("java.util.List<java.lang.String>", typeDeclaration.getField("b").getType().describe());
     56     }
     57 }
     58