JSources extends JNode { List<JPackage> getHierarchyPackages(List<JavaPackage> packages) { Map<String, JPackage> pkgMap = new HashMap<String, JPackage>(); for (JavaPackage pkg : packages) { addPackage(pkgMap, new JPackage(pkg)); } boolean repeat; do { repeat = false; for (JPackage pkg : pkgMap.values()) { if (pkg.getInnerPackages().size() == 1 && pkg.getClasses().isEmpty()) { JPackage innerPkg = pkg.getInnerPackages().get(0); pkg.getInnerPackages().clear(); pkg.getInnerPackages().addAll(innerPkg.getInnerPackages()); pkg.getClasses().addAll(innerPkg.getClasses()); pkg.setName(pkg.getName() + "." + innerPkg.getName()); innerPkg.getInnerPackages().clear(); innerPkg.getClasses().clear(); repeat = true; break; } } } while (repeat); for (Iterator<Map.Entry<String, JPackage>> it = pkgMap.entrySet().iterator(); it.hasNext(); ) { JPackage pkg = it.next().getValue(); if (pkg.getInnerPackages().isEmpty() && pkg.getClasses().isEmpty()) { it.remove(); } } Set<JPackage> innerPackages = Collections.newSetFromMap(new IdentityHashMap<JPackage, Boolean>()); for (JPackage pkg : pkgMap.values()) { innerPackages.addAll(pkg.getInnerPackages()); } List<JPackage> rootPkgs = new ArrayList<JPackage>(); for (JPackage pkg : pkgMap.values()) { if (!innerPackages.contains(pkg)) { rootPkgs.add(pkg); } } Collections.sort(rootPkgs); return rootPkgs; } JSources(JRoot jRoot, JadxWrapper wrapper); final void update(); @Override Icon getIcon(); @Override JClass getJParent(); @Override String makeString();  }
@Test public void testHierarchyPackages() { String pkgName = "a.b.c.d.e"; List<JavaPackage> packages = Arrays.asList(newPkg(pkgName)); List<JPackage> out = sources.getHierarchyPackages(packages); assertEquals(out.size(), 1); JPackage jpkg = out.get(0); assertEquals(jpkg.getName(), pkgName); assertEquals(jpkg.getClasses().size(), 1); }
@Test public void testHierarchyPackages2() { List<JavaPackage> packages = Arrays.asList( newPkg("a.b"), newPkg("a.c"), newPkg("a.d") ); List<JPackage> out = sources.getHierarchyPackages(packages); assertEquals(out.size(), 1); JPackage jpkg = out.get(0); assertEquals(jpkg.getName(), "a"); assertEquals(jpkg.getClasses().size(), 0); assertEquals(jpkg.getInnerPackages().size(), 3); }
@Test public void testHierarchyPackages3() { List<JavaPackage> packages = Arrays.asList( newPkg("a.b.p1"), newPkg("a.b.p2"), newPkg("a.b.p3") ); List<JPackage> out = sources.getHierarchyPackages(packages); assertEquals(out.size(), 1); JPackage jpkg = out.get(0); assertEquals(jpkg.getName(), "a.b"); assertEquals(jpkg.getClasses().size(), 0); assertEquals(jpkg.getInnerPackages().size(), 3); }
@Test public void testHierarchyPackages4() { List<JavaPackage> packages = Arrays.asList( newPkg("a.p1"), newPkg("a.b.c.p2"), newPkg("a.b.c.p3"), newPkg("d.e"), newPkg("d.f.a") ); List<JPackage> out = sources.getHierarchyPackages(packages); assertEquals(out.size(), 2); assertEquals(out.get(0).getName(), "a"); assertEquals(out.get(0).getInnerPackages().size(), 2); assertEquals(out.get(1).getName(), "d"); assertEquals(out.get(1).getInnerPackages().size(), 2); }