private static int sizeOfParameters()

in rhino/src/main/java/org/mozilla/classfile/ClassFileWriter.java [2838:2939]


    private static int sizeOfParameters(String pString) {
        int length = pString.length();
        int rightParenthesis = pString.lastIndexOf(')');
        if (3 <= length /* minimal signature takes at least 3 chars: ()V */
                && pString.charAt(0) == '('
                && 1 <= rightParenthesis
                && rightParenthesis + 1 < length) {
            boolean ok = true;
            int index = 1;
            int stackDiff = 0;
            int count = 0;
            stringLoop:
            while (index != rightParenthesis) {
                switch (pString.charAt(index)) {
                    default:
                        ok = false;
                        break stringLoop;
                    case 'J':
                    case 'D':
                        --stackDiff;
                    // fall through
                    case 'B':
                    case 'S':
                    case 'C':
                    case 'I':
                    case 'Z':
                    case 'F':
                        --stackDiff;
                        ++count;
                        ++index;
                        continue;
                    case '[':
                        ++index;
                        int c = pString.charAt(index);
                        while (c == '[') {
                            ++index;
                            c = pString.charAt(index);
                        }
                        switch (c) {
                            default:
                                ok = false;
                                break stringLoop;
                            case 'J':
                            case 'D':
                            case 'B':
                            case 'S':
                            case 'C':
                            case 'I':
                            case 'Z':
                            case 'F':
                                --stackDiff;
                                ++count;
                                ++index;
                                continue;
                            case 'L':
                                // fall through
                        }
                    // fall through
                    case 'L':
                        {
                            --stackDiff;
                            ++count;
                            ++index;
                            int semicolon = pString.indexOf(';', index);
                            if (!(index + 1 <= semicolon && semicolon < rightParenthesis)) {
                                ok = false;
                                break stringLoop;
                            }
                            index = semicolon + 1;
                            continue;
                        }
                }
            }
            if (ok) {
                switch (pString.charAt(rightParenthesis + 1)) {
                    default:
                        ok = false;
                        break;
                    case 'J':
                    case 'D':
                        ++stackDiff;
                    // fall through
                    case 'B':
                    case 'S':
                    case 'C':
                    case 'I':
                    case 'Z':
                    case 'F':
                    case 'L':
                    case '[':
                        ++stackDiff;
                    // fall through
                    case 'V':
                        break;
                }
                if (ok) {
                    return ((count << 16) | (0xFFFF & stackDiff));
                }
            }
        }
        throw new IllegalArgumentException("Bad parameter signature: " + pString);
    }