1 /* 2 * Copyright (c) 2013, 2015, 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 * @summary Test ReferenceType.visibleMethods 27 * @bug 8028430 28 * @author Staffan Larsen 29 * 30 * @run build TestScaffold VMConnection TargetListener TargetAdapter 31 * @run compile -g VisibleMethods.java 32 * @run driver VisibleMethods 33 */ 34 import com.sun.jdi.Method; 35 import com.sun.jdi.ReferenceType; 36 import com.sun.jdi.StackFrame; 37 import com.sun.jdi.StringReference; 38 import com.sun.jdi.ThreadReference; 39 import com.sun.jdi.event.BreakpointEvent; 40 41 import java.util.Arrays; 42 import java.util.List; 43 import java.util.stream.Collectors; 44 45 /********** target program **********/ 46 47 interface Super { 48 public void m(Object o); // This method should not be visible in AC 49 public void m(String s); // This method should not be visible in AC 50 } 51 52 interface One extends Super { 53 public void m(Object o); 54 public void m1(); // Either this method or Two.m1 should be visible in AC 55 } 56 57 interface Two extends Super { 58 public void m(String s); 59 public void m1(); // Either this method or One.m1 should be visible in AC 60 } 61 62 abstract class AC implements One, Two { 63 } 64 65 class CC extends AC { 66 public void m(Object o) { 67 } 68 public void m(String s) { 69 } 70 public void m1() { 71 } 72 public static void main(String[] args) { 73 System.out.println("Goodbye from VisibleMethods!"); 74 } 75 } 76 77 /********** test program **********/ 78 79 public class VisibleMethods extends TestScaffold { 80 ReferenceType targetClass; 81 ThreadReference mainThread; 82 83 VisibleMethods(String args[]) { 84 super(args); 85 } 86 87 public static void main(String[] args) throws Exception { 88 new VisibleMethods(args).startTests(); 89 } 90 91 /********** test core **********/ 92 93 protected void runTests() 94 throws Exception 95 { 96 /* 97 * Run to String.<init> 98 */ 99 startToMain("CC"); 100 101 ReferenceType ac = findReferenceType("AC"); 102 List<String> visible = ac.visibleMethods(). 103 stream(). 104 map(Method::toString). 105 collect(Collectors.toList()); 106 107 System.out.println("visibleMethods(): " + visible); 108 109 verifyContains(visible, 1, "Two.m(java.lang.String)"); 110 verifyContains(visible, 1, "One.m(java.lang.Object)"); 111 verifyContains(visible, 0, "Super.m(java.lang.Object)"); 112 verifyContains(visible, 0, "Super.m(java.lang.String)"); 113 verifyContains(visible, 1, "Two.m1()", "One.m1()"); 114 115 /* 116 * resume the target listening for events 117 */ 118 listenUntilVMDisconnect(); 119 } 120 121 private void verifyContains(List<String> methods, int matches, 122 String... sigs) throws Exception { 123 if (countMatches(methods, sigs) != matches) { 124 throw new Exception("visibleMethods() should have contained " 125 + matches + " entry/entries from " + Arrays.toString(sigs)); 126 } 127 } 128 129 private int countMatches(List<String> list1, String[] list2) { 130 int count = 0; 131 for (String s1 : list1) { 132 for (String s2 : list2) { 133 if (s1.equals(s2)) { 134 count++; 135 } 136 } 137 } 138 return count; 139 } 140 }