Path: blob/master/test/langtools/tools/sjavac/ApiExtraction.java
41144 views
/*1* Copyright (c) 2015, 2016, 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 805471726* @summary Make sure extraction of non-private APIs work as expected.27* @library /tools/lib28* @modules jdk.compiler/com.sun.tools.javac.api29* jdk.compiler/com.sun.tools.javac.main30* jdk.compiler/com.sun.tools.sjavac31* jdk.compiler/com.sun.tools.sjavac.options32* jdk.compiler/com.sun.tools.sjavac.pubapi33* @build Wrapper toolbox.ToolBox toolbox.JavacTask34* @run main Wrapper ApiExtraction35*/3637import static java.util.Arrays.asList;38import static java.util.Collections.emptyList;39import static javax.lang.model.element.Modifier.FINAL;40import static javax.lang.model.element.Modifier.PROTECTED;41import static javax.lang.model.element.Modifier.PUBLIC;42import static javax.lang.model.element.Modifier.STATIC;4344import java.io.IOException;45import java.util.HashSet;46import java.util.List;47import java.util.Set;4849import javax.lang.model.type.TypeKind;5051import com.sun.tools.sjavac.PubApiExtractor;52import com.sun.tools.sjavac.options.Options;53import com.sun.tools.sjavac.pubapi.PrimitiveTypeDesc;54import com.sun.tools.sjavac.pubapi.PubApi;55import com.sun.tools.sjavac.pubapi.PubMethod;56import com.sun.tools.sjavac.pubapi.PubType;57import com.sun.tools.sjavac.pubapi.PubVar;58import com.sun.tools.sjavac.pubapi.ReferenceTypeDesc;5960import toolbox.JavacTask;61import toolbox.ToolBox;6263public class ApiExtraction {64public static void main(String[] args) throws IOException {6566String testSrc = String.join("\n",67"import java.util.*;",68"public final class TestClass extends Thread {",6970// Fields with various combination of modifiers71" private String s1 = \"str 1\";",72" public String s2 = \"str 2\";",73" protected final String s3 = \"str 3\";",74" static String s4 = \"str 4\";",7576// Methods with various combinations of types and modifiers77" protected void m1() {}",78" public static Map<Integer, List<String>> m2() {",79" return null;",80" }",81" final void m3(Set<Map<Integer, Map<String, String>>> s) {}",8283// Some inner classes84" static class DummyInner1 implements Runnable {",85" protected int field;",86" public void run() {}",87" }",88" final class DummyInner2 { }",89"}");9091// Create class file to extract API from92new JavacTask(new ToolBox()).sources(testSrc).run();9394// Extract PubApi95Options options = Options.parseArgs("-d", "bin", "--state-dir=bin", "-cp", ".");96PubApiExtractor pubApiExtr = new PubApiExtractor(options);97PubApi actualApi = pubApiExtr.getPubApi("TestClass");98pubApiExtr.close();99100// Validate result101PubApi expectedApi = getExpectedPubApi();102if (!expectedApi.equals(actualApi)) {103List<String> diffs = expectedApi.diff(actualApi);104System.out.println(diffs.size() + " differences found.");105for (String diff : diffs) {106System.out.println(diff);107}108throw new AssertionError("Actual API differs from expected API.");109}110}111112private static PubApi getExpectedPubApi() {113114ReferenceTypeDesc string = new ReferenceTypeDesc("java.lang.String");115116// Fields117// (s1 is private and therefore not included)118PubVar s2 = new PubVar(setOf(PUBLIC), string, "s2", null);119PubVar s4 = new PubVar(setOf(STATIC), string, "s4", null);120PubVar s3 = new PubVar(setOf(PROTECTED, FINAL), string, "s3",121"\"\\u0073\\u0074\\u0072\\u0020\\u0033\"");122123// Methods124PubMethod init = new PubMethod(setOf(PUBLIC),125emptyList(),126new PrimitiveTypeDesc(TypeKind.VOID),127"<init>",128emptyList(),129emptyList());130131PubMethod clinit = new PubMethod(setOf(STATIC),132emptyList(),133new PrimitiveTypeDesc(TypeKind.VOID),134"<clinit>",135emptyList(),136emptyList());137138PubMethod m1 = new PubMethod(setOf(PROTECTED),139emptyList(),140new PrimitiveTypeDesc(TypeKind.VOID),141"m1",142emptyList(),143emptyList());144145PubMethod m2 = new PubMethod(setOf(PUBLIC, STATIC),146emptyList(),147new ReferenceTypeDesc("java.util.Map"),148"m2",149emptyList(),150emptyList());151152PubMethod m3 = new PubMethod(setOf(FINAL),153emptyList(),154new PrimitiveTypeDesc(TypeKind.VOID),155"m3",156asList(new ReferenceTypeDesc("java.util.Set")),157emptyList());158159// Complete class160PubType testClass = new PubType(setOf(PUBLIC, FINAL),161"TestClass",162new PubApi(asList(getDummyInner1(), getDummyInner2()),163asList(s2, s3, s4),164asList(init, clinit, m1, m2, m3)));165166// Wrap in "package level" PubApi167return new PubApi(asList(testClass), emptyList(), emptyList());168}169170private static PubType getDummyInner1() {171PubMethod init = new PubMethod(setOf(),172emptyList(),173new PrimitiveTypeDesc(TypeKind.VOID),174"<init>",175emptyList(),176emptyList());177178PubMethod run = new PubMethod(setOf(PUBLIC),179emptyList(),180new PrimitiveTypeDesc(TypeKind.VOID),181"run",182emptyList(),183emptyList());184185PubVar field = new PubVar(setOf(PROTECTED),186new PrimitiveTypeDesc(TypeKind.INT),187"field",188null);189190return new PubType(setOf(STATIC),191"TestClass$DummyInner1",192new PubApi(emptyList(),193asList(field),194asList(init, run)));195}196197private static PubType getDummyInner2() {198PubMethod init = new PubMethod(setOf(),199emptyList(),200new PrimitiveTypeDesc(TypeKind.VOID),201"<init>",202emptyList(),203emptyList());204205return new PubType(setOf(FINAL),206"TestClass$DummyInner2",207new PubApi(emptyList(),208emptyList(),209asList(init)));210}211212@SafeVarargs213private static <T> Set<T> setOf(T... elements) {214return new HashSet<>(asList(elements));215}216}217218219