Java Code Examples for com.sun.tools.classfile.ClassFile#getAttribute()

The following examples show how to use com.sun.tools.classfile.ClassFile#getAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeprecatedTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void testInnerClasses(ClassFile cf, Map<String, ? extends JavaFileObject> classes)
        throws ConstantPoolException, IOException {
    InnerClasses_attribute innerAttr = (InnerClasses_attribute)
            cf.getAttribute(Attribute.InnerClasses);
    for (Info innerClass : innerAttr.classes) {
        String innerClassName = cf.constant_pool.
                getClassInfo(innerClass.inner_class_info_index).getName();
        echo("Testing inner class : " + innerClassName);
        ClassFile innerCf = readClassFile(classes.get(innerClassName));
        Deprecated_attribute attr = (Deprecated_attribute)
                innerCf.getAttribute(Attribute.Deprecated);
        String innerClassSimpleName = innerClass.getInnerName(cf.constant_pool);
        testAttribute(innerClassSimpleName, attr, innerCf);
        if (innerClassName.contains("Local")) {
            testMethods(innerCf);
            testFields(innerCf);
        }
    }
}
 
Example 2
Source File: DeprecatedTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void test(String src) {
    addTestCase(src);
    printf("Testing test case :\n%s\n", src);
    try {
        Map<String, ? extends JavaFileObject> classes = compile(src).getClasses();
        String outerClassName = classes.containsKey("deprecated")
                ? "deprecated"
                : "notDeprecated";
        echo("Testing outer class : " + outerClassName);
        ClassFile cf = readClassFile(classes.get(outerClassName));
        Deprecated_attribute attr = (Deprecated_attribute)
                cf.getAttribute(Attribute.Deprecated);
        testAttribute(outerClassName, attr, cf);
        testInnerClasses(cf, classes);
        testMethods(cf);
        testFields(cf);
    } catch (Exception e) {
        addFailure(e);
    }
}
 
Example 3
Source File: TestBootstrapMethodsCount.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void verifyBytecode() {
    File compiledTest = new File("Test.class");
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                        .getAttribute(Attribute.BootstrapMethods);
        int length = bsm_attr.bootstrap_method_specifiers.length;
        if (length != 1) {
            throw new Error("Bad number of method specifiers " +
                    "in BootstrapMethods attribute: " + length);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example 4
Source File: T4241573.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
        String found = sfa.getSourceFile(cf.constant_pool);
        String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
        if (!expect.equals(found)) {
            error("bad value found: " + found + ", expected: " + expect);
        }
    } catch (Exception e) {
        error("error reading " + f +": " + e);
    }
}
 
Example 5
Source File: T4241573.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
        String found = sfa.getSourceFile(cf.constant_pool);
        String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
        if (!expect.equals(found)) {
            error("bad value found: " + found + ", expected: " + expect);
        }
    } catch (Exception e) {
        error("error reading " + f +": " + e);
    }
}
 
Example 6
Source File: T4241573.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
        String found = sfa.getSourceFile(cf.constant_pool);
        String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
        if (!expect.equals(found)) {
            error("bad value found: " + found + ", expected: " + expect);
        }
    } catch (Exception e) {
        error("error reading " + f +": " + e);
    }
}
 
Example 7
Source File: ModuleTestBase.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected void testModuleAttribute(Path modulePath, ModuleDescriptor moduleDescriptor) throws Exception {
    ClassFile classFile = ClassFile.read(modulePath.resolve("module-info.class"));
    Module_attribute moduleAttribute = (Module_attribute) classFile.getAttribute("Module");
    ConstantPool constantPool = classFile.constant_pool;
    testModuleName(moduleDescriptor, moduleAttribute, constantPool);
    testModuleFlags(moduleDescriptor, moduleAttribute);
    testRequires(moduleDescriptor, moduleAttribute, constantPool);
    testExports(moduleDescriptor, moduleAttribute, constantPool);
    testOpens(moduleDescriptor, moduleAttribute, constantPool);
    testProvides(moduleDescriptor, moduleAttribute, constantPool);
    testUses(moduleDescriptor, moduleAttribute, constantPool);
}
 
Example 8
Source File: InnerClassesIndexTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void test() throws TestFailedException {
    try {
        addTestCase("Source is InnerClassesIndexTest.java");
        ClassFile classFile = readClassFile(InnerClassesIndexTest.class);
        InnerClasses_attribute attr = (InnerClasses_attribute)
                classFile.getAttribute(Attribute.InnerClasses);

        Set<String> foundClasses = new HashSet<>();
        for (Info info : attr.classes) {
            String innerName = classFile.constant_pool.
                    getClassInfo(info.inner_class_info_index).getBaseName();
            echo("Testing class : " + innerName);
            if (isExcluded(innerName)) {
                echo("Ignored : " + innerName);
                continue;
            }
            foundClasses.add(innerName);
            checkEquals(info.outer_class_info_index, 0,
                    "outer_class_info_index of " + innerName);
            if (innerName.matches("\\$\\d+")) {
                checkEquals(info.inner_name_index, 0,
                        "inner_name_index of anonymous class");
            }
        }
        Set<String> expectedClasses = getInnerClasses();
        expectedClasses.remove("InnerClassesIndexTest$Inner");
        checkEquals(foundClasses, expectedClasses, "All classes are found");
    } catch (Exception e) {
        addFailure(e);
    } finally {
        checkStatus();
    }
}
 
Example 9
Source File: DeprecatedPackageTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void test(String package_info, String src) {
    addTestCase(src);
    printf("Testing test case: \n%s\n", src);
    try {
        ClassFile cf = readClassFile(compile(
                    new String[]{"package-info.java", package_info},
                    new String[]{"notDeprecated.java", src})
                .getClasses().get(CLASS_NAME));
        Deprecated_attribute attr =
                (Deprecated_attribute) cf.getAttribute(Attribute.Deprecated);
        checkNull(attr, "Class can not have deprecated attribute : " + CLASS_NAME);
    } catch (Exception e) {
        addFailure(e);
    }
}
 
Example 10
Source File: T4241573.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
        String found = sfa.getSourceFile(cf.constant_pool);
        String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
        if (!expect.equals(found)) {
            error("bad value found: " + found + ", expected: " + expect);
        }
    } catch (Exception e) {
        error("error reading " + f +": " + e);
    }
}
 
Example 11
Source File: T4241573.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
        String found = sfa.getSourceFile(cf.constant_pool);
        String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
        if (!expect.equals(found)) {
            error("bad value found: " + found + ", expected: " + expect);
        }
    } catch (Exception e) {
        error("error reading " + f +": " + e);
    }
}
 
Example 12
Source File: T4241573.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
        String found = sfa.getSourceFile(cf.constant_pool);
        String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
        if (!expect.equals(found)) {
            error("bad value found: " + found + ", expected: " + expect);
        }
    } catch (Exception e) {
        error("error reading " + f +": " + e);
    }
}
 
Example 13
Source File: TestLambdaBytecode.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void verifyBytecode(Result<Iterable<? extends JavaFileObject>> res) {
    if (res.hasErrors()) {
        boolean errorExpected = !mk1.isOK() || !mk2.isOK();
        errorExpected |= mk1.isStatic() && !mk2.isStatic();

        if (!errorExpected) {
            fail("Diags found when compiling instance; " + res.compilationInfo());
        }
        return;
    }
    try (InputStream is = res.get().iterator().next().openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            fail("Test method not found");
            return;
        }
        Code_attribute ea =
                (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            fail("Code attribute for test() method not found");
            return;
        }

        int bsmIdx = -1;

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokedynamic")) {
                CONSTANT_InvokeDynamic_info indyInfo =
                     (CONSTANT_InvokeDynamic_info)cf
                        .constant_pool.get(i.getShort(1));
                bsmIdx = indyInfo.bootstrap_method_attr_index;
                if (!indyInfo.getNameAndTypeInfo().getType().equals(makeIndyType())) {
                    fail("type mismatch for CONSTANT_InvokeDynamic_info " +
                            res.compilationInfo() + "\n" + indyInfo.getNameAndTypeInfo().getType() +
                            "\n" + makeIndyType());
                    return;
                }
            }
        }
        if (bsmIdx == -1) {
            fail("Missing invokedynamic in generated code");
            return;
        }

        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                .getAttribute(Attribute.BootstrapMethods);
        if (bsm_attr.bootstrap_method_specifiers.length != 1) {
            fail("Bad number of method specifiers " +
                    "in BootstrapMethods attribute");
            return;
        }
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec =
                bsm_attr.bootstrap_method_specifiers[0];

        if (bsm_spec.bootstrap_arguments.length != MF_ARITY) {
            fail("Bad number of static invokedynamic args " +
                    "in BootstrapMethod attribute");
            return;
        }

        CONSTANT_MethodHandle_info mh =
                (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_arguments[1]);

        boolean kindOK;
        switch (mh.reference_kind) {
            case REF_invokeStatic: kindOK = mk2.isStatic(); break;
            case REF_invokeSpecial: kindOK = !mk2.isStatic(); break;
            case REF_invokeInterface: kindOK = mk2.inInterface(); break;
            default:
                kindOK = false;
        }

        if (!kindOK) {
            fail("Bad invoke kind in implementation method handle");
            return;
        }

        if (!mh.getCPRefInfo().getNameAndTypeInfo().getType().toString().equals(MH_SIG)) {
            fail("Type mismatch in implementation method handle");
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("error reading " + res.compilationInfo() + ": " + e);
    }
}
 
Example 14
Source File: TestLambdaBytecode.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
void verifyBytecode(int id, JavaSource source) {
    File compiledTest = new File(String.format("Test%d.class", id));
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea =
                (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        int bsmIdx = -1;

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokedynamic")) {
                CONSTANT_InvokeDynamic_info indyInfo =
                     (CONSTANT_InvokeDynamic_info)cf
                        .constant_pool.get(i.getShort(1));
                bsmIdx = indyInfo.bootstrap_method_attr_index;
                if (!indyInfo.getNameAndTypeInfo().getType().equals(makeIndyType(id))) {
                    throw new
                        AssertionError("type mismatch for CONSTANT_InvokeDynamic_info " + source.source + "\n" + indyInfo.getNameAndTypeInfo().getType() + "\n" + makeIndyType(id));
                }
            }
        }
        if (bsmIdx == -1) {
            throw new Error("Missing invokedynamic in generated code");
        }

        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                .getAttribute(Attribute.BootstrapMethods);
        if (bsm_attr.bootstrap_method_specifiers.length != 1) {
            throw new Error("Bad number of method specifiers " +
                    "in BootstrapMethods attribute");
        }
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec =
                bsm_attr.bootstrap_method_specifiers[0];

        if (bsm_spec.bootstrap_arguments.length != MF_ARITY) {
            throw new Error("Bad number of static invokedynamic args " +
                    "in BootstrapMethod attribute");
        }

        CONSTANT_MethodHandle_info mh =
                (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_arguments[1]);

        boolean kindOK;
        switch (mh.reference_kind) {
            case REF_invokeStatic: kindOK = mk2.isStatic(); break;
            case REF_invokeSpecial: kindOK = !mk2.isStatic(); break;
            case REF_invokeInterface: kindOK = mk2.inInterface(); break;
            default:
                kindOK = false;
        }

        if (!kindOK) {
            throw new Error("Bad invoke kind in implementation method handle");
        }

        if (!mh.getCPRefInfo().getNameAndTypeInfo().getType().toString().equals(MH_SIG)) {
            throw new Error("Type mismatch in implementation method handle");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example 15
Source File: TestLambdaBytecode.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
void verifyBytecode(int id, JavaSource source) {
    File compiledTest = new File(String.format("Test%d.class", id));
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea =
                (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        int bsmIdx = -1;

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokedynamic")) {
                CONSTANT_InvokeDynamic_info indyInfo =
                     (CONSTANT_InvokeDynamic_info)cf
                        .constant_pool.get(i.getShort(1));
                bsmIdx = indyInfo.bootstrap_method_attr_index;
                if (!indyInfo.getNameAndTypeInfo().getType().equals(makeIndyType(id))) {
                    throw new
                        AssertionError("type mismatch for CONSTANT_InvokeDynamic_info " + source.source + "\n" + indyInfo.getNameAndTypeInfo().getType() + "\n" + makeIndyType(id));
                }
            }
        }
        if (bsmIdx == -1) {
            throw new Error("Missing invokedynamic in generated code");
        }

        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                .getAttribute(Attribute.BootstrapMethods);
        if (bsm_attr.bootstrap_method_specifiers.length != 1) {
            throw new Error("Bad number of method specifiers " +
                    "in BootstrapMethods attribute");
        }
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec =
                bsm_attr.bootstrap_method_specifiers[0];

        if (bsm_spec.bootstrap_arguments.length != MF_ARITY) {
            throw new Error("Bad number of static invokedynamic args " +
                    "in BootstrapMethod attribute");
        }

        CONSTANT_MethodHandle_info mh =
                (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_arguments[1]);

        boolean kindOK;
        switch (mh.reference_kind) {
            case REF_invokeStatic: kindOK = mk2.isStatic(); break;
            case REF_invokeSpecial: kindOK = !mk2.isStatic(); break;
            case REF_invokeInterface: kindOK = mk2.inInterface(); break;
            default:
                kindOK = false;
        }

        if (!kindOK) {
            throw new Error("Bad invoke kind in implementation method handle");
        }

        if (!mh.getCPRefInfo().getNameAndTypeInfo().getType().toString().equals(MH_SIG)) {
            throw new Error("Type mismatch in implementation method handle");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example 16
Source File: TestLambdaBytecode.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
void verifyBytecode(int id, JavaSource source) {
    File compiledTest = new File(String.format("Test%d.class", id));
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea =
                (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        int bsmIdx = -1;

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokedynamic")) {
                CONSTANT_InvokeDynamic_info indyInfo =
                     (CONSTANT_InvokeDynamic_info)cf
                        .constant_pool.get(i.getShort(1));
                bsmIdx = indyInfo.bootstrap_method_attr_index;
                if (!indyInfo.getNameAndTypeInfo().getType().equals(makeIndyType(id))) {
                    throw new
                        AssertionError("type mismatch for CONSTANT_InvokeDynamic_info " + source.source + "\n" + indyInfo.getNameAndTypeInfo().getType() + "\n" + makeIndyType(id));
                }
            }
        }
        if (bsmIdx == -1) {
            throw new Error("Missing invokedynamic in generated code");
        }

        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                .getAttribute(Attribute.BootstrapMethods);
        if (bsm_attr.bootstrap_method_specifiers.length != 1) {
            throw new Error("Bad number of method specifiers " +
                    "in BootstrapMethods attribute");
        }
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec =
                bsm_attr.bootstrap_method_specifiers[0];

        if (bsm_spec.bootstrap_arguments.length != MF_ARITY) {
            throw new Error("Bad number of static invokedynamic args " +
                    "in BootstrapMethod attribute");
        }

        CONSTANT_MethodHandle_info mh =
                (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_arguments[1]);

        boolean kindOK;
        switch (mh.reference_kind) {
            case REF_invokeStatic: kindOK = mk2.isStatic(); break;
            case REF_invokeSpecial: kindOK = !mk2.isStatic(); break;
            case REF_invokeInterface: kindOK = mk2.inInterface(); break;
            default:
                kindOK = false;
        }

        if (!kindOK) {
            throw new Error("Bad invoke kind in implementation method handle");
        }

        if (!mh.getCPRefInfo().getNameAndTypeInfo().getType().toString().equals(MH_SIG)) {
            throw new Error("Type mismatch in implementation method handle");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example 17
Source File: TestLambdaBytecode.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
void verifyBytecode(int id, JavaSource source) {
    File compiledTest = new File(String.format("Test%d.class", id));
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea =
                (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        int bsmIdx = -1;

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokedynamic")) {
                CONSTANT_InvokeDynamic_info indyInfo =
                     (CONSTANT_InvokeDynamic_info)cf
                        .constant_pool.get(i.getShort(1));
                bsmIdx = indyInfo.bootstrap_method_attr_index;
                if (!indyInfo.getNameAndTypeInfo().getType().equals(makeIndyType(id))) {
                    throw new
                        AssertionError("type mismatch for CONSTANT_InvokeDynamic_info " + source.source + "\n" + indyInfo.getNameAndTypeInfo().getType() + "\n" + makeIndyType(id));
                }
            }
        }
        if (bsmIdx == -1) {
            throw new Error("Missing invokedynamic in generated code");
        }

        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                .getAttribute(Attribute.BootstrapMethods);
        if (bsm_attr.bootstrap_method_specifiers.length != 1) {
            throw new Error("Bad number of method specifiers " +
                    "in BootstrapMethods attribute");
        }
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec =
                bsm_attr.bootstrap_method_specifiers[0];

        if (bsm_spec.bootstrap_arguments.length != MF_ARITY) {
            throw new Error("Bad number of static invokedynamic args " +
                    "in BootstrapMethod attribute");
        }

        CONSTANT_MethodHandle_info mh =
                (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_arguments[1]);

        boolean kindOK;
        switch (mh.reference_kind) {
            case REF_invokeStatic: kindOK = mk2.isStatic(); break;
            case REF_invokeSpecial: kindOK = !mk2.isStatic(); break;
            case REF_invokeInterface: kindOK = mk2.inInterface(); break;
            default:
                kindOK = false;
        }

        if (!kindOK) {
            throw new Error("Bad invoke kind in implementation method handle");
        }

        if (!mh.getCPRefInfo().getNameAndTypeInfo().getType().toString().equals(MH_SIG)) {
            throw new Error("Type mismatch in implementation method handle");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example 18
Source File: TestLambdaBytecode.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
void verifyBytecode(int id, JavaSource source) {
    File compiledTest = new File(String.format("Test%d.class", id));
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea =
                (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        int bsmIdx = -1;

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokedynamic")) {
                CONSTANT_InvokeDynamic_info indyInfo =
                     (CONSTANT_InvokeDynamic_info)cf
                        .constant_pool.get(i.getShort(1));
                bsmIdx = indyInfo.bootstrap_method_attr_index;
                if (!indyInfo.getNameAndTypeInfo().getType().equals(makeIndyType(id))) {
                    throw new
                        AssertionError("type mismatch for CONSTANT_InvokeDynamic_info " + source.source + "\n" + indyInfo.getNameAndTypeInfo().getType() + "\n" + makeIndyType(id));
                }
            }
        }
        if (bsmIdx == -1) {
            throw new Error("Missing invokedynamic in generated code");
        }

        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                .getAttribute(Attribute.BootstrapMethods);
        if (bsm_attr.bootstrap_method_specifiers.length != 1) {
            throw new Error("Bad number of method specifiers " +
                    "in BootstrapMethods attribute");
        }
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec =
                bsm_attr.bootstrap_method_specifiers[0];

        if (bsm_spec.bootstrap_arguments.length != MF_ARITY) {
            throw new Error("Bad number of static invokedynamic args " +
                    "in BootstrapMethod attribute");
        }

        CONSTANT_MethodHandle_info mh =
                (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_arguments[1]);

        boolean kindOK;
        switch (mh.reference_kind) {
            case REF_invokeStatic: kindOK = mk2.isStatic(); break;
            case REF_invokeSpecial: kindOK = !mk2.isStatic(); break;
            case REF_invokeInterface: kindOK = mk2.inInterface(); break;
            default:
                kindOK = false;
        }

        if (!kindOK) {
            throw new Error("Bad invoke kind in implementation method handle");
        }

        if (!mh.getCPRefInfo().getNameAndTypeInfo().getType().toString().equals(MH_SIG)) {
            throw new Error("Type mismatch in implementation method handle");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example 19
Source File: TestLambdaBytecode.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
void verifyBytecode(int id, JavaSource source) {
    File compiledTest = new File(String.format("Test%d.class", id));
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea =
                (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        int bsmIdx = -1;

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokedynamic")) {
                CONSTANT_InvokeDynamic_info indyInfo =
                     (CONSTANT_InvokeDynamic_info)cf
                        .constant_pool.get(i.getShort(1));
                bsmIdx = indyInfo.bootstrap_method_attr_index;
                if (!indyInfo.getNameAndTypeInfo().getType().equals(makeIndyType(id))) {
                    throw new
                        AssertionError("type mismatch for CONSTANT_InvokeDynamic_info " + source.source + "\n" + indyInfo.getNameAndTypeInfo().getType() + "\n" + makeIndyType(id));
                }
            }
        }
        if (bsmIdx == -1) {
            throw new Error("Missing invokedynamic in generated code");
        }

        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                .getAttribute(Attribute.BootstrapMethods);
        if (bsm_attr.bootstrap_method_specifiers.length != 1) {
            throw new Error("Bad number of method specifiers " +
                    "in BootstrapMethods attribute");
        }
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec =
                bsm_attr.bootstrap_method_specifiers[0];

        if (bsm_spec.bootstrap_arguments.length != MF_ARITY) {
            throw new Error("Bad number of static invokedynamic args " +
                    "in BootstrapMethod attribute");
        }

        CONSTANT_MethodHandle_info mh =
                (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_arguments[1]);

        boolean kindOK;
        switch (mh.reference_kind) {
            case REF_invokeStatic: kindOK = mk2.isStatic(); break;
            case REF_invokeSpecial: kindOK = !mk2.isStatic(); break;
            case REF_invokeInterface: kindOK = mk2.inInterface(); break;
            default:
                kindOK = false;
        }

        if (!kindOK) {
            throw new Error("Bad invoke kind in implementation method handle");
        }

        if (!mh.getCPRefInfo().getNameAndTypeInfo().getType().toString().equals(MH_SIG)) {
            throw new Error("Type mismatch in implementation method handle");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example 20
Source File: TestLambdaBytecode.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
void verifyBytecode(int id, JavaSource source) {
    File compiledTest = new File(String.format("Test%d.class", id));
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea =
                (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        int bsmIdx = -1;

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokedynamic")) {
                CONSTANT_InvokeDynamic_info indyInfo =
                     (CONSTANT_InvokeDynamic_info)cf
                        .constant_pool.get(i.getShort(1));
                bsmIdx = indyInfo.bootstrap_method_attr_index;
                if (!indyInfo.getNameAndTypeInfo().getType().equals(makeIndyType(id))) {
                    throw new
                        AssertionError("type mismatch for CONSTANT_InvokeDynamic_info " + source.source + "\n" + indyInfo.getNameAndTypeInfo().getType() + "\n" + makeIndyType(id));
                }
            }
        }
        if (bsmIdx == -1) {
            throw new Error("Missing invokedynamic in generated code");
        }

        BootstrapMethods_attribute bsm_attr =
                (BootstrapMethods_attribute)cf
                .getAttribute(Attribute.BootstrapMethods);
        if (bsm_attr.bootstrap_method_specifiers.length != 1) {
            throw new Error("Bad number of method specifiers " +
                    "in BootstrapMethods attribute");
        }
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec =
                bsm_attr.bootstrap_method_specifiers[0];

        if (bsm_spec.bootstrap_arguments.length != MF_ARITY) {
            throw new Error("Bad number of static invokedynamic args " +
                    "in BootstrapMethod attribute");
        }

        CONSTANT_MethodHandle_info mh =
                (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_arguments[1]);

        boolean kindOK;
        switch (mh.reference_kind) {
            case REF_invokeStatic: kindOK = mk2.isStatic(); break;
            case REF_invokeSpecial: kindOK = !mk2.isStatic(); break;
            case REF_invokeInterface: kindOK = mk2.inInterface(); break;
            default:
                kindOK = false;
        }

        if (!kindOK) {
            throw new Error("Bad invoke kind in implementation method handle");
        }

        if (!mh.getCPRefInfo().getNameAndTypeInfo().getType().toString().equals(MH_SIG)) {
            throw new Error("Type mismatch in implementation method handle");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}