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

com.github.bingoohuang.springrestclient.utils.Obj Maven / Gradle / Ivy

There is a newer version: 0.0.23
Show newest version
package com.github.bingoohuang.springrestclient.utils;

import lombok.experimental.UtilityClass;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

@UtilityClass
public class Obj {
    public Object getOrCreateBean(ApplicationContext appContext, Class requiredType) {
        try {
            return appContext.getBean(requiredType);
        } catch (NoSuchBeanDefinitionException e) {
            return createObject(requiredType).toString();
        }
    }

    public  T getBean(ApplicationContext appContext, Class beanClass) {
        try {
            return appContext.getBean(beanClass);
        } catch (BeansException e) {
            return null;
        }
    }

    public  T createObject(Class clazz, Object ctorArg) {
        try {
            Constructor constructor = clazz.getConstructor(ctorArg.getClass());
            return constructor.newInstance(ctorArg);
        } catch (NoSuchMethodException ex) {

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return createObject(clazz);
    }


    public  T createObject(Class clazz) {
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public void setField(Class clazz, Object object, String fieldName, Object fieldValue) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(object, fieldValue);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("unable to set field " + fieldName, e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("unable to set field " + fieldName, e);
        }
    }


    public  void ensureInterface(Class clazz) {
        if (clazz.isInterface()) return;

        throw new RuntimeException(clazz + " is not an interface");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy