test/tools/javac/defaultMethods/TestDefaultBody.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/defaultMethods/TestDefaultBody.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,123 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 7192246
    1.30 + * @summary  check that code attributed for default methods is correctly generated
    1.31 + */
    1.32 +
    1.33 +import com.sun.tools.classfile.AccessFlags;
    1.34 +import com.sun.tools.classfile.Attribute;
    1.35 +import com.sun.tools.classfile.ClassFile;
    1.36 +import com.sun.tools.classfile.ConstantPool.*;
    1.37 +import com.sun.tools.classfile.Code_attribute;
    1.38 +import com.sun.tools.classfile.Instruction;
    1.39 +import com.sun.tools.classfile.Method;
    1.40 +
    1.41 +import com.sun.tools.classfile.Opcode;
    1.42 +import java.io.*;
    1.43 +
    1.44 +public class TestDefaultBody {
    1.45 +
    1.46 +    interface TestInterface {
    1.47 +        int no_default(int i);
    1.48 +        default int yes_default(int i) { return impl(this, i); }
    1.49 +    }
    1.50 +
    1.51 +    static int impl(TestInterface ti, int i) { return 0; }
    1.52 +
    1.53 +    static final String TARGET_CLASS_NAME = "TestDefaultBody";
    1.54 +    static final String TARGET_NAME = "impl";
    1.55 +    static final String TARGET_TYPE = "(LTestDefaultBody$TestInterface;I)I";
    1.56 +    static final String SUBTEST_NAME = TestInterface.class.getName() + ".class";
    1.57 +    static final String TEST_METHOD_NAME = "yes_default";
    1.58 +
    1.59 +    public static void main(String... args) throws Exception {
    1.60 +        new TestDefaultBody().run();
    1.61 +    }
    1.62 +
    1.63 +    public void run() throws Exception {
    1.64 +        String workDir = System.getProperty("test.classes");
    1.65 +        File compiledTest = new File(workDir, SUBTEST_NAME);
    1.66 +        verifyDefaultBody(compiledTest);
    1.67 +    }
    1.68 +
    1.69 +    void verifyDefaultBody(File f) {
    1.70 +        System.err.println("verify: " + f);
    1.71 +        try {
    1.72 +            ClassFile cf = ClassFile.read(f);
    1.73 +            Method testMethod = null;
    1.74 +            Code_attribute codeAttr = null;
    1.75 +            for (Method m : cf.methods) {
    1.76 +                codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
    1.77 +                String mname = m.getName(cf.constant_pool);
    1.78 +                if (mname.equals(TEST_METHOD_NAME)) {
    1.79 +                    testMethod = m;
    1.80 +                    break;
    1.81 +                } else {
    1.82 +                    codeAttr = null;
    1.83 +                }
    1.84 +            }
    1.85 +            if (testMethod == null) {
    1.86 +                throw new Error("Test method not found");
    1.87 +            }
    1.88 +            if (testMethod.access_flags.is(AccessFlags.ACC_ABSTRACT)) {
    1.89 +                throw new Error("Test method is abstract");
    1.90 +            }
    1.91 +            if (codeAttr == null) {
    1.92 +                throw new Error("Code attribute in test method not found");
    1.93 +            }
    1.94 +
    1.95 +            boolean found = false;
    1.96 +            for (Instruction instr : codeAttr.getInstructions()) {
    1.97 +                if (instr.getOpcode() == Opcode.INVOKESTATIC) {
    1.98 +                    found = true;
    1.99 +                    int pc_index = instr.getShort(1);
   1.100 +                    CONSTANT_Methodref_info mref = (CONSTANT_Methodref_info)cf.constant_pool.get(pc_index);
   1.101 +                    String className = mref.getClassName();
   1.102 +                    String targetName = mref.getNameAndTypeInfo().getName();
   1.103 +                    String targetType = mref.getNameAndTypeInfo().getType();
   1.104 +
   1.105 +                    if (!className.equals(TARGET_CLASS_NAME)) {
   1.106 +                        throw new Error("unexpected class in default method body " + className);
   1.107 +                    }
   1.108 +                    if (!targetName.equals(TARGET_NAME)) {
   1.109 +                        throw new Error("unexpected method name in default method body " + targetName);
   1.110 +                    }
   1.111 +                    if (!targetType.equals(TARGET_TYPE)) {
   1.112 +                        throw new Error("unexpected method type in default method body " + targetType);
   1.113 +                    }
   1.114 +                    break;
   1.115 +                }
   1.116 +            }
   1.117 +
   1.118 +            if (!found) {
   1.119 +                throw new Error("no invokestatic found in default method body");
   1.120 +            }
   1.121 +        } catch (Exception e) {
   1.122 +            e.printStackTrace();
   1.123 +            throw new Error("error reading " + f +": " + e);
   1.124 +        }
   1.125 +    }
   1.126 +}

mercurial