hotspot/test/runtime/8026365/InvokeSpecialAnonTest.java
author jlahoda
Tue, 26 Nov 2013 15:27:19 +0100
changeset 21896 f39003ba351f
parent 21178 47909bd1abde
child 29678 dd2f3932c21e
permissions -rw-r--r--
8026374: javac accepts void as a method parameter Summary: Changing Check.validate to reject void types. Reviewed-by: jjg, vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21178
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     1
/*
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     4
 *
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     8
 *
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    13
 * accompanied this code).
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    14
 *
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    18
 *
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    21
 * questions.
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    22
 */
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    23
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    24
/*
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    25
 * @test
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    26
 * @bug 8026365
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    27
 * @summary Test invokespecial of host class method from an anonymous class
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    28
 * @author  Robert Field
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    29
 * @library /testlibrary
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    30
 * @compile -XDignore.symbol.file InvokeSpecialAnonTest.java
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    31
 * @run main ClassFileInstaller InvokeSpecialAnonTest AnonTester
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    32
 * @run main/othervm -Xbootclasspath/a:. -Xverify:all InvokeSpecialAnonTest
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    33
 */
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    34
import jdk.internal.org.objectweb.asm.*;
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    35
import java.lang.reflect.Constructor;
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    36
import sun.misc.Unsafe;
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    37
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    38
public class InvokeSpecialAnonTest implements Opcodes {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    39
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    40
    static byte[] anonClassBytes() throws Exception {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    41
        ClassWriter cw = new ClassWriter(0);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    42
        MethodVisitor mv;
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    43
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    44
        cw.visit(V1_8, ACC_FINAL + ACC_SUPER, "Anon", null, "java/lang/Object", null);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    45
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    46
        {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    47
            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    48
            mv.visitCode();
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    49
            mv.visitVarInsn(ALOAD, 0);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    50
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    51
            mv.visitInsn(RETURN);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    52
            mv.visitMaxs(2, 2);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    53
            mv.visitEnd();
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    54
        }
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    55
        {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    56
            mv = cw.visitMethod(ACC_PUBLIC, "m", "(LInvokeSpecialAnonTest;)I", null, null);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    57
            mv.visitCode();
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    58
            mv.visitVarInsn(ALOAD, 0);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    59
            mv.visitVarInsn(ALOAD, 1);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    60
            mv.visitMethodInsn(INVOKESPECIAL, "InvokeSpecialAnonTest", "privMethod", "()I");
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    61
            mv.visitInsn(IRETURN);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    62
            mv.visitMaxs(2, 3);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    63
            mv.visitEnd();
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    64
        }
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    65
        cw.visitEnd();
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    66
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    67
        return cw.toByteArray();
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    68
    }
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    69
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    70
    private int privMethod() { return 1234; }
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    71
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    72
    public static void main(String[] args) throws Exception {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    73
        Class<?> klass = InvokeSpecialAnonTest.class;
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    74
        try {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    75
           Class<?> result = AnonTester.defineTest(klass, anonClassBytes());
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    76
           System.out.println("Passed.");
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    77
        } catch (Exception e) {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    78
           e.printStackTrace();
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    79
           throw e;
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    80
        }
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    81
    }
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    82
}
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    83
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    84
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    85
class AnonTester {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    86
    private static final Unsafe UNSAFE = Unsafe.getUnsafe();
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    87
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    88
    public static Class<?> defineTest(Class<?> targetClass, byte[] classBytes) throws Exception {
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    89
        return UNSAFE.defineAnonymousClass(targetClass, classBytes, null);
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    90
    }
47909bd1abde 8026893: Push 8026365 to TL early and add test
acorn
parents:
diff changeset
    91
}