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 8175335
  27  * @summary Test Elements.getPackageOf
  28  * @author  Joseph D. Darcy
  29  * @library /tools/javac/lib
  30  * @modules java.compiler
  31  *          jdk.compiler
  32  * @build   JavacTestingAbstractProcessor TestPseudoTypeHandling
  33  * @compile -processor TestPseudoTypeHandling -proc:only TestPseudoTypeHandling.java
  34  */
  35 
  36 import java.util.*;
  37 import java.util.function.*;
  38 import static java.util.Objects.*;
  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.type.*;
  44 import javax.lang.model.util.*;
  45 import static javax.lang.model.util.ElementFilter.*;
  46 import static javax.tools.Diagnostic.Kind.*;
  47 import static javax.tools.StandardLocation.*;
  48 
  49 /**
  50  * Test basic handling of module type.
  51  */
  52 public class TestPseudoTypeHandling extends JavacTestingAbstractProcessor {
  53    /**
  54      */
  55     public boolean process(Set<? extends TypeElement> annotations,
  56                            RoundEnvironment roundEnv) {
  57         if (!roundEnv.processingOver()) {
  58             TypeMirror objectType  = requireNonNull(eltUtils.getTypeElement("java.lang.Object")).asType();
  59 
  60             List<TypeMirror> typeMirrorsToTest =
  61                 List.of(requireNonNull(eltUtils.getModuleElement("java.base")).asType(),
  62                         requireNonNull(eltUtils.getPackageElement("java.lang")).asType());
  63 
  64             for (TypeMirror type : typeMirrorsToTest) {
  65                 expectException(t -> typeUtils.isSubtype(t, objectType), type);
  66                 expectException(t -> typeUtils.isSubtype(objectType, t), type);
  67 
  68                 expectException(t -> typeUtils.isAssignable(t, objectType), type);
  69                 expectException(t -> typeUtils.isAssignable(objectType, t), type);
  70 
  71                 expectException(t -> typeUtils.contains(t, objectType), type);
  72                 expectException(t -> typeUtils.contains(objectType, t), type);
  73 
  74                 expectException(t -> typeUtils.capture(t), type);
  75                 expectException(t -> typeUtils.erasure(t), type);
  76 
  77                 expectException(t -> typeUtils.getArrayType(t), type);
  78 
  79                 expectException(t -> typeUtils.directSupertypes(t), type);
  80             }
  81         }
  82         return true;
  83     }
  84 
  85     void expectException(Consumer<TypeMirror> argument, TypeMirror type) {
  86         try {
  87             argument.accept(type);
  88             throw new RuntimeException("Should not reach " + type.toString());
  89         } catch (IllegalArgumentException e) {
  90             ; // Expected
  91         }
  92     }
  93 }