Search in sources :

Example 6 with Method

use of com.sun.tools.classfile.Method in project jdk8u_jdk by JetBrains.

the class AnnotationsElementVisitor method readMethods.

private void readMethods(ClassFile c, Element klass) throws IOException {
    int len = c.methods.length;
    Element methods = new Element(len);
    for (Method m : c.methods) {
        Element member = new Element("Method");
        member.setAttr("name", getCpString(m.name_index));
        member.setAttr("type", getCpString(m.descriptor.index));
        member.setAttr("flags", flagString(m.access_flags.flags, member));
        readAttributesFor(c, m.attributes, member);
        member.trimToSize();
        methods.add(member);
    }
    if (!keepOrder) {
        methods.sort();
    }
    klass.addAll(methods);
}
Also used : Element(xmlkit.XMLKit.Element) Method(com.sun.tools.classfile.Method)

Example 7 with Method

use of com.sun.tools.classfile.Method in project ceylon-compiler by ceylon.

the class FindNativeFiles method isNativeClass.

boolean isNativeClass(JarFile jar, JarEntry entry) throws IOException, ConstantPoolException {
    String name = entry.getName();
    if (name.startsWith("META-INF") || !name.endsWith(".class"))
        return false;
    //String className = name.substring(0, name.length() - 6).replace("/", ".");
    //System.err.println("check " + className);
    InputStream in = jar.getInputStream(entry);
    ClassFile cf = ClassFile.read(in);
    in.close();
    for (int i = 0; i < cf.methods.length; i++) {
        Method m = cf.methods[i];
        if (m.access_flags.is(AccessFlags.ACC_NATIVE)) {
            // System.err.println(className);
            return true;
        }
    }
    return false;
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) InputStream(java.io.InputStream) Method(com.sun.tools.classfile.Method)

Example 8 with Method

use of com.sun.tools.classfile.Method in project ceylon-compiler by ceylon.

the class T6199075 method verifyBytecode.

void verifyBytecode(VarargsMethod selected) {
    bytecodeCheckCount++;
    File compiledTest = new File("Test.class");
    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");
        }
        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokevirtual")) {
                int cp_entry = i.getUnsignedShort(1);
                CONSTANT_Methodref_info methRef = (CONSTANT_Methodref_info) cf.constant_pool.get(cp_entry);
                String type = methRef.getNameAndTypeInfo().getType();
                if (!type.contains(selected.varargsElement.bytecodeString)) {
                    throw new Error("Unexpected type method call: " + type);
                }
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest + ": " + e);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Code_attribute(com.sun.tools.classfile.Code_attribute) Method(com.sun.tools.classfile.Method) Instruction(com.sun.tools.classfile.Instruction) File(java.io.File) ClassFile(com.sun.tools.classfile.ClassFile)

Example 9 with Method

use of com.sun.tools.classfile.Method in project ceylon-compiler by ceylon.

the class T7042566 method verifyBytecode.

void verifyBytecode(VarargsMethod selected, JavaSource source) {
    bytecodeCheckCount++;
    File compiledTest = new File("Test.class");
    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");
        }
        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokevirtual")) {
                int cp_entry = i.getUnsignedShort(1);
                CONSTANT_Methodref_info methRef = (CONSTANT_Methodref_info) cf.constant_pool.get(cp_entry);
                String type = methRef.getNameAndTypeInfo().getType();
                String sig = selected.parameterTypes.bytecodeSigStr;
                if (!type.contains(sig)) {
                    throw new Error("Unexpected type method call: " + type + "" + "\nfound: " + sig + "\n" + source.getCharContent(true));
                }
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest + ": " + e);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Code_attribute(com.sun.tools.classfile.Code_attribute) Method(com.sun.tools.classfile.Method) Instruction(com.sun.tools.classfile.Instruction) File(java.io.File) ClassFile(com.sun.tools.classfile.ClassFile)

Example 10 with Method

use of com.sun.tools.classfile.Method in project ceylon-compiler by ceylon.

the class CodeWriter method writeVerboseHeader.

public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) {
    Method method = classWriter.getMethod();
    String argCount;
    try {
        int n = method.descriptor.getParameterCount(constant_pool);
        if (!method.access_flags.is(AccessFlags.ACC_STATIC))
            // for 'this'
            ++n;
        argCount = Integer.toString(n);
    } catch (ConstantPoolException e) {
        argCount = report(e);
    } catch (DescriptorException e) {
        argCount = report(e);
    }
    println("stack=" + attr.max_stack + ", locals=" + attr.max_locals + ", args_size=" + argCount);
}
Also used : DescriptorException(com.sun.tools.classfile.DescriptorException) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) Method(com.sun.tools.classfile.Method)

Aggregations

Method (com.sun.tools.classfile.Method)12 ClassFile (com.sun.tools.classfile.ClassFile)8 Code_attribute (com.sun.tools.classfile.Code_attribute)5 Instruction (com.sun.tools.classfile.Instruction)3 InputStream (java.io.InputStream)3 ConstantPool (com.sun.tools.classfile.ConstantPool)2 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)2 File (java.io.File)2 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attribute (com.sun.tools.classfile.Attribute)1 Attributes (com.sun.tools.classfile.Attributes)1 ClassWriter (com.sun.tools.classfile.ClassWriter)1 Exception_data (com.sun.tools.classfile.Code_attribute.Exception_data)1 Descriptor (com.sun.tools.classfile.Descriptor)1 InvalidDescriptor (com.sun.tools.classfile.Descriptor.InvalidDescriptor)1 DescriptorException (com.sun.tools.classfile.DescriptorException)1 Field (com.sun.tools.classfile.Field)1 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)1 StackMapTable_attribute.verification_type_info (com.sun.tools.classfile.StackMapTable_attribute.verification_type_info)1 DataInputStream (java.io.DataInputStream)1