Path: blob/master/test/jdk/com/sun/jdi/AccessSpecifierTest.java
41149 views
/*1* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 435962826* @summary Test fix for JDI: methods Accessible.is...() lie about array types27* @author Tim Bell28*29* @run build TestScaffold VMConnection TargetListener TargetAdapter30* @run compile -g AccessSpecifierTest.java31* @run driver AccessSpecifierTest32*/33import com.sun.jdi.*;34import com.sun.jdi.event.*;35import com.sun.jdi.request.*;3637import java.util.*;3839/********** target program **********/404142/** Sample package-private interface. */43interface AccessSpecifierPackagePrivateInterface {}4445/** Sample package-private class. */46class AccessSpecifierPackagePrivateClass {}4748/** Sample package-private class. */49class AccessSpecifierPackagePrivateClassTwo implements50AccessSpecifierPackagePrivateInterface {}5152class AccessSpecifierTarg {53private boolean z0;54boolean z1[]={z0}, z2[][]={z1};5556public byte b0;57byte b1[]={b0}, b2[][]={b1};5859protected short s0;60short s1[]={s0}, s2[][]={s1};6162int i0;63int i1[]={i0}, i2[][]={i1};6465private long l0;66long l1[]={l0}, l2[][]={l1};6768public char c0;69char c1[]={c0}, c2[][]={c1};7071protected float f0;72float f1[]={f0}, f2[][]={f1};7374double d0;75double d1[]={d0}, d2[][]={d1};7677Boolean Z0 = Boolean.TRUE;78Boolean Z1[]={Z0}, Z2[][]={Z1};79Byte B0 = new Byte ((byte)0x1f);80Byte B1[]={B0}, B2[][]={B1};81Character C0 = new Character ('a');82Character C1[]={C0}, C2[][]={C1};83Double D0 = new Double (1.0d);84Double D1[]={D0}, D2[][]={D1};85Float F0 = new Float (2.0f);86Float F1[]={F0}, F2[][]={F1};87Integer I0 = new Integer (8675309);88Integer I1[]={I0}, I2[][]={I1};89Long L0 = new Long (973230999L);90Long L1[]={L0}, L2[][]={L1};91String S0 = "A String";92String S1[]={S0}, S2[][]={S1};93Object O0 = new Object();94Object O1[]={O0}, O2[][]={O1};9596private static class U {}97protected static class V {}98public static class W {}99static class P {} // package private100101U u0=new U(), u1[]={u0}, u2[][]={u1};102V v0=new V(), v1[]={v0}, v2[][]={v1};103W w0=new W(), w1[]={w0}, w2[][]={w1};104P p0=new P(), p1[]={p0}, p2[][]={p1};105106private static interface StaticInterface {}107private static class ClassUsingStaticInterface108implements StaticInterface {}109110StaticInterface staticInterface_0 = new ClassUsingStaticInterface();111StaticInterface staticInterface_1[]={staticInterface_0};112StaticInterface staticInterface_2[][]={staticInterface_1};113114AccessSpecifierTarg a0, a1[]={a0}, a2[][]={a1};115116AccessSpecifierPackagePrivateClass ppc0=new AccessSpecifierPackagePrivateClass();117AccessSpecifierPackagePrivateClass ppc1[]={ppc0};118AccessSpecifierPackagePrivateClass ppc2[][]={ppc1};119120AccessSpecifierPackagePrivateInterface ppi0 =121new AccessSpecifierPackagePrivateClassTwo ();122AccessSpecifierPackagePrivateInterface ppi1[]={ppi0};123AccessSpecifierPackagePrivateInterface ppi2[][]={ppi1};124125public AccessSpecifierTarg() {126super();127}128129public void ready(){130System.out.println("Ready!");131}132133public static void main(String[] args){134System.out.println("Howdy!");135AccessSpecifierTarg my = new AccessSpecifierTarg();136my.ready();137System.out.println("Goodbye from AccessSpecifierTarg!");138}139}140141/********** test program **********/142143public class AccessSpecifierTest extends TestScaffold {144145private final static String debugeeName = "AccessSpecifierTarg";146147/** Known Accessible Information about the Debugee. */148private static final int NAME = 0;149private static final int ACCESS = 1;150private final static String primitives[][] = {151{"z", "private", "public", "public"},152{"b", "public", "public", "public"},153{"s", "protected", "public", "public"},154{"i", "package private", "public", "public"},155{"l", "private", "public", "public"},156{"c", "public", "public", "public"},157{"f", "protected", "public", "public"},158{"d", "package private", "public", "public"},159};160private final static String references[][] = {161{"java.lang.Boolean" , "public"},162{"java.lang.Character", "public"},163{"java.lang.Class" , "public"},164{"java.lang.Double" , "public"},165{"java.lang.Float" , "public"},166{"java.lang.Integer" , "public"},167{"java.lang.Long" , "public"},168{"java.lang.String" , "public"},169{"java.lang.Object" , "public"},170171{"AccessSpecifierTarg", "package private"},172{"AccessSpecifierPackagePrivateClass", "package private"},173{"AccessSpecifierPackagePrivateInterface", "package private"},174175{"AccessSpecifierTarg$StaticInterface", "private"},176177{"AccessSpecifierTarg$U", "private"},178{"AccessSpecifierTarg$V", "protected"},179{"AccessSpecifierTarg$W", "public"},180{"AccessSpecifierTarg$P", "package private"}181};182183AccessSpecifierTest (String args[]) {184super(args);185}186187public static void main(String[] args) throws Exception {188new AccessSpecifierTest (args).startTests();189}190191/********** test core **********/192193private void testAccessible (String name, Accessible a,194boolean isPublic, boolean isProtected,195boolean isPrivate, boolean isPackagePrivate) {196System.out.println (" Testing: " + name + " modifiers = " +197Integer.toBinaryString(a.modifiers()));198if (a.isPublic() != isPublic) {199failure("**Name = " + name + " expecting: " + isPublic +200" isPublic() was: " + a.isPublic());201}202if (a.isPrivate() != isPrivate) {203failure("**Name = " + name + " expecting: " + isPrivate +204" isPrivate() was: " + a.isPrivate());205}206if (a.isProtected() != isProtected) {207failure("**Name = " + name + " expecting: " + isProtected +208" isProtected() is: " + a.isProtected());209}210if (a.isPackagePrivate() != isPackagePrivate) {211failure("**Name = " + name + " expecting: " + isPackagePrivate +212" isPackagePrivate() is: " + a.isPackagePrivate());213}214}215216protected void runTests() throws Exception {217/*218* Get to the top of ready()219*/220startTo(debugeeName, "ready", "()V");221222ReferenceType rt = findReferenceType(debugeeName);223if (rt == null) {224throw new Exception ("ReferenceType not found for: " + debugeeName);225}226for (int i = 0; i < primitives.length; i++) {227for (int j = 0; j < 3; j++) {228String suffix = Integer.toString(j);229String fieldName = primitives[i][NAME] + suffix;230Field field = rt.fieldByName(fieldName);231if (field == null) {232throw new Exception ("Field not found for: " + fieldName);233}234235Type t = field.type();236if (t instanceof ReferenceType) {237ReferenceType reft = (ReferenceType)t;238if (primitives[i][ACCESS + j].equals("public")) {239testAccessible(reft.name(), reft,240true, false, false, false);241} else if (primitives[i][ACCESS + j].equals("protected")) {242testAccessible(reft.name(), reft,243false, true, false, false);244} else if (primitives[i][ACCESS + j].equals("private")) {245testAccessible(reft.name(), reft,246false, false, true, false);247} else if (primitives[i][ACCESS + j].equals("package private")) {248testAccessible(reft.name(), reft,249false, false, false, true);250}251} else {252System.out.println (" Skipping " + t +253" (primitive scalar type)");254}255}256}257258String brackets[] = {"[][]", "[]", ""};259260for (int i = 0; i < references.length; i++) {261for (int j = 0; j < 3; j++) {262String suffix = brackets[j];263String referenceName = references[i][NAME] + suffix;264ReferenceType refType = findReferenceType(referenceName);265if (refType == null) {266System.out.println ("Skipping " + referenceName +267" (not found)");268} else {269if (references[i][ACCESS].equals("public")) {270testAccessible(refType.name(), refType, true, false, false, false);271} else if (references[i][ACCESS].equals("protected")) {272testAccessible(refType.name(), refType, false, true, false, false);273} else if (references[i][ACCESS].equals("private")) {274testAccessible(refType.name(), refType, false, false, true, false);275} else if (references[i][ACCESS].equals("package private")) {276testAccessible(refType.name(), refType, false, false, false, true);277}278}279}280}281282/*283* resume the target listening for events284*/285listenUntilVMDisconnect();286287/*288* deal with results of test289* if anything has called failure("foo") testFailed will be true290*/291if (!testFailed) {292println("AccessSpecifierTest: passed");293} else {294throw new Exception("AccessSpecifierTest: failed");295}296}297}298299300