mercredi 4 décembre 2019

Java check if java native object don't have default value, or whether it is instantiated or not

I have check whether "Java native object don't have default value". Please note: I have to ignore the custom Object values, in case they aren't 'null'

/**
 * This method will check if given object is used by given testConfig file 1. if
 * instance of {@link String}, and it's value shouldn't be 'empty' or 'null'. 2.
 * if instance of {@link Integer}, and it's value shouldn't be '0'(zero). 3. if
 * instance of {@link Boolean}, and it's value shouldn't be 'false'. 4. if
 * instance of any non-null {@link Object}
 * 
 * @param method  tag name in the form of 'getter method'
 * @param objName given 'getter method' return type
 * @return list of used tags
 */
private List<String> isUsedTag(Method method, Object objName) {
    List<String> usedTags = new ArrayList<>();
    if (objName != null && (((objName instanceof Integer) && (TestngUtil.isNullorZero((Integer) objName)))
                    || ((objName instanceof String) && (!Util.isAnyNullOrEmpty((String) objName)) && !((String)objName).equalsIgnoreCase("0"))
                    || ((objName instanceof Boolean) && ((Boolean) objName))
                    || ((objName instanceof Object) && !(objName instanceof Boolean))
                    || ((objName instanceof List) && !Util.isEmpty((List)objName))
                    || ((objName.getClass().isPrimitive()) && (Integer.valueOf((int)objName) != 0))
            )) {
        usedTags.add(method.getName());
    } 
    return usedTags;
}

And checking whether the Integer object is '0' method is as

/**
 * Checks whether given 'Integer' Object is non-null/non-zero
 * @param i
 * @return true if given integer value is '0'
 */
public static boolean isNullorZero(Integer i){
    return 0 == ( i == null ? 0 : i);
}

Is there any better way I can do this?🤔





Aucun commentaire:

Enregistrer un commentaire