1 /*
   2  * Copyright (c) 2006, 2019, 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 8230337
  27  * @summary Test Elements.getModuleOf
  28  * @library /tools/javac/lib
  29  * @modules java.compiler
  30  *          jdk.compiler
  31  * @build   JavacTestingAbstractProcessor TestGetModuleOf
  32  * @compile -processor TestGetModuleOf -proc:only TestGetModuleOf.java
  33  * @compile -processor TestGetModuleOf -proc:only -source 8 TestGetModuleOf.java
  34  */
  35 
  36 // Also run test under -source 8 to test old behavior pre-modules.
  37 
  38 import java.util.*;
  39 import javax.annotation.processing.*;
  40 import javax.lang.model.SourceVersion;
  41 import static javax.lang.model.SourceVersion.*;
  42 import javax.lang.model.element.*;
  43 import javax.lang.model.util.*;
  44 import static javax.lang.model.util.ElementFilter.*;
  45 import static javax.tools.Diagnostic.Kind.*;
  46 import static javax.tools.StandardLocation.*;
  47 
  48 /**
  49  * Test basic workings of Elements.getModuleOf
  50  */
  51 public class TestGetModuleOf extends JavacTestingAbstractProcessor {
  52     /**
  53      * Check expected behavior on classes and packages and other elements.
  54      */
  55     public boolean process(Set<? extends TypeElement> annotations,
  56                            RoundEnvironment roundEnv) {
  57         if (!roundEnv.processingOver()) {
  58             TypeElement    charElt     = eltUtils.getTypeElement("java.lang.Character");
  59             PackageElement javaLangPkg = eltUtils.getPackageElement("java.lang");
  60             ModuleElement  expectedMod = enclosingToModule(javaLangPkg);
  61 
  62             checkMod(charElt, expectedMod);
  63             checkMod(javaLangPkg, expectedMod);
  64 
  65             // The module of fields and methods and nested types of
  66             // java.lang.Character should match the module of
  67             // java.lang.
  68             for (Element e : charElt.getEnclosedElements()) {
  69                 checkMod(e, expectedMod);
  70             }
  71 
  72             // A module of a module is itself
  73             if (expectedMod != null)
  74                 checkMod(expectedMod, expectedMod);
  75         }
  76         return true;
  77     }
  78 
  79     private ModuleElement enclosingToModule(Element e) {
  80         Element enclosing = e.getEnclosingElement();
  81         if (enclosing == null)
  82             return null;
  83         else
  84             return ElementFilter.modulesIn(List.of(enclosing)).get(0);
  85     }
  86 
  87     private void checkMod(Element e, ModuleElement expectedMod) {
  88         ModuleElement actualMod = eltUtils.getModuleOf(e);
  89         if (!Objects.equals(actualMod, expectedMod)) {
  90             throw new RuntimeException(String.format("Unexpected module ``%s''' for %s %s, expected ``%s''%n",
  91                                                      actualMod, e.getKind(), e.toString(), expectedMod));
  92         }
  93     }
  94 }