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