private static void setStringValue()

in tika-serialization/src/main/java/org/apache/tika/serialization/TikaJsonDeserializer.java [264:346]


    private static void setStringValue(String name, String txt, Object obj, List<Method> setters) throws ReflectiveOperationException {

        //try for exact match first
        for (Method m : setters) {
            Class argClass = m.getParameters()[0].getType();
            if (argClass.equals(String.class)) {
                m.invoke(obj, txt);
                return;
            }
        }
        Method intMethod = null;
        Method longMethod = null;
        Method doubleMethod = null;
        Method floatMethod = null;
        Method shortMethod = null;
        Method boolMethod = null;
        for (Method m : setters) {
            Class argClass = m.getParameters()[0].getType();
            if (argClass.equals(Integer.class) || argClass.equals(int.class)) {
                intMethod = m;
            } else if (argClass.equals(Long.class) || argClass.equals(long.class)) {
                longMethod = m;
            } else if (argClass.equals(Float.class) || argClass.equals(float.class)) {
                floatMethod = m;
            } else if (argClass.equals(Double.class) || argClass.equals(double.class)) {
                doubleMethod = m;
            } else if (argClass.equals(Short.class) || argClass.equals(short.class)) {
                shortMethod = m;
            } else if (argClass.equals(Boolean.class) || argClass.equals(boolean.class)) {
                boolMethod = m;
            }
        }

        if (shortMethod != null) {
            try {
                short val = Short.parseShort(txt);
                shortMethod.invoke(obj, val);
                return;
            } catch (NumberFormatException e) {
                //swallow
            }
        } else if (intMethod != null) {
            try {
                int val = Integer.parseInt(txt);
                intMethod.invoke(obj, val);
                return;
            } catch (NumberFormatException e) {
                //swallow
            }
        } else if (floatMethod != null) {
            try {
                float val = Float.parseFloat(txt);
                floatMethod.invoke(obj, val);
                return;
            } catch (NumberFormatException e) {
                //swallow
            }
        } else if (longMethod != null) {
            try {
                long val = Long.parseLong(txt);
                longMethod.invoke(obj, val);
                return;
            } catch (NumberFormatException e) {
                //swallow
            }
        } else if (doubleMethod != null) {
            try {
                double val = Double.parseDouble(txt);
                doubleMethod.invoke(obj, val);
                return;
            } catch (NumberFormatException e) {
                //swallow
            }
        } else if (boolMethod != null) {
            if (txt.equalsIgnoreCase("true")) {
                boolMethod.invoke(obj, true);
            } else if (txt.equalsIgnoreCase("false")) {
                boolMethod.invoke(obj, false);
            }
        }
        throw new IllegalArgumentException("I regret I couldn't find a setter for: " + name);

    }