1 /*
  2  * Copyright (c) 2021, 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 /* This test "covers"/verifies com.sun.tools.javac.model.JavacTypes#asMemberOf's calls
 25    to asSuper work*properly with primitive types.
 26 */
 27 
 28 /**
 29  * @test
 30  * @bug 8244712
 31  * @summary Test API usage with reference projection types.
 32  * @ignore
 33  * @library ./lib
 34  * @modules jdk.compiler/com.sun.tools.javac.api
 35  *          jdk.compiler/com.sun.tools.javac.code
 36  * @build ToolTester
 37  */
 38 
 39 import java.io.*;
 40 import javax.lang.model.element.Element;
 41 import javax.lang.model.element.TypeElement;
 42 import javax.lang.model.type.DeclaredType;
 43 import javax.lang.model.type.TypeMirror;
 44 import javax.lang.model.util.ElementFilter;
 45 import javax.lang.model.util.Elements;
 46 import javax.lang.model.util.Types;
 47 import javax.tools.*;
 48 
 49 import com.sun.tools.javac.api.JavacTaskImpl;
 50 
 51 public class TestApisWithProjections extends ToolTester {
 52     public static void main(String... args) throws Exception {
 53         try (TestApisWithProjections t = new TestApisWithProjections()) {
 54             t.run();
 55         }
 56     }
 57 
 58     void run() throws Exception {
 59         StringWriter sw = new StringWriter();
 60         PrintWriter pw = new PrintWriter(sw);
 61         File file = new File(test_src, "TestApisWithProjections.java");
 62         final Iterable<? extends JavaFileObject> compilationUnits =
 63             fm.getJavaFileObjects(new File[] {file});
 64         task = (JavacTaskImpl)tool.getTask(pw, fm, null, null, null, compilationUnits);
 65         elements = task.getElements();
 66         types = task.getTypes();
 67 
 68         Iterable<? extends TypeElement> toplevels;
 69         toplevels = ElementFilter.typesIn(task.enter(task.parse()));
 70 
 71         for (TypeElement clazz : toplevels) {
 72             System.out.format("Testing %s:%n%n", clazz.getSimpleName());
 73             testParseType(clazz);
 74         }
 75 
 76         pw.close();
 77 
 78         String out = sw.toString();
 79         System.out.println(out);
 80 
 81         if (out.contains("com.sun.tools.javac.util"))
 82             throw new Exception("Unexpected output from compiler");
 83     }
 84 
 85     void testParseType(TypeElement clazz) {
 86         DeclaredType type = (DeclaredType)task.parseType("PrimitiveClass<String>", clazz);
 87         for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
 88             TypeMirror mt = types.asMemberOf(type, member);
 89             System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
 90         }
 91     }
 92 
 93     JavacTaskImpl task;
 94     Elements elements;
 95     Types types;
 96 }
 97 
 98 abstract class Base<T> {
 99     void foo(T t) {}
100 }
101 
102 primitive class PrimitiveClass<T> extends Base<T> {
103 }