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