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