Java Code Examples for com.sun.tools.classfile.Method#getName()

The following examples show how to use com.sun.tools.classfile.Method#getName() . 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: LambdaAsm.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static int checkMethod(ClassFile cf, String mthd) throws Exception {
    if (cf.major_version < 52) {
        throw new RuntimeException("unexpected class file version, in "
                + cf.getName() + "expected 52, got " + cf.major_version);
    }
    int count = 0;
    for (Method m : cf.methods) {
        String mname = m.getName(cf.constant_pool);
        if (mname.equals(mthd)) {
            for (Attribute a : m.attributes) {
                if ("Code".equals(a.getName(cf.constant_pool))) {
                    count++;
                    checkMethod(cf.getName(), mname, cf.constant_pool,
                            (Code_attribute) a);
                }
            }
        }
    }
    return count;
}
 
Example 2
Source File: LambdaAsm.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static int checkMethod(ClassFile cf, String mthd) throws Exception {
    if (cf.major_version < 52) {
        throw new RuntimeException("unexpected class file version, in "
                + cf.getName() + "expected 52, got " + cf.major_version);
    }
    int count = 0;
    for (Method m : cf.methods) {
        String mname = m.getName(cf.constant_pool);
        if (mname.equals(mthd)) {
            for (Attribute a : m.attributes) {
                if ("Code".equals(a.getName(cf.constant_pool))) {
                    count++;
                    checkMethod(cf.getName(), mname, cf.constant_pool,
                            (Code_attribute) a);
                }
            }
        }
    }
    return count;
}
 
Example 3
Source File: LVTHarness.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
void checkClassFile(File file)
        throws IOException, ConstantPoolException, InvalidDescriptor {
    ClassFile classFile = ClassFile.read(file);
    ConstantPool constantPool = classFile.constant_pool;

    //lets get all the methods in the class file.
    for (Method method : classFile.methods) {
        for (ElementKey elementKey: aliveRangeMap.keySet()) {
            String methodDesc = method.getName(constantPool) +
                    method.descriptor.getParameterTypes(constantPool).replace(" ", "");
            if (methodDesc.equals(elementKey.elem.toString())) {
                checkMethod(constantPool, method, aliveRangeMap.get(elementKey));
                seenAliveRanges.add(elementKey);
            }
        }
    }
}
 
Example 4
Source File: LambdaAsm.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static int checkMethod(ClassFile cf, String mthd) throws Exception {
    if (cf.major_version < 52) {
        throw new RuntimeException("unexpected class file version, in "
                + cf.getName() + "expected 52, got " + cf.major_version);
    }
    int count = 0;
    for (Method m : cf.methods) {
        String mname = m.getName(cf.constant_pool);
        if (mname.equals(mthd)) {
            for (Attribute a : m.attributes) {
                if ("Code".equals(a.getName(cf.constant_pool))) {
                    count++;
                    checkMethod(cf.getName(), mname, cf.constant_pool,
                            (Code_attribute) a);
                }
            }
        }
    }
    return count;
}
 
Example 5
Source File: LambdaAsm.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static int checkMethod(ClassFile cf, String mthd) throws Exception {
    if (cf.major_version < 52) {
        throw new RuntimeException("unexpected class file version, in "
                + cf.getName() + "expected 52, got " + cf.major_version);
    }
    int count = 0;
    for (Method m : cf.methods) {
        String mname = m.getName(cf.constant_pool);
        if (mname.equals(mthd)) {
            for (Attribute a : m.attributes) {
                if ("Code".equals(a.getName(cf.constant_pool))) {
                    count++;
                    checkMethod(cf.getName(), mname, cf.constant_pool,
                            (Code_attribute) a);
                }
            }
        }
    }
    return count;
}
 
Example 6
Source File: LVTHarness.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
void checkClassFile(File file)
        throws IOException, ConstantPoolException, InvalidDescriptor {
    ClassFile classFile = ClassFile.read(file);
    ConstantPool constantPool = classFile.constant_pool;

    //lets get all the methods in the class file.
    for (Method method : classFile.methods) {
        for (ElementKey elementKey: aliveRangeMap.keySet()) {
            String methodDesc = method.getName(constantPool) +
                    method.descriptor.getParameterTypes(constantPool).replace(" ", "");
            if (methodDesc.equals(elementKey.elem.toString())) {
                checkMethod(constantPool, method, aliveRangeMap.get(elementKey));
                seenAliveRanges.add(elementKey);
            }
        }
    }
}
 
Example 7
Source File: ClassWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
String getName(Method m) {
    try {
        return m.getName(constant_pool);
    } catch (ConstantPoolException e) {
        return report(e);
    }
}
 
Example 8
Source File: TestNoBridgeOnDefaults.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void checkNoBridgeOnDefaults(File f) {
    System.err.println("check: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        for (Method m : cf.methods) {
            String mname = m.getName(cf.constant_pool);
            if (mname.equals(TEST_METHOD_NAME)) {
                throw new Error("unexpected bridge method found " + m);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f +": " + e);
    }
}
 
Example 9
Source File: TestNonSerializableLambdaNameStability.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
    List<JavaFileObject> sources = new ArrayList<>();

    for (int i = 0; i < 3; i++) {
        sources.add(new SourceJavaFileObject("L" + i, String.format(lambdaSource, i)));
    }

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    try (MemoryFileManager fm = new MemoryFileManager(compiler.getStandardFileManager(null, null, null))) {
        if (!compiler.getTask(null, fm, null, null, null, sources).call()) {
            throw new AssertionError("Compilation failed!");
        }

        for (String file : fm.name2Content.keySet()) {
            byte[] fileBytes = fm.name2Content.get(file);
            try (InputStream in = new ByteArrayInputStream(fileBytes)) {
                boolean foundLambdaMethod = false;
                ClassFile cf = ClassFile.read(in);
                StringBuilder seenMethods = new StringBuilder();
                String sep = "";
                for (Method m : cf.methods) {
                    String methodName = m.getName(cf.constant_pool);
                    if (expectedLambdaMethodName.equals(methodName)) {
                        foundLambdaMethod = true;
                        break;
                    }
                    seenMethods.append(sep);
                    seenMethods.append(methodName);
                    sep = ", ";
                }

                if (!foundLambdaMethod) {
                    throw new AbstractMethodError("Did not find the lambda method, " +
                                                  "found methods: " + seenMethods.toString());
                }
            }
        }
    }
}
 
Example 10
Source File: ClassWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
String getName(Method m) {
    try {
        return m.getName(constant_pool);
    } catch (ConstantPoolException e) {
        return report(e);
    }
}
 
Example 11
Source File: TestNoBridgeOnDefaults.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void checkNoBridgeOnDefaults(File f) {
    System.err.println("check: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        for (Method m : cf.methods) {
            String mname = m.getName(cf.constant_pool);
            if (mname.equals(TEST_METHOD_NAME)) {
                throw new Error("unexpected bridge method found " + m);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f +": " + e);
    }
}
 
Example 12
Source File: ClassWriter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
String getName(Method m) {
    try {
        return m.getName(constant_pool);
    } catch (ConstantPoolException e) {
        return report(e);
    }
}
 
Example 13
Source File: TestNoBridgeOnDefaults.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void checkNoBridgeOnDefaults(File f) {
    System.err.println("check: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        for (Method m : cf.methods) {
            String mname = m.getName(cf.constant_pool);
            if (mname.equals(TEST_METHOD_NAME)) {
                throw new Error("unexpected bridge method found " + m);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f +": " + e);
    }
}
 
Example 14
Source File: TestNonSerializableLambdaNameStability.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
    List<JavaFileObject> sources = new ArrayList<>();

    for (int i = 0; i < 3; i++) {
        sources.add(new SourceJavaFileObject("L" + i, String.format(lambdaSource, i)));
    }

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    try (MemoryFileManager fm = new MemoryFileManager(compiler.getStandardFileManager(null, null, null))) {
        if (!compiler.getTask(null, fm, null, null, null, sources).call()) {
            throw new AssertionError("Compilation failed!");
        }

        for (String file : fm.name2Content.keySet()) {
            byte[] fileBytes = fm.name2Content.get(file);
            try (InputStream in = new ByteArrayInputStream(fileBytes)) {
                boolean foundLambdaMethod = false;
                ClassFile cf = ClassFile.read(in);
                StringBuilder seenMethods = new StringBuilder();
                String sep = "";
                for (Method m : cf.methods) {
                    String methodName = m.getName(cf.constant_pool);
                    if (expectedLambdaMethodName.equals(methodName)) {
                        foundLambdaMethod = true;
                        break;
                    }
                    seenMethods.append(sep);
                    seenMethods.append(methodName);
                    sep = ", ";
                }

                if (!foundLambdaMethod) {
                    throw new AbstractMethodError("Did not find the lambda method, " +
                                                  "found methods: " + seenMethods.toString());
                }
            }
        }
    }
}
 
Example 15
Source File: BridgeHarness.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * return a string representation of a bytecode method
 */
static String descriptor(Method m, ConstantPool cp) throws ConstantPoolException {
    return m.getName(cp) + m.descriptor.getValue(cp);
}
 
Example 16
Source File: TestDefaultBody.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
void verifyDefaultBody(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        Method testMethod = null;
        Code_attribute codeAttr = null;
        for (Method m : cf.methods) {
            codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
            String mname = m.getName(cf.constant_pool);
            if (mname.equals(TEST_METHOD_NAME)) {
                testMethod = m;
                break;
            } else {
                codeAttr = null;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        if (testMethod.access_flags.is(AccessFlags.ACC_ABSTRACT)) {
            throw new Error("Test method is abstract");
        }
        if (codeAttr == null) {
            throw new Error("Code attribute in test method not found");
        }

        boolean found = false;
        for (Instruction instr : codeAttr.getInstructions()) {
            if (instr.getOpcode() == Opcode.INVOKESTATIC) {
                found = true;
                int pc_index = instr.getShort(1);
                CONSTANT_Methodref_info mref = (CONSTANT_Methodref_info)cf.constant_pool.get(pc_index);
                String className = mref.getClassName();
                String targetName = mref.getNameAndTypeInfo().getName();
                String targetType = mref.getNameAndTypeInfo().getType();

                if (!className.equals(TARGET_CLASS_NAME)) {
                    throw new Error("unexpected class in default method body " + className);
                }
                if (!targetName.equals(TARGET_NAME)) {
                    throw new Error("unexpected method name in default method body " + targetName);
                }
                if (!targetType.equals(TARGET_TYPE)) {
                    throw new Error("unexpected method type in default method body " + targetType);
                }
                break;
            }
        }

        if (!found) {
            throw new Error("no invokestatic found in default method body");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f +": " + e);
    }
}
 
Example 17
Source File: TestDefaultBody.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
void verifyDefaultBody(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        Method testMethod = null;
        Code_attribute codeAttr = null;
        for (Method m : cf.methods) {
            codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
            String mname = m.getName(cf.constant_pool);
            if (mname.equals(TEST_METHOD_NAME)) {
                testMethod = m;
                break;
            } else {
                codeAttr = null;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        if (testMethod.access_flags.is(AccessFlags.ACC_ABSTRACT)) {
            throw new Error("Test method is abstract");
        }
        if (codeAttr == null) {
            throw new Error("Code attribute in test method not found");
        }

        boolean found = false;
        for (Instruction instr : codeAttr.getInstructions()) {
            if (instr.getOpcode() == Opcode.INVOKESTATIC) {
                found = true;
                int pc_index = instr.getShort(1);
                CONSTANT_Methodref_info mref = (CONSTANT_Methodref_info)cf.constant_pool.get(pc_index);
                String className = mref.getClassName();
                String targetName = mref.getNameAndTypeInfo().getName();
                String targetType = mref.getNameAndTypeInfo().getType();

                if (!className.equals(TARGET_CLASS_NAME)) {
                    throw new Error("unexpected class in default method body " + className);
                }
                if (!targetName.equals(TARGET_NAME)) {
                    throw new Error("unexpected method name in default method body " + targetName);
                }
                if (!targetType.equals(TARGET_TYPE)) {
                    throw new Error("unexpected method type in default method body " + targetType);
                }
                break;
            }
        }

        if (!found) {
            throw new Error("no invokestatic found in default method body");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f +": " + e);
    }
}
 
Example 18
Source File: BridgeHarness.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * return a string representation of a bytecode method
 */
static String descriptor(Method m, ConstantPool cp) throws ConstantPoolException {
    return m.getName(cp) + m.descriptor.getValue(cp);
}
 
Example 19
Source File: BridgeHarness.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * return a string representation of a bytecode method
 */
static String descriptor(Method m, ConstantPool cp) throws ConstantPoolException {
    return m.getName(cp) + m.descriptor.getValue(cp);
}
 
Example 20
Source File: TestDefaultBody.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
void verifyDefaultBody(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        Method testMethod = null;
        Code_attribute codeAttr = null;
        for (Method m : cf.methods) {
            codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
            String mname = m.getName(cf.constant_pool);
            if (mname.equals(TEST_METHOD_NAME)) {
                testMethod = m;
                break;
            } else {
                codeAttr = null;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        if (testMethod.access_flags.is(AccessFlags.ACC_ABSTRACT)) {
            throw new Error("Test method is abstract");
        }
        if (codeAttr == null) {
            throw new Error("Code attribute in test method not found");
        }

        boolean found = false;
        for (Instruction instr : codeAttr.getInstructions()) {
            if (instr.getOpcode() == Opcode.INVOKESTATIC) {
                found = true;
                int pc_index = instr.getShort(1);
                CONSTANT_Methodref_info mref = (CONSTANT_Methodref_info)cf.constant_pool.get(pc_index);
                String className = mref.getClassName();
                String targetName = mref.getNameAndTypeInfo().getName();
                String targetType = mref.getNameAndTypeInfo().getType();

                if (!className.equals(TARGET_CLASS_NAME)) {
                    throw new Error("unexpected class in default method body " + className);
                }
                if (!targetName.equals(TARGET_NAME)) {
                    throw new Error("unexpected method name in default method body " + targetName);
                }
                if (!targetType.equals(TARGET_TYPE)) {
                    throw new Error("unexpected method type in default method body " + targetType);
                }
                break;
            }
        }

        if (!found) {
            throw new Error("no invokestatic found in default method body");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f +": " + e);
    }
}