public Method getMethod()

in src/main/java/org/apache/bcel/generic/MethodGen.java [796:877]


    public Method getMethod() {
        final String signature = getSignature();
        final ConstantPoolGen cp = super.getConstantPool();
        final int nameIndex = cp.addUtf8(super.getName());
        final int signatureIndex = cp.addUtf8(signature);
        /*
         * Also updates positions of instructions, i.e., their indices
         */
        final byte[] byteCode = il != null ? il.getByteCode() : null;
        LineNumberTable lnt = null;
        LocalVariableTable lvt = null;
        /*
         * Create LocalVariableTable and LineNumberTable attributes (for debuggers, for example)
         */
        if (!variableList.isEmpty() && !stripAttributes) {
            updateLocalVariableTable(getLocalVariableTable(cp));
            addCodeAttribute(lvt = getLocalVariableTable(cp));
        }
        if (localVariableTypeTable != null) {
            // LocalVariable length in LocalVariableTypeTable is not updated automatically. It's a difference with
            // LocalVariableTable.
            if (lvt != null) {
                adjustLocalVariableTypeTable(lvt);
            }
            addCodeAttribute(localVariableTypeTable);
        }
        if (!lineNumberList.isEmpty() && !stripAttributes) {
            addCodeAttribute(lnt = getLineNumberTable(cp));
        }
        final Attribute[] codeAttrs = getCodeAttributes();
        /*
         * Each attribute causes 6 additional header bytes
         */
        int attrsLen = 0;
        for (final Attribute codeAttr : codeAttrs) {
            attrsLen += codeAttr.getLength() + 6;
        }
        final CodeException[] cExc = getCodeExceptions();
        final int excLen = cExc.length * 8; // Every entry takes 8 bytes
        Code code = null;
        if (byteCode != null && !isAbstract() && !isNative()) {
            // Remove any stale code attribute
            final Attribute[] attributes = getAttributes();
            for (final Attribute a : attributes) {
                if (a instanceof Code) {
                    removeAttribute(a);
                }
            }
            code = new Code(cp.addUtf8("Code"), 8 + byteCode.length + // prologue byte code
                2 + excLen + // exceptions
                2 + attrsLen, // attributes
                maxStack, maxLocals, byteCode, cExc, codeAttrs, cp.getConstantPool());
            addAttribute(code);
        }
        final Attribute[] annotations = addRuntimeAnnotationsAsAttribute(cp);
        final Attribute[] parameterAnnotations = addRuntimeParameterAnnotationsAsAttribute(cp);
        ExceptionTable et = null;
        if (!throwsList.isEmpty()) {
            addAttribute(et = getExceptionTable(cp));
            // Add 'Exceptions' if there are "throws" clauses
        }
        final Method m = new Method(super.getAccessFlags(), nameIndex, signatureIndex, getAttributes(), cp.getConstantPool());
        // Undo effects of adding attributes
        if (lvt != null) {
            removeCodeAttribute(lvt);
        }
        if (localVariableTypeTable != null) {
            removeCodeAttribute(localVariableTypeTable);
        }
        if (lnt != null) {
            removeCodeAttribute(lnt);
        }
        if (code != null) {
            removeAttribute(code);
        }
        if (et != null) {
            removeAttribute(et);
        }
        removeRuntimeAttributes(annotations);
        removeRuntimeAttributes(parameterAnnotations);
        return m;
    }