Search in sources :

Example 1 with Descriptor

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

the class LocalVariableTableWriter method writeLocalVariables.

public void writeLocalVariables(int pc, NoteKind kind) {
    ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
    // get from Options?
    String indent = space(2);
    List<LocalVariableTable_attribute.Entry> entries = pcMap.get(pc);
    if (entries != null) {
        for (ListIterator<LocalVariableTable_attribute.Entry> iter = entries.listIterator(kind == NoteKind.END ? entries.size() : 0); kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext(); ) {
            LocalVariableTable_attribute.Entry entry = kind == NoteKind.END ? iter.previous() : iter.next();
            if (kind.match(entry, pc)) {
                print(indent);
                print(kind.text);
                print(" local ");
                print(entry.index);
                print(" // ");
                Descriptor d = new Descriptor(entry.descriptor_index);
                try {
                    print(d.getFieldType(constant_pool));
                } catch (InvalidDescriptor e) {
                    print(report(e));
                } catch (ConstantPoolException e) {
                    print(report(e));
                }
                print(" ");
                try {
                    print(constant_pool.getUTF8Value(entry.name_index));
                } catch (ConstantPoolException e) {
                    print(report(e));
                }
                println();
            }
        }
    }
}
Also used : ConstantPool(com.sun.tools.classfile.ConstantPool) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor) Descriptor(com.sun.tools.classfile.Descriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor) LocalVariableTable_attribute(com.sun.tools.classfile.LocalVariableTable_attribute)

Example 2 with Descriptor

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

the class AnnotationWriter method writeDescriptor.

private void writeDescriptor(int index, boolean resolveIndices) {
    if (resolveIndices) {
        try {
            ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
            Descriptor d = new Descriptor(index);
            print(d.getFieldType(constant_pool));
            return;
        } catch (ConstantPoolException ignore) {
        } catch (InvalidDescriptor ignore) {
        }
    }
    print("#" + index);
}
Also used : ConstantPool(com.sun.tools.classfile.ConstantPool) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) Descriptor(com.sun.tools.classfile.Descriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor)

Example 3 with Descriptor

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

the class ClassWriter method writeMethod.

protected void writeMethod(Method m) {
    if (!options.checkAccess(m.access_flags))
        return;
    method = m;
    AccessFlags flags = m.access_flags;
    Descriptor d;
    Type.MethodType methodType;
    List<? extends Type> methodExceptions;
    Signature_attribute sigAttr = getSignature(m.attributes);
    if (sigAttr == null) {
        d = m.descriptor;
        methodType = null;
        methodExceptions = null;
    } else {
        Signature methodSig = sigAttr.getParsedSignature();
        d = methodSig;
        try {
            methodType = (Type.MethodType) methodSig.getType(constant_pool);
            methodExceptions = methodType.throwsTypes;
            if (methodExceptions != null && methodExceptions.isEmpty())
                methodExceptions = null;
        } catch (ConstantPoolException e) {
            // report error?
            // fall back on standard descriptor
            methodType = null;
            methodExceptions = null;
        }
    }
    writeModifiers(flags.getMethodModifiers());
    if (methodType != null) {
        writeListIfNotEmpty("<", methodType.typeParamTypes, "> ");
    }
    if (getName(m).equals("<init>")) {
        print(getJavaName(classFile));
        print(getJavaParameterTypes(d, flags));
    } else if (getName(m).equals("<clinit>")) {
        print("{}");
    } else {
        print(getJavaReturnType(d));
        print(" ");
        print(getName(m));
        print(getJavaParameterTypes(d, flags));
    }
    Attribute e_attr = m.attributes.get(Attribute.Exceptions);
    if (e_attr != null) {
        // if there are generic exceptions, there must be erased exceptions
        if (e_attr instanceof Exceptions_attribute) {
            Exceptions_attribute exceptions = (Exceptions_attribute) e_attr;
            print(" throws ");
            if (methodExceptions != null) {
                // use generic list if available
                writeList("", methodExceptions, "");
            } else {
                for (int i = 0; i < exceptions.number_of_exceptions; i++) {
                    if (i > 0)
                        print(", ");
                    print(getJavaException(exceptions, i));
                }
            }
        } else {
            report("Unexpected or invalid value for Exceptions attribute");
        }
    }
    println(";");
    indent(+1);
    if (options.showInternalSignatures) {
        println("Signature: " + getValue(m.descriptor));
    }
    if (options.verbose && !options.compat) {
        writeList("flags: ", flags.getMethodFlags(), NEWLINE);
    }
    Code_attribute code = null;
    Attribute c_attr = m.attributes.get(Attribute.Code);
    if (c_attr != null) {
        if (c_attr instanceof Code_attribute)
            code = (Code_attribute) c_attr;
        else
            report("Unexpected or invalid value for Code attribute");
    }
    if (options.showDisassembled && !options.showAllAttrs) {
        if (code != null) {
            println("Code:");
            codeWriter.writeInstrs(code);
            codeWriter.writeExceptionTable(code);
        }
    }
    if (options.showLineAndLocalVariableTables) {
        if (code != null) {
            attrWriter.write(code, code.attributes.get(Attribute.LineNumberTable), constant_pool);
            attrWriter.write(code, code.attributes.get(Attribute.LocalVariableTable), constant_pool);
        }
    }
    if (options.showAllAttrs) {
        Attribute[] attrs = m.attributes.attrs;
        for (Attribute attr : attrs) attrWriter.write(m, attr, constant_pool);
    }
    indent(-1);
    // set pendingNewline to write a newline before the next method (if any)
    // if a separator is desired
    setPendingNewline(options.showDisassembled || options.showAllAttrs || options.showInternalSignatures || options.showLineAndLocalVariableTables || options.verbose);
}
Also used : MethodType(com.sun.tools.classfile.Type.MethodType) Attribute(com.sun.tools.classfile.Attribute) Signature_attribute(com.sun.tools.classfile.Signature_attribute) Code_attribute(com.sun.tools.classfile.Code_attribute) Exceptions_attribute(com.sun.tools.classfile.Exceptions_attribute) Type(com.sun.tools.classfile.Type) ArrayType(com.sun.tools.classfile.Type.ArrayType) ClassType(com.sun.tools.classfile.Type.ClassType) MethodType(com.sun.tools.classfile.Type.MethodType) WildcardType(com.sun.tools.classfile.Type.WildcardType) TypeParamType(com.sun.tools.classfile.Type.TypeParamType) SimpleType(com.sun.tools.classfile.Type.SimpleType) ClassSigType(com.sun.tools.classfile.Type.ClassSigType) Signature(com.sun.tools.classfile.Signature) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) Descriptor(com.sun.tools.classfile.Descriptor) AccessFlags(com.sun.tools.classfile.AccessFlags)

Example 4 with Descriptor

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

the class LocalVariableTypeTableWriter method writeLocalVariables.

public void writeLocalVariables(int pc, NoteKind kind) {
    ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
    // get from Options?
    String indent = space(2);
    List<LocalVariableTypeTable_attribute.Entry> entries = pcMap.get(pc);
    if (entries != null) {
        for (ListIterator<LocalVariableTypeTable_attribute.Entry> iter = entries.listIterator(kind == NoteKind.END ? entries.size() : 0); kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext(); ) {
            LocalVariableTypeTable_attribute.Entry entry = kind == NoteKind.END ? iter.previous() : iter.next();
            if (kind.match(entry, pc)) {
                print(indent);
                print(kind.text);
                print(" generic local ");
                print(entry.index);
                print(" // ");
                Descriptor d = new Signature(entry.signature_index);
                try {
                    print(d.getFieldType(constant_pool).toString().replace("/", "."));
                } catch (InvalidDescriptor e) {
                    print(report(e));
                } catch (ConstantPoolException e) {
                    print(report(e));
                }
                print(" ");
                try {
                    print(constant_pool.getUTF8Value(entry.name_index));
                } catch (ConstantPoolException e) {
                    print(report(e));
                }
                println();
            }
        }
    }
}
Also used : ConstantPool(com.sun.tools.classfile.ConstantPool) Signature(com.sun.tools.classfile.Signature) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) LocalVariableTypeTable_attribute(com.sun.tools.classfile.LocalVariableTypeTable_attribute) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor) Descriptor(com.sun.tools.classfile.Descriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor)

Example 5 with Descriptor

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

the class StackMapWriter method setStackMap.

void setStackMap(StackMapTable_attribute attr) {
    if (attr == null) {
        map = null;
        return;
    }
    Method m = classWriter.getMethod();
    Descriptor d = m.descriptor;
    String[] args;
    try {
        ConstantPool cp = classWriter.getClassFile().constant_pool;
        String argString = d.getParameterTypes(cp);
        args = argString.substring(1, argString.length() - 1).split("[, ]+");
    } catch (ConstantPoolException e) {
        return;
    } catch (InvalidDescriptor e) {
        return;
    }
    boolean isStatic = m.access_flags.is(AccessFlags.ACC_STATIC);
    verification_type_info[] initialLocals = new verification_type_info[(isStatic ? 0 : 1) + args.length];
    if (!isStatic)
        initialLocals[0] = new CustomVerificationTypeInfo("this");
    for (int i = 0; i < args.length; i++) {
        initialLocals[(isStatic ? 0 : 1) + i] = new CustomVerificationTypeInfo(args[i].replace(".", "/"));
    }
    map = new HashMap<Integer, StackMap>();
    StackMapBuilder builder = new StackMapBuilder();
    // using -1 as the pc for the initial frame effectively compensates for
    // the difference in behavior for the first stack map frame (where the
    // pc offset is just offset_delta) compared to subsequent frames (where
    // the pc offset is always offset_delta+1).
    int pc = -1;
    map.put(pc, new StackMap(initialLocals, empty));
    for (int i = 0; i < attr.entries.length; i++) pc = attr.entries[i].accept(builder, pc);
}
Also used : Method(com.sun.tools.classfile.Method) StackMapTable_attribute.verification_type_info(com.sun.tools.classfile.StackMapTable_attribute.verification_type_info) ConstantPool(com.sun.tools.classfile.ConstantPool) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor) Descriptor(com.sun.tools.classfile.Descriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor)

Aggregations

ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)5 Descriptor (com.sun.tools.classfile.Descriptor)5 ConstantPool (com.sun.tools.classfile.ConstantPool)4 InvalidDescriptor (com.sun.tools.classfile.Descriptor.InvalidDescriptor)4 Signature (com.sun.tools.classfile.Signature)2 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attribute (com.sun.tools.classfile.Attribute)1 Code_attribute (com.sun.tools.classfile.Code_attribute)1 Exceptions_attribute (com.sun.tools.classfile.Exceptions_attribute)1 LocalVariableTable_attribute (com.sun.tools.classfile.LocalVariableTable_attribute)1 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)1 Method (com.sun.tools.classfile.Method)1 Signature_attribute (com.sun.tools.classfile.Signature_attribute)1 StackMapTable_attribute.verification_type_info (com.sun.tools.classfile.StackMapTable_attribute.verification_type_info)1 Type (com.sun.tools.classfile.Type)1 ArrayType (com.sun.tools.classfile.Type.ArrayType)1 ClassSigType (com.sun.tools.classfile.Type.ClassSigType)1 ClassType (com.sun.tools.classfile.Type.ClassType)1 MethodType (com.sun.tools.classfile.Type.MethodType)1 SimpleType (com.sun.tools.classfile.Type.SimpleType)1