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 8054717 27 * @summary Make sure extraction of non-private APIs work as expected. 28 * @library /tools/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.compiler/com.sun.tools.sjavac 32 * jdk.jdeps/com.sun.tools.javap 33 * @build Wrapper toolbox.ToolBox 34 * @run main Wrapper ApiExtraction 35 */ 36 import static java.util.Arrays.asList; 37 import static java.util.Collections.emptyList; 38 import static javax.lang.model.element.Modifier.FINAL; 39 import static javax.lang.model.element.Modifier.PROTECTED; 40 import static javax.lang.model.element.Modifier.PUBLIC; 41 import static javax.lang.model.element.Modifier.STATIC; 42 43 import java.io.IOException; 44 import java.util.HashSet; 45 import java.util.List; 46 import java.util.Set; 47 48 import javax.lang.model.type.TypeKind; 49 50 import com.sun.tools.sjavac.PubApiExtractor; 51 import com.sun.tools.sjavac.options.Options; 52 import com.sun.tools.sjavac.pubapi.PrimitiveTypeDesc; 53 import com.sun.tools.sjavac.pubapi.PubApi; 54 import com.sun.tools.sjavac.pubapi.PubMethod; 55 import com.sun.tools.sjavac.pubapi.PubType; 56 import com.sun.tools.sjavac.pubapi.PubVar; 57 import com.sun.tools.sjavac.pubapi.ReferenceTypeDesc; 58 59 60 public class ApiExtraction { 61 public static void main(String[] args) throws IOException { 62 63 String testSrc = String.join("\n", 64 "import java.util.*;", 65 "public final class TestClass extends Thread {", 66 67 // Fields with various combination of modifiers 68 " private String s1 = \"str 1\";", 69 " public String s2 = \"str 2\";", 70 " protected final String s3 = \"str 3\";", 71 " static String s4 = \"str 4\";", 72 73 // Methods with various combinations of types and modifiers 74 " protected void m1() {}", 75 " public static Map<Integer, List<String>> m2() {", 76 " return null;", 77 " }", 78 " final void m3(Set<Map<Integer, Map<String, String>>> s) {}", 79 80 // Some inner classes 81 " static class DummyInner1 implements Runnable {", 82 " protected int field;", 83 " public void run() {}", 84 " }", 85 " final class DummyInner2 { }", 86 "}"); 87 88 // Create class file to extract API from 89 new ToolBox().new JavacTask().sources(testSrc).run(); 90 91 // Extract PubApi 92 Options options = Options.parseArgs("-d", "bin", "--state-dir=bin", "-cp", "."); 93 PubApiExtractor pubApiExtr = new PubApiExtractor(options); 94 PubApi actualApi = pubApiExtr.getPubApi("TestClass"); 95 pubApiExtr.close(); 96 97 // Validate result 98 PubApi expectedApi = getExpectedPubApi(); 99 if (!expectedApi.equals(actualApi)) { 100 List<String> diffs = expectedApi.diff(actualApi); 101 System.out.println(diffs.size() + " differences found."); 102 for (String diff : diffs) { 103 System.out.println(diff); 104 } 105 throw new AssertionError("Actual API differs from expected API."); 106 } 107 } 108 109 private static PubApi getExpectedPubApi() { | 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 8054717 27 * @summary Make sure extraction of non-private APIs work as expected. 28 * @library /tools/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.compiler/com.sun.tools.sjavac 32 * jdk.compiler/com.sun.tools.sjavac.options 33 * jdk.compiler/com.sun.tools.sjavac.pubapi 34 * @build Wrapper toolbox.ToolBox toolbox.JavacTask 35 * @run main Wrapper ApiExtraction 36 */ 37 38 import static java.util.Arrays.asList; 39 import static java.util.Collections.emptyList; 40 import static javax.lang.model.element.Modifier.FINAL; 41 import static javax.lang.model.element.Modifier.PROTECTED; 42 import static javax.lang.model.element.Modifier.PUBLIC; 43 import static javax.lang.model.element.Modifier.STATIC; 44 45 import java.io.IOException; 46 import java.util.HashSet; 47 import java.util.List; 48 import java.util.Set; 49 50 import javax.lang.model.type.TypeKind; 51 52 import com.sun.tools.sjavac.PubApiExtractor; 53 import com.sun.tools.sjavac.options.Options; 54 import com.sun.tools.sjavac.pubapi.PrimitiveTypeDesc; 55 import com.sun.tools.sjavac.pubapi.PubApi; 56 import com.sun.tools.sjavac.pubapi.PubMethod; 57 import com.sun.tools.sjavac.pubapi.PubType; 58 import com.sun.tools.sjavac.pubapi.PubVar; 59 import com.sun.tools.sjavac.pubapi.ReferenceTypeDesc; 60 61 import toolbox.JavacTask; 62 import toolbox.ToolBox; 63 64 public class ApiExtraction { 65 public static void main(String[] args) throws IOException { 66 67 String testSrc = String.join("\n", 68 "import java.util.*;", 69 "public final class TestClass extends Thread {", 70 71 // Fields with various combination of modifiers 72 " private String s1 = \"str 1\";", 73 " public String s2 = \"str 2\";", 74 " protected final String s3 = \"str 3\";", 75 " static String s4 = \"str 4\";", 76 77 // Methods with various combinations of types and modifiers 78 " protected void m1() {}", 79 " public static Map<Integer, List<String>> m2() {", 80 " return null;", 81 " }", 82 " final void m3(Set<Map<Integer, Map<String, String>>> s) {}", 83 84 // Some inner classes 85 " static class DummyInner1 implements Runnable {", 86 " protected int field;", 87 " public void run() {}", 88 " }", 89 " final class DummyInner2 { }", 90 "}"); 91 92 // Create class file to extract API from 93 new JavacTask(new ToolBox()).sources(testSrc).run(); 94 95 // Extract PubApi 96 Options options = Options.parseArgs("-d", "bin", "--state-dir=bin", "-cp", "."); 97 PubApiExtractor pubApiExtr = new PubApiExtractor(options); 98 PubApi actualApi = pubApiExtr.getPubApi("TestClass"); 99 pubApiExtr.close(); 100 101 // Validate result 102 PubApi expectedApi = getExpectedPubApi(); 103 if (!expectedApi.equals(actualApi)) { 104 List<String> diffs = expectedApi.diff(actualApi); 105 System.out.println(diffs.size() + " differences found."); 106 for (String diff : diffs) { 107 System.out.println(diff); 108 } 109 throw new AssertionError("Actual API differs from expected API."); 110 } 111 } 112 113 private static PubApi getExpectedPubApi() { |