protected Constructor getConstructor()

in hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/JavaDeserializer.java [129:172]


    protected Constructor<?> getConstructor(Class<?> cl) {
        Constructor<?>[] constructors = cl.getDeclaredConstructors();
        long bestCost = Long.MAX_VALUE;

        Constructor<?> constructor = null;

        for (int i = 0; i < constructors.length; i++) {
            Class<?>[] param = constructors[i].getParameterTypes();
            long cost = 0;

            for (int j = 0; j < param.length; j++) {
                cost = 4 * cost;

                if (Object.class.equals(param[j]))
                    cost += 1;
                else if (String.class.equals(param[j]))
                    cost += 2;
                else if (int.class.equals(param[j]))
                    cost += 3;
                else if (long.class.equals(param[j]))
                    cost += 4;
                else if (param[j].isPrimitive())
                    cost += 5;
                else
                    cost += 6;
            }

            if (cost < 0 || cost > (1 << 48))
                cost = 1 << 48;

            cost += (long) param.length << 48;

            if (cost < bestCost) {
                constructor = constructors[i];
                bestCost = cost;
            }
        }

        if (constructor != null) {
            constructor.setAccessible(true);
        }

        return constructor;
    }