1 /*
   2  * Copyright (c) 2001, 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  * @bug 4359628
  27  * @summary Test fix for JDI: methods Accessible.is...() lie about array types
  28  * @author Tim Bell
  29  *
  30  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  31  * @run compile -g AccessSpecifierTest.java
  32  * @run driver AccessSpecifierTest
  33  */
  34 import com.sun.jdi.*;
  35 import com.sun.jdi.event.*;
  36 import com.sun.jdi.request.*;
  37 
  38 import java.util.*;
  39 
  40     /********** target program **********/
  41 
  42 
  43 /** Sample package-private interface. */
  44 interface AccessSpecifierPackagePrivateInterface {}
  45 
  46 /** Sample package-private class. */
  47 class AccessSpecifierPackagePrivateClass {}
  48 
  49 /** Sample package-private class. */
  50 class AccessSpecifierPackagePrivateClassTwo implements
  51     AccessSpecifierPackagePrivateInterface {}
  52 
  53 class AccessSpecifierTarg {
  54     private boolean z0;
  55     boolean z1[]={z0}, z2[][]={z1};
  56 
  57     public byte b0;
  58     byte b1[]={b0}, b2[][]={b1};
  59 
  60     protected short s0;
  61     short s1[]={s0}, s2[][]={s1};
  62 
  63     int i0;
  64     int i1[]={i0}, i2[][]={i1};
  65 
  66     private long l0;
  67     long l1[]={l0}, l2[][]={l1};
  68 
  69     public char c0;
  70     char c1[]={c0}, c2[][]={c1};
  71 
  72     protected float f0;
  73     float f1[]={f0}, f2[][]={f1};
  74 
  75     double d0;
  76     double d1[]={d0}, d2[][]={d1};
  77 
  78     Boolean   Z0 = Boolean.TRUE;
  79     Boolean   Z1[]={Z0}, Z2[][]={Z1};
  80     Byte      B0 = new Byte ((byte)0x1f);
  81     Byte      B1[]={B0}, B2[][]={B1};
  82     Character C0 = new Character ('a');
  83     Character C1[]={C0}, C2[][]={C1};
  84     Double    D0 = new Double (1.0d);
  85     Double    D1[]={D0}, D2[][]={D1};
  86     Float     F0 = new Float (2.0f);
  87     Float     F1[]={F0}, F2[][]={F1};
  88     Integer   I0 = new Integer (8675309);
  89     Integer   I1[]={I0}, I2[][]={I1};
  90     Long      L0 = new Long (973230999L);
  91     Long      L1[]={L0}, L2[][]={L1};
  92     String    S0 = "A String";
  93     String    S1[]={S0}, S2[][]={S1};
  94     Object    O0 = new Object();
  95     Object    O1[]={O0}, O2[][]={O1};
  96 
  97     private   static class  U {}
  98     protected static class  V {}
  99     public    static class  W {}
 100     static class  P {} // package private
 101 
 102     U u0=new U(), u1[]={u0}, u2[][]={u1};
 103     V v0=new V(), v1[]={v0}, v2[][]={v1};
 104     W w0=new W(), w1[]={w0}, w2[][]={w1};
 105     P p0=new P(), p1[]={p0}, p2[][]={p1};
 106 
 107     private static interface StaticInterface {}
 108     private static class ClassUsingStaticInterface
 109         implements StaticInterface {}
 110 
 111     StaticInterface staticInterface_0 = new ClassUsingStaticInterface();
 112     StaticInterface staticInterface_1[]={staticInterface_0};
 113     StaticInterface staticInterface_2[][]={staticInterface_1};
 114 
 115     AccessSpecifierTarg a0, a1[]={a0}, a2[][]={a1};
 116 
 117     AccessSpecifierPackagePrivateClass ppc0=new AccessSpecifierPackagePrivateClass();
 118     AccessSpecifierPackagePrivateClass ppc1[]={ppc0};
 119     AccessSpecifierPackagePrivateClass ppc2[][]={ppc1};
 120 
 121     AccessSpecifierPackagePrivateInterface ppi0 =
 122         new AccessSpecifierPackagePrivateClassTwo ();
 123     AccessSpecifierPackagePrivateInterface ppi1[]={ppi0};
 124     AccessSpecifierPackagePrivateInterface ppi2[][]={ppi1};
 125 
 126     public AccessSpecifierTarg() {
 127         super();
 128     }
 129 
 130     public void ready(){
 131         System.out.println("Ready!");
 132     }
 133 
 134     public static void main(String[] args){
 135         System.out.println("Howdy!");
 136         AccessSpecifierTarg my = new AccessSpecifierTarg();
 137         my.ready();
 138         System.out.println("Goodbye from AccessSpecifierTarg!");
 139     }
 140 }
 141 
 142     /********** test program **********/
 143 
 144 public class AccessSpecifierTest extends TestScaffold {
 145 
 146     private final static String debugeeName = "AccessSpecifierTarg";
 147 
 148     /** Known Accessible Information about the Debugee. */
 149     private static final int NAME = 0;
 150     private static final int ACCESS = 1;
 151     private final static String primitives[][] = {
 152         {"z", "private", "public", "public"},
 153         {"b", "public", "public", "public"},
 154         {"s", "protected", "public", "public"},
 155         {"i", "package private", "public", "public"},
 156         {"l", "private", "public", "public"},
 157         {"c", "public", "public", "public"},
 158         {"f", "protected", "public", "public"},
 159         {"d", "package private", "public", "public"},
 160     };
 161     private final static String references[][] = {
 162         {"java.lang.Boolean"  , "public"},
 163         {"java.lang.Character", "public"},
 164         {"java.lang.Class"    , "public"},
 165         {"java.lang.Double"   , "public"},
 166         {"java.lang.Float"    , "public"},
 167         {"java.lang.Integer"  , "public"},
 168         {"java.lang.Long"     , "public"},
 169         {"java.lang.String"   , "public"},
 170         {"java.lang.Object"   , "public"},
 171 
 172         {"AccessSpecifierTarg", "package private"},
 173         {"AccessSpecifierPackagePrivateClass", "package private"},
 174         {"AccessSpecifierPackagePrivateInterface", "package private"},
 175 
 176         {"AccessSpecifierTarg$StaticInterface", "private"},
 177 
 178         {"AccessSpecifierTarg$U", "private"},
 179         {"AccessSpecifierTarg$V", "protected"},
 180         {"AccessSpecifierTarg$W", "public"},
 181         {"AccessSpecifierTarg$P", "package private"}
 182     };
 183 
 184     AccessSpecifierTest (String args[]) {
 185         super(args);
 186     }
 187 
 188     public static void main(String[] args)      throws Exception {
 189         new AccessSpecifierTest (args).startTests();
 190     }
 191 
 192     /********** test core **********/
 193 
 194     private void testAccessible (String name, Accessible a,
 195                                  boolean isPublic, boolean isProtected,
 196                                  boolean isPrivate, boolean isPackagePrivate) {
 197         System.out.println ("  Testing: " + name + " modifiers = " +
 198                             Integer.toBinaryString(a.modifiers()));
 199         if (a.isPublic() != isPublic) {
 200             failure("**Name = " + name + " expecting: " + isPublic +
 201                     " isPublic() was: " + a.isPublic());
 202         }
 203         if (a.isPrivate() != isPrivate) {
 204             failure("**Name = " + name + " expecting: " + isPrivate +
 205                     " isPrivate() was: " + a.isPrivate());
 206         }
 207         if (a.isProtected() != isProtected) {
 208             failure("**Name = " + name + " expecting: " + isProtected +
 209                     " isProtected() is: " + a.isProtected());
 210         }
 211         if (a.isPackagePrivate() != isPackagePrivate) {
 212             failure("**Name = " + name + " expecting: " + isPackagePrivate +
 213                     " isPackagePrivate() is: " + a.isPackagePrivate());
 214         }
 215     }
 216 
 217     protected void runTests() throws Exception {
 218         /*
 219          * Get to the top of ready()
 220          */
 221         startTo(debugeeName, "ready", "()V");
 222 
 223         ReferenceType rt = findReferenceType(debugeeName);
 224         if (rt == null) {
 225             throw new Exception ("ReferenceType not found for: " + debugeeName);
 226         }
 227         for (int i = 0; i < primitives.length; i++) {
 228             for (int j = 0; j < 3; j++) {
 229                 String suffix = Integer.toString(j);
 230                 String fieldName = primitives[i][NAME] + suffix;
 231                 Field field = rt.fieldByName(fieldName);
 232                 if (field == null) {
 233                     throw new Exception ("Field not found for: " + fieldName);
 234                 }
 235 
 236                 Type t = field.type();
 237                 if (t instanceof ReferenceType) {
 238                     ReferenceType reft = (ReferenceType)t;
 239                     if (primitives[i][ACCESS + j].equals("public")) {
 240                         testAccessible(reft.name(), reft,
 241                                        true, false, false, false);
 242                     } else if (primitives[i][ACCESS + j].equals("protected")) {
 243                         testAccessible(reft.name(), reft,
 244                                        false, true, false, false);
 245                     } else if (primitives[i][ACCESS + j].equals("private")) {
 246                         testAccessible(reft.name(), reft,
 247                                        false, false, true, false);
 248                     } else if (primitives[i][ACCESS + j].equals("package private")) {
 249                         testAccessible(reft.name(), reft,
 250                                        false, false, false, true);
 251                     }
 252                 } else {
 253                     System.out.println ("  Skipping " + t +
 254                                         " (primitive scalar type)");
 255                 }
 256             }
 257         }
 258 
 259         String brackets[] = {"[][]", "[]", ""};
 260 
 261         for (int i = 0; i < references.length; i++) {
 262             for (int j = 0; j < 3; j++) {
 263                 String suffix = brackets[j];
 264                 String referenceName = references[i][NAME] + suffix;
 265                 ReferenceType refType = findReferenceType(referenceName);
 266                 if (refType == null) {
 267                     System.out.println ("Skipping " + referenceName +
 268                                         " (not found)");
 269                 } else {
 270                     if (references[i][ACCESS].equals("public")) {
 271                         testAccessible(refType.name(), refType, true, false, false, false);
 272                     } else if  (references[i][ACCESS].equals("protected")) {
 273                         testAccessible(refType.name(), refType, false, true, false, false);
 274                     } else if  (references[i][ACCESS].equals("private")) {
 275                         testAccessible(refType.name(), refType, false, false, true, false);
 276                     } else if  (references[i][ACCESS].equals("package private")) {
 277                         testAccessible(refType.name(), refType, false, false, false, true);
 278                     }
 279                 }
 280             }
 281         }
 282 
 283         /*
 284          * resume the target listening for events
 285          */
 286         listenUntilVMDisconnect();
 287 
 288         /*
 289          * deal with results of test
 290          * if anything has called failure("foo") testFailed will be true
 291          */
 292         if (!testFailed) {
 293             println("AccessSpecifierTest: passed");
 294         } else {
 295             throw new Exception("AccessSpecifierTest: failed");
 296         }
 297     }
 298 }