PackageUtils { public static String packageName(@Nullable PackageDeclarationTree packageDeclarationTree, String separator) { if(packageDeclarationTree == null) { return ""; } Deque<String> pieces = new LinkedList<>(); ExpressionTree expr = packageDeclarationTree.packageName(); while (expr.is(Tree.Kind.MEMBER_SELECT)) { MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr; pieces.push(mse.identifier().name()); pieces.push(separator); expr = mse.expression(); } if (expr.is(Tree.Kind.IDENTIFIER)) { IdentifierTree idt = (IdentifierTree) expr; pieces.push(idt.name()); } StringBuilder sb = new StringBuilder(); for (String piece: pieces) { sb.append(piece); } return sb.toString(); } private  PackageUtils(); static String packageName(@Nullable PackageDeclarationTree packageDeclarationTree, String separator);  }
@Test void no_package_empty_string() { assertThat(packageName("class A{}")).isEmpty(); }
@Test void identifier_package() { assertThat(packageName("package foo; class A{}")).isEqualTo("foo"); }
@Test void member_select_package() { assertThat(packageName("package foo.bar.plop; class A{}")).isEqualTo("foo.bar.plop"); }
@Test void different_separator() { assertThat(packageName("package foo.bar.plop; class A{}", "/")).isEqualTo("foo/bar/plop"); }