Home > Enterprise >  How to search for null values inside a java object?
How to search for null values inside a java object?

Time:07-06

I'm writing a JUnit test to assert that my algorithm's output object does not present any null value or empty strings.

For simplicity imagine 3 classes : Parent, Child, Car, where Parent is the object that I have to validate.

@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Builder
public class Child {
    String name;
    int age;
}

@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Builder
public class Car {
    String brand;
    String model;
}

@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Builder
public class Parent {
    String name;
    int age;
    List<Child> children;
    Car car;
}

what is the best and easy way to search for null values or empty strings?

I'm currently using the following method as a validator, checking field by field for null values and empty strings.

private boolean isValid(Parent parent) {
    if(parent == null) return false;

    boolean isObjectNull = Stream.of(parent.getName(), parent.getChildren(), parent.getCar()).anyMatch(Objects::isNull);

    if(isObjectNull) return false;

    isObjectNull = Stream.of(parent.getCar().getBrand(), parent.getCar().getModel()).anyMatch(Objects::isNull);

    if(isObjectNull) return false;
    
    for(Child child : parent.getChildren()){
        isObjectNull = Stream.of(child.getName()).anyMatch(Objects::isNull);
        
        if(isObjectNull) return false;

        if(!isValidString(child.getName())) return false;
    }
    
    return isValidString(parent.getName(), parent.getCar().getBrand(), parent.getCar().getModel());
}

private boolean isValidString(String... values){
    for(String s : values){
        if(s.isEmpty())
    }
}

But I would love something I can also use for other objects I will create in the future.

CodePudding user response:

You can use reflection to obtain all the getters from your objects that do return a reference (instead of a primitive). Then iterate over that list (or array, your choice) and execute them; when the return value for any of these is null, return false or throw an appropriate exception.

A little bit like this:

public final void validateNonNull( final Object candidate ) throws ValidationException
{
  if( isNull( candidate ) throw new ValidationException( "candidate is null" );

  final var candidateClass = candidate.getClass();
  final List<Method> getters = Arrays.stream( candidateClass.getMethods() ) // getters are always public!
    .filter( m -> !m.getName().equals( "getClass" ) )
    .filter( m -> m.getName().startsWith( "get" ) )
    .filter( m -> m.getParameterCount() == 0 )
    .filter( m -> !m.getReturnType().isPrimitive() )
    .collect( Collectors.toList() );

  for( var method : methods )
  {
    if( isNull( method.invoke( candidate ) ) throw new ValidationException( "candidate.%s() returned null".formatted( method.getName() ) );
  }
}

ValidationException is a custom exception, and you need to declare the checked exceptions that are declared for Method::invoke.

To check for empty Strings, too, change the for loop like this:

…
for( var method : methods )
{
  var retValue = method.invoke( candidate );
  if( retValue instanceof String aString && aString.isEmpty() ) throw new ValidationException( "candidate.%s() returned the empty String".formatted( method.getName() ) ); 
  if( isNull( retValue ) throw new ValidationException( "candidate.%s() returned null".formatted( method.getName() ) );
}

CodePudding user response:

This is the method that I created thanks to @tquadrat

/*
 * Validate output object recursively
 * - Object != null
 * - String not empty
 * - Validate inner object
 * - Validate collection
 *
 */
public final void validateOutputRecursively(final Object output) throws TestException {
    if (output == null) {
        throw new TestException("output is null");
    }

    final var outputClass = output.getClass();

    // null Object handler
    final List<Method> gettersAllObject = Arrays.stream(outputClass.getMethods())
        .filter(m -> !m.getName().equals("getClass"))
        .filter(m -> m.getName().startsWith("get"))
        .filter(m -> m.getParameterCount() == 0)
        .filter(m -> !m.getReturnType().isPrimitive())
        .collect(Collectors.toList());

    for (Method method : gettersAllObject) {
        Object value;
        try {
            value = method.invoke(output);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new TestException(e);
        }
        if (value == null) {
            throw new TestException("output."   method.getName()   "() returned null");
        }
    }

    // Object validation (not String, not Collection, just Object with field)
    final List<Method> gettersObject = Arrays.stream(outputClass.getMethods())
        .filter(m -> !m.getName().equals("getClass"))
        .filter(m -> m.getName().startsWith("get"))
        .filter(m -> m.getParameterCount() == 0)
        .filter(m -> !m.getReturnType().isPrimitive())
        .filter(m -> m.getReturnType() != String.class)
        .filter(m -> !Collection.class.isAssignableFrom(m.getReturnType()))
        .collect(Collectors.toList());

    for (Method method : gettersObject) {
        Object value;
        try {
            value = method.invoke(output);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new TestException(e);
        }

        validateOutputRecursively(value);
    }

    // String validation
    final List<Method> gettersString = Arrays.stream(outputClass.getMethods())
        .filter(m -> !m.getName().equals("getClass"))
        .filter(m -> m.getName().startsWith("get"))
        .filter(m -> m.getParameterCount() == 0)
        .filter(m -> m.getReturnType() == String.class)
        .collect(Collectors.toList());

    for (Method method : gettersString) {
        Object value;
        try {
            value = method.invoke(output);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new TestException(e);
        }
        if (((String) value).isEmpty()) {
            throw new TestException("output."   method.getName()   "() returned empty String");
        }
    }

    // Collection validation
    final List<Method> gettersCollection = Arrays.stream(outputClass.getMethods())
        .filter(m -> !m.getName().equals("getClass"))
        .filter(m -> m.getName().startsWith("get"))
        .filter(m -> m.getParameterCount() == 0)
        .filter(m -> Collection.class.isAssignableFrom(m.getReturnType()))
        .collect(Collectors.toList());

    for (Method method : gettersCollection) {
        Object list;
        try {
            list = method.invoke(output);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new TestException(e);
        }

        for (Object obj : (Collection<?>) list) {
            validateOutputRecursively(obj);
        }
    }
}
  • Related