All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.parisoft.resty.utils.ObjectUtils Maven / Gradle / Ivy

Go to download

A minimalist HTTP client made on top of Apache HttpComponets and Jackson Data Processor

There is a newer version: 1.0.6
Show newest version
package com.github.parisoft.resty.utils;

import static com.github.parisoft.resty.utils.ReflectionUtils.containsConstructor;
import static com.github.parisoft.resty.utils.ReflectionUtils.containsMethod;
import static com.github.parisoft.resty.utils.ReflectionUtils.getConstructor;
import static com.github.parisoft.resty.utils.ReflectionUtils.getMethod;

@SuppressWarnings("unchecked")
public class ObjectUtils {

    private static final String METHOD_VALUE_OF_STRING = "valueOf";

    public static boolean isInstanciableFromString(Class someClass) {
        return containsConstructor(someClass, String.class)
                || containsMethod(someClass, METHOD_VALUE_OF_STRING, String.class);
    }

    public static  T newInstanceFromString(Class someClass, String string) {
        if (containsConstructor(someClass, String.class)) {
            try {
                return getConstructor(someClass, String.class).newInstance(string);
            } catch (Exception ignored) {
            }
        }

        if (containsMethod(someClass, METHOD_VALUE_OF_STRING, String.class)) {
            try {
                return (T) getMethod(someClass, METHOD_VALUE_OF_STRING, String.class).invoke(null, string);
            } catch (Exception ignored) {
            }
        }

        return null;
    }

    public static boolean isPrimitive(Object object) {
        return object.getClass().isPrimitive();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy