langtools/test/tools/javac/defaultMethods/TestDefaultBody.java
author mcimadamore
Sun, 04 Nov 2012 10:59:42 +0000
changeset 14443 91c05eb19277
child 14547 86d8d242b0c4
permissions -rw-r--r--
7192246: Add type-checking support for default methods Summary: Add type-checking support for default methods as per Featherweight-Defender document Reviewed-by: jjg, dlsmith
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     1
/*
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     4
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     8
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    14
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    18
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    21
 * questions.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    22
 */
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    23
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    24
/*
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    25
 * @test
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    26
 * @ignore awaits for VM support
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    27
 * @summary  check that code attributed for default methods is correctly generated
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    28
 * @compile -XDallowDefaultMethods TestDefaultBody.java
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    29
 * @run main TestDefaultBody
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    30
 */
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    31
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.classfile.AccessFlags;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.classfile.Attribute;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.classfile.ClassFile;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.classfile.ConstantPool.*;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    36
import com.sun.tools.classfile.Code_attribute;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    37
import com.sun.tools.classfile.Instruction;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    38
import com.sun.tools.classfile.Method;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    39
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    40
import com.sun.tools.classfile.Opcode;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    41
import java.io.*;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    42
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    43
public class TestDefaultBody {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    44
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    45
    interface TestInterface {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    46
        int no_default(int i);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    47
        default int yes_default(int i) { return impl(this, i); }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    48
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    49
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    50
    static int impl(TestInterface ti, int i) { return 0; }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    51
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    52
    static final String TARGET_CLASS_NAME = "TestDefaultBody";
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    53
    static final String TARGET_NAME = "impl";
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    54
    static final String TARGET_TYPE = "(LTestDefaultBody$TestInterface;I)I";
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    55
    static final String SUBTEST_NAME = TestInterface.class.getName() + ".class";
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    56
    static final String TEST_METHOD_NAME = "yes_default";
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    57
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    58
    public static void main(String... args) throws Exception {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    59
        new TestDefaultBody().run();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    60
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    61
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    62
    public void run() throws Exception {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    63
        String workDir = System.getProperty("test.classes");
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    64
        File compiledTest = new File(workDir, SUBTEST_NAME);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    65
        verifyDefaultBody(compiledTest);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    66
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    67
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    68
    void verifyDefaultBody(File f) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    69
        System.err.println("verify: " + f);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    70
        try {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    71
            ClassFile cf = ClassFile.read(f);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    72
            Method testMethod = null;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    73
            Code_attribute codeAttr = null;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    74
            for (Method m : cf.methods) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    75
                codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    76
                String mname = m.getName(cf.constant_pool);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    77
                if (mname.equals(TEST_METHOD_NAME)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    78
                    testMethod = m;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    79
                    break;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    80
                } else {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    81
                    codeAttr = null;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    82
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    83
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    84
            if (testMethod == null) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    85
                throw new Error("Test method not found");
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    86
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    87
            if (testMethod.access_flags.is(AccessFlags.ACC_ABSTRACT)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    88
                throw new Error("Test method is abstract");
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    89
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    90
            if (codeAttr == null) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    91
                throw new Error("Code attribute in test method not found");
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    92
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    93
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    94
            boolean found = false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    95
            for (Instruction instr : codeAttr.getInstructions()) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    96
                if (instr.getOpcode() == Opcode.INVOKESTATIC) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    97
                    found = true;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    98
                    int pc_index = instr.getShort(1);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    99
                    CONSTANT_Methodref_info mref = (CONSTANT_Methodref_info)cf.constant_pool.get(pc_index);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   100
                    String className = mref.getClassName();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   101
                    String targetName = mref.getNameAndTypeInfo().getName();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   102
                    String targetType = mref.getNameAndTypeInfo().getType();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   103
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   104
                    if (!className.equals(TARGET_CLASS_NAME)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   105
                        throw new Error("unexpected class in default method body " + className);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   106
                    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   107
                    if (!targetName.equals(TARGET_NAME)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   108
                        throw new Error("unexpected method name in default method body " + targetName);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   109
                    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   110
                    if (!targetType.equals(TARGET_TYPE)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   111
                        throw new Error("unexpected method type in default method body " + targetType);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   112
                    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   113
                    break;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   114
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   115
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   116
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   117
            if (!found) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   118
                throw new Error("no invokestatic found in default method body");
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   119
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   120
        } catch (Exception e) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   121
            e.printStackTrace();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   122
            throw new Error("error reading " + f +": " + e);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   123
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   124
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   125
}