简体   繁体   English

如何在 java object 中搜索 null 值?

[英]How to search for null values inside a java object?

I'm writing a JUnit test to assert that my algorithm's output object does not present any null value or empty strings.我正在编写一个 JUnit 测试来断言我的算法的 output object 不存在任何 null 值或空字符串。

For simplicity imagine 3 classes: Parent, Child, Car, where Parent is the object that I have to validate.为简单起见,假设有 3 个类:Parent、Child、Car,其中 Parent 是我必须验证的 object。

@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?搜索 null 值或空字符串的最佳和简便方法是什么?

I'm currently using the following method as a validator, checking field by field for null values and empty strings.我目前正在使用以下方法作为验证器,逐个字段检查 null 值和空字符串。

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.但我会喜欢一些我也可以用于我将来创建的其他对象的东西。

You can use reflection to obtain all the getters from your objects that do return a reference (instead of a primitive).您可以使用反射从您的对象中获取所有返回引用(而不是原语)的 getter。 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.当其中任何一个的返回值为null时,返回false或引发适当的异常。

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 . ValidationException是一个自定义异常,您需要声明为Method::invoke声明的已检查异常。

To check for empty Strings, too, change the for loop like this:要检查空字符串,也可以像这样更改for循环:

…
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() ) );
}

This is the method that I created after the answer of @tquadrat这是我在@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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM