2

I need to check if all value is null in a Scala class (Test.class) defined as:

class Test extends Serializable {
  a: String = _
  b: String = _
  c: String = _
}

I can't change this class (is legacy code in my project). How can i do this without a lot of if? The real class have 22 fields.

I've tried to use java reflection to detect the defined fields, but they are all defined every and I can't access it because the fields are private for the reflectors. (criteria.getClass.getDeclaredFields)

znndrd
  • 79
  • 1
  • 8

1 Answers1

1

Thanks @Dmytro Mitin , it works for me. I think that I will use this approach for check the not null element:

testClass.getClass.getDeclaredFields.flatMap(f => {
        f.setAccessible(true)
        Option(f.get(criteria))
    }).length 
znndrd
  • 79
  • 1
  • 8