0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/A/**
0N/A * @test
0N/A
0N/A * @bug 6490436
0N/A * @summary Verify that protected constructor calls are not allowed for classfile version >= 50 (but that they are allowed for lesser versions).
0N/A * @author Keith McGuigan
0N/A */
0N/A
0N/Apublic class VerifyProtectedConstructor extends ClassLoader {
0N/A public static void main(String argv[]) throws Exception {
0N/A VerifyProtectedConstructor t = new VerifyProtectedConstructor();
0N/A
0N/A t.loadSuperClass();
0N/A
0N/A try {
0N/A t.checkClassVersion(49); // should not throw VerifyError
0N/A }
0N/A catch(VerifyError e) {
0N/A throw new Exception("FAIL: should be no VerifyError for CF version 49");
0N/A }
0N/A
0N/A try {
0N/A t.checkClassVersion(50); // should throw VerifyError
0N/A throw new Exception("FAIL: should be a VerifyError for CF version 50");
0N/A }
0N/A catch(VerifyError e) {
0N/A System.out.println("PASS");
0N/A }
0N/A }
0N/A
0N/A private void loadSuperClass() {
0N/A /* -- code for super class A.A --
0N/A package A;
0N/A public class A {
0N/A protected A() {}
0N/A }
0N/A */
0N/A long[] cls_data = {
0N/A 0xcafebabe00000032L, 0x000a0a0003000707L,
0N/A 0x0008070009010006L, 0x3c696e69743e0100L,
0N/A 0x0328295601000443L, 0x6f64650c00040005L,
0N/A 0x010003412f410100L, 0x106a6176612f6c61L,
0N/A 0x6e672f4f626a6563L, 0x7400210002000300L,
0N/A 0x0000000001000400L, 0x0400050001000600L,
0N/A 0x0000110001000100L, 0x0000052ab70001b1L,
0N/A 0x0000000000000000L // 2 bytes extra
0N/A };
0N/A final int EXTRA = 2;
0N/A byte cf_bytes[] = toByteArray(cls_data);
0N/A defineClass("A.A", cf_bytes, 0, cf_bytes.length - EXTRA);
0N/A }
0N/A
0N/A private int num_calls;
0N/A private static String classNames[] = { "B.B", "C.C" };
0N/A
0N/A private void checkClassVersion(int version) throws VerifyError {
0N/A // This class is in violation of the spec since it accesses
0N/A // a protected constructor of a superclass while not being in the
0N/A // same package.
0N/A /* -- code for test class --
0N/A package B;
0N/A public class B extends A.A {
0N/A public static void f() { new A.A(); }
0N/A }
0N/A */
0N/A long[] cls_data = {
0N/A 0xcafebabe00000032L, 0x000b0a0002000807L,
0N/A 0x000907000a010006L, 0x3c696e69743e0100L,
0N/A 0x0328295601000443L, 0x6f6465010001660cL,
0N/A 0x0004000501000341L, 0x2f41010003422f42L,
0N/A 0x0021000300020000L, 0x0000000200010004L,
0N/A 0x0005000100060000L, 0x0011000100010000L,
0N/A 0x00052ab70001b100L, 0x0000000009000700L,
0N/A 0x0500010006000000L, 0x1500020000000000L,
0N/A 0x09bb000259b70001L, 0x57b1000000000000L // no extra bytes
0N/A };
0N/A final int EXTRA = 0;
0N/A
0N/A byte cf_bytes[] = toByteArray(cls_data);
0N/A
0N/A // set version
0N/A cf_bytes[7] = (byte)version;
0N/A
0N/A // Change B.B to C.C, D.D, ... for subsequent calls so we can call this
0N/A // multiple times and define different classes.
0N/A cf_bytes[61] += num_calls;
0N/A cf_bytes[63] += num_calls;
0N/A String name = classNames[num_calls];
0N/A num_calls++;
0N/A
0N/A Class c = defineClass(name, cf_bytes, 0, cf_bytes.length - EXTRA);
0N/A
0N/A try { c.newInstance(); } // to force linking, thus verification
0N/A catch(InstantiationException e) {}
0N/A catch(IllegalAccessException e) {}
0N/A }
0N/A
0N/A static private byte[] toByteArray(long arr[]) {
0N/A // convert long array to byte array
0N/A java.nio.ByteBuffer bbuf = java.nio.ByteBuffer.allocate(arr.length * 8);
0N/A bbuf.asLongBuffer().put(java.nio.LongBuffer.wrap(arr));
0N/A return bbuf.array();
0N/A }
0N/A}