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