DATypeImportComputer { public static Collection<DAImport> computeImports(DAType daType) { List<DAImport> qualifiedName = hasQualifiedNameToImport(daType) ? Collections.<DAImport>emptyList() : Collections.singletonList(DAImportImpl.from(daType.getQualifiedName())); Set<DAImport> res = new HashSet<DAImport>(); addAll(res, qualifiedName); List<Collection<DAImport>> argsImportLists = from(daType.getTypeArgs()).transform(COMPUTE_IMPORTS).toList(); for (Collection<DAImport> importList : argsImportLists) { addAll(res, importList); } addAll(res, daType.getSuperBound() == null ? Collections.<DAImport>emptyList() : computeImports(daType.getSuperBound()) ); addAll(res, daType.getExtendsBound() == null ? Collections.<DAImport>emptyList() : computeImports(daType.getExtendsBound())); return res; }  static Collection<DAImport> computeImports(DAType daType);  }
@Test public void getImports_no_typeArgs() throws Exception { DAType daType = daType("test.Toto"); Assertions.assertThat(DATypeImportComputer.computeImports(daType)).extracting("qualifiedName.name").containsOnly("test.Toto"); }
@Test public void getImports_from_default_package_no_typeArgs() throws Exception { DAType daType = daType("Toto"); Assertions.assertThat(DATypeImportComputer.computeImports(daType)).extracting("qualifiedName.name").isEmpty(); }
@Test public void getImports_one_typeArg() throws Exception { DAType daType = daType( "test.Toto", ImmutableList.of(daType("test.Titi")) ); Assertions.assertThat(DATypeImportComputer.computeImports(daType)).extracting("qualifiedName.name").containsOnly( "test.Toto", "test.Titi" ); }
@Test public void getImports_one_typeArg_from_default_package() throws Exception { DAType daType = daType( "test.Toto", ImmutableList.of(daType("Titi")) ); Assertions.assertThat(DATypeImportComputer.computeImports(daType)).extracting("qualifiedName.name").containsOnly("test.Toto"); }
@Test public void getImports_multiple_typeArg() throws Exception { DAType daType = daType( "test.Toto", ImmutableList.of(daType("test.Titi"), daType("test.Tutu")) ); Assertions.assertThat(DATypeImportComputer.computeImports(daType)) .extracting("qualifiedName.name") .containsOnly("test.Toto", "test.Titi", "test.Tutu"); }
@Test public void getImports_one_typeArg_recursive() throws Exception { DAType daType = daType( "test.Toto", ImmutableList.of( daType("test.Titi", ImmutableList.of(daType("test.Tutu"))) ) ); Assertions.assertThat(DATypeImportComputer.computeImports(daType)) .extracting("qualifiedName.name") .containsOnly("test.Toto", "test.Titi", "test.Tutu"); }
@Test public void getImports_retrieve_imports_for_type_arguments() throws Exception { DAType daType = daType("test.Toto"); Assertions.assertThat(DATypeImportComputer.computeImports(daType)).extracting("qualifiedName.name").containsOnly("test.Toto"); daType = daType("test.Toto", ImmutableList.of(daType("test.Titi"))); Assertions.assertThat(DATypeImportComputer.computeImports(daType)).extracting("qualifiedName.name").containsOnly( "test.Toto", "test.Titi" ); }
@Test public void getImports_does_not_import_classes_from_java_lang() throws Exception { DAType daType = DATypeFactory.from(String.class); Assertions.assertThat(DATypeImportComputer.computeImports(daType)).isEmpty(); daType = daType("test.Toto", ImmutableList.of(DATypeFactory.from(Integer.class))); Assertions.assertThat(DATypeImportComputer.computeImports(daType)).extracting("qualifiedName.name").containsOnly("test.Toto"); }