UnnamedModuleUnnamedPackageTest.java revision 3612:e666d0f958f6
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8161501
27 * @summary JSR269 jigsaw update: javax.lang.model.element.ModuleElement.getEnclosedElements() on unnamed module with unnamed package
28 * @compile UnnamedModuleUnnamedPackageTest.java
29 * @compile -processor UnnamedModuleUnnamedPackageTest UnnamedModuleUnnamedPackageTest.java EmptyClass.java
30 */
31
32import javax.annotation.processing.AbstractProcessor;
33import javax.annotation.processing.RoundEnvironment;
34import javax.annotation.processing.SupportedAnnotationTypes;
35import javax.lang.model.SourceVersion;
36import javax.lang.model.element.*;
37
38import java.util.*;
39import java.util.stream.Collectors;
40
41@SupportedAnnotationTypes("*")
42public class UnnamedModuleUnnamedPackageTest extends AbstractProcessor {
43    static final Set<String> expected = new HashSet<>(Arrays.asList("unnamed package", "pkg1"));
44
45    @Override
46    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
47        for (Element e: roundEnv.getRootElements()) {
48            Element m = e.getEnclosingElement();
49            while (!(m instanceof ModuleElement)) {
50                m = m.getEnclosingElement();
51            }
52            Set<String> found = m.getEnclosedElements().stream()
53                .map(p -> ((PackageElement)p).isUnnamed() ?
54                                        "unnamed package" :
55                                        ((PackageElement)p).getQualifiedName().toString())
56                .collect(Collectors.toSet());
57            if (!Objects.equals(expected, found)) {
58                System.err.println("expected: " + expected);
59                System.err.println("found: " + found);
60                throw new AssertionError("unexpected packages found");
61            }
62        }
63        return false;
64    }
65
66    @Override
67    public SourceVersion getSupportedSourceVersion() {
68        return SourceVersion.latest();
69    }
70}
71