Path: blob/master/test/langtools/tools/sjavac/PubApisTest.java
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8241312 824677426* @summary test for com.sun.tools.sjavac.comp.PubAPIs and com.sun.tools.sjavac.comp.PubapiVisitor27* @library /tools/lib28* @modules jdk.compiler/com.sun.tools.javac.util29* jdk.compiler/com.sun.tools.sjavac.comp30* jdk.compiler/com.sun.tools.sjavac.pubapi31*/32import com.sun.source.util.JavacTask;33import com.sun.tools.javac.util.Context;34import java.io.IOException;35import com.sun.tools.sjavac.comp.PubAPIs;36import com.sun.tools.sjavac.pubapi.PubApi;37import java.net.URI;38import static java.util.Arrays.asList;39import static java.util.Collections.emptySet;40import java.util.List;41import javax.tools.JavaFileObject;42import javax.tools.SimpleJavaFileObject;43import javax.tools.ToolProvider;4445public class PubApisTest {4647public static void main(String[] args) throws Throwable {48javax.tools.JavaCompiler c = ToolProvider.getSystemJavaCompiler();49JavacTask t = (JavacTask) c.getTask(null, null, null, null, null,50List.of(new SimpleJavaFileObject(URI.create("TestClass.java"), JavaFileObject.Kind.SOURCE) {51@Override52public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {53return String.join("\n",54"import java.util.*;",55"public final class TestClass {",56" private String s1 = \"str 1\";",57" public String s2 = \"str 2\";",58" protected final String s3 = \"str 3\";",59" static String s4 = \"str 4\";",60" protected TestClass(int i) {}",61" protected void m1() {}",62" public static Map<Integer, List<String>> m2() {",63" return null;",64" }",65" final void m3(Set<Map<Integer, Map<String, String>>> s) {}",66" static class DummyInner1 implements Runnable {",67" protected int field;",68" public void run() {}",69" }",70" final class DummyInner2 { }",71" public record Record3(int f1, String f2) {}",72"}");73}74}));75PubAPIs apis = PubAPIs.instance(new Context());76t.analyze().forEach(apis::visitPubapi);77PubApi actualApi = (PubApi) apis.getPubapis(emptySet(), false).get(":");78PubApi expectedApi = new PubApi();79asList( "TYPE final public TestClass",80" VAR public java.lang.String s2",81" VAR final protected java.lang.String s3 = \"\\u0073\\u0074\\u0072\\u0020\\u0033\"",82" VAR static java.lang.String s4",83" METHOD protected void m1()",84" METHOD public static java.util.Map m2()",85" METHOD final void m3(java.util.Set)",86" METHOD protected void <init>(int)",87" TYPE static TestClass$DummyInner1",88" VAR protected int field",89" METHOD public void run()",90" METHOD void <init>()",91" TYPE final TestClass$DummyInner2",92" METHOD void <init>()",93" TYPE final public static TestClass$Record3",94" METHOD final public boolean equals(java.lang.Object)",95" METHOD final public int hashCode()",96" METHOD public int f1()",97" METHOD public java.lang.String f2()",98" METHOD final public java.lang.String toString()",99" METHOD public void <init>(int,java.lang.String)"100).forEach(expectedApi::appendItem);101if (!expectedApi.equals(actualApi)) {102List<String> diffs = expectedApi.diff(actualApi);103System.out.println(diffs.size() + " differences found.");104diffs.forEach(System.out::println);105throw new AssertionError("Actual API differs from expected API.");106}107}108}109110111