[Java] Reflash all items in BeanClass

Common methods that were often used at work before ```java Caller.java { return (GetChangeAddressDialogInfoOutDto) CommonUtil.refreshServOutDto(serviceOutDto); }

â– CommonUtil.java /** *Reresh ServiceOutDto
* (String Type : Null -> Blank) * * @param bean * @return Object */ public static Object refreshServOutDto(Object bean) { if (bean != null) { return refreshServOutDto(bean, StringUtils.EMPTY); } return bean; }

/**
 *Reresh ServiceOutDto
 * 
 * @param bean
 * @param defaultStr
 * @return Object
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public static Object refreshServOutDto(Object bean, String defaultStr) {
    if (bean != null) {
        Map bMap = new BeanMap(bean);
        HashMap hMap = new HashMap(bMap);
        hMap.remove("class");
        Iterator itr = hMap.keySet().iterator();
        while (itr.hasNext()) {
            String propNm = (String) itr.next();
            reSetter(bean, propNm, defaultStr);
        }
    }
    return bean;
}

/**
 *Convert Null to blank
 * 
 * @param bean
 * @param propNm
 * @param defaultStr
 */
@SuppressWarnings("rawtypes")
public static <T> void reSetter(Object bean, String propNm, String defaultStr) {
    //Prepare a descriptor
    PropertyDescriptor nameProp;
    try {
        nameProp = new PropertyDescriptor(propNm, bean.getClass());
        //Getter method acquisition
        Method nameGetter = nameProp.getReadMethod();
        Object nowVal = nameGetter.invoke(bean, (Object[]) null);

        //Get setter method
        Method nameSetter = nameProp.getWriteMethod();
        String PropType = nameProp.getPropertyType().toString();
        if (PropType.endsWith("java.util.List")) {
            //Array
            if (nowVal == null) {
                nameSetter.invoke(bean, (List) new ArrayList());
            }
        } else if (PropType.endsWith("java.lang.String")) {
            //String
            String tmpStr = (String) nowVal;
            tmpStr = StringUtils.defaultIfEmpty(((String) nowVal), defaultStr);
            // "null"/"NULL"/"Null"If, convert
            if (Objects.equal(tmpStr.toLowerCase(), "null")) {
                tmpStr = defaultStr;
            }
            nameSetter.invoke(bean, tmpStr);
        } else if (PropType.endsWith("int")) {
            // int

        } else if (PropType.endsWith("java.lang.Number")) {
            // Number

        } else if (PropType.endsWith("boolean")) {
            // boolean

        } else if (PropType.endsWith("java.util.Date")) {
            // Date

        } else if (PropType.matches(".*com\\.smbc_card\\.credit\\.compass\\.service\\.dto\\..+")) {
            // Java Bean Class (ex.Address etc.)
            if (nowVal == null) {
                nameSetter.invoke(    bean,
                                    refreshServOutDto(getClassForName(
                                                                        PropType.replaceFirst(    "class ",
                                                                                                StringUtils.EMPTY))
                                        .newInstance()));
            }
        }
    } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
        | InstantiationException | ClassNotFoundException e) {
        System.out.println("Abnormal information:" + e);
    }
}

/**
 * getClassForName
 * 
 * @param className
 * @return Class<T>
 * @throws ClassNotFoundException
 */
@SuppressWarnings("unchecked")
public static <T> Class<T> getClassForName(String className) throws ClassNotFoundException {
    return (Class<T>) Class.forName(className);
}


Recommended Posts

[Java] Reflash all items in BeanClass
All same hash code string in Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Boyer-Moore implementation in Java
Hello World in Java
Get Locale objects for all locales available in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
All about Java programming
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! â‘ 
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Write flyway callbacks in Java
Topic Analysis (LDA) in Java
Importing Excel data in Java 2
NEologd preprocessing in Java neologdn-java