1/*
2 * Copyright (c) 2017, 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 8173945
27 * @summary Test Elements.getAll{Type, Package, Module}Elements
28 * @library /tools/javac/lib
29 * @modules java.compiler
30 *          jdk.compiler
31 * @build   JavacTestingAbstractProcessor TestAllFoos
32 * @compile -processor TestAllFoos -proc:only --release 8 --source-path modules/m1/pkg  modules/m1/pkg/C.java
33 * @compile -processor TestAllFoos -proc:only --release 8 --source-path modules/m2/pkg  modules/m2/pkg/C.java
34 */
35// @compile -processor TestAllFoos -proc:only             --module-source-path  modules -m m1,m2
36
37import java.util.Set;
38import static java.util.Objects.*;
39import javax.annotation.processing.*;
40import static javax.lang.model.SourceVersion.*;
41import javax.lang.model.element.*;
42import javax.lang.model.util.*;
43
44/**
45 * Test basic workings of Elements.getAll{Type, Package, Module}Elements under
46 * pre- and post-modules.
47 */
48public class TestAllFoos extends JavacTestingAbstractProcessor {
49    public boolean process(Set<? extends TypeElement> annotations,
50                           RoundEnvironment roundEnv) {
51        if (!roundEnv.processingOver()) {
52            boolean expectModules =
53                (processingEnv.getSourceVersion().compareTo(RELEASE_9) >= 0);
54
55            testSetSize(eltUtils.getAllTypeElements("java.lang.String"), 1);
56            testSetSize(eltUtils.getAllTypeElements("example.com"), 0);
57
58            if (!expectModules) {
59                // Expect empty modules set, single package named "pkg" with one type "pkg.C".
60                testSetSize(eltUtils.getAllModuleElements(), 0);
61                testSetSize(eltUtils.getAllPackageElements("pkg"), 1);
62                testSetSize(eltUtils.getAllTypeElements("pkg.C"),  1);
63            } else {
64                Set<? extends ModuleElement> modules =
65                    requireNonNull(eltUtils.getAllModuleElements());
66
67                ModuleElement m1 = requireNonNull(eltUtils.getModuleElement("m1"));
68                ModuleElement m2 = requireNonNull(eltUtils.getModuleElement("m2"));
69
70                if (!modules.contains(m1) ||
71                    !modules.contains(m2) ||
72                    !modules.contains(requireNonNull(eltUtils.getModuleElement("java.base"))))
73                    throw new RuntimeException("Missing modules " + modules);
74
75                // Expect two packages named "pkg" and two types named "pkg.C".
76                testSetSize(eltUtils.getAllPackageElements("pkg"), 2);
77                testSetSize(eltUtils.getAllTypeElements("pkg.C"),  2);
78            }
79        }
80        return true;
81    }
82
83    /**
84     * Check the set argument against null and throw an exception if
85     * the set is not of the expected size.
86     */
87    private static <E> Set<E> testSetSize(Set<E> set, int expectedSize) {
88        requireNonNull(set);
89        if (set.size() != expectedSize)
90            throw new RuntimeException("Unexpected size of set " + set);
91        return set;
92    }
93}
94