ProvidenceHelper { @SuppressWarnings("unchecked") @Nonnull public static <T> Optional<T> optionalInMessage(PMessage message, PField... fields) { if (fields == null || fields.length == 0) { throw new IllegalArgumentException("No fields arguments."); } PField field = fields[0]; if (!message.has(field.getId())) { return Optional.empty(); } if (fields.length > 1) { if (field.getType() != PType.MESSAGE) { throw new IllegalArgumentException("Field " + field.getName() + " is not a message."); } return optionalInMessage((PMessage) message.get(field), Arrays.copyOfRange(fields, 1, fields.length)); } else { return Optional.of((T) message.get(field.getId())); } } private  ProvidenceHelper(); @SuppressWarnings("unchecked") static B getTargetModifications(M source, M target); @Nonnull static Message fromJsonResource(String path,
                             PMessageDescriptor<Message, Field> descriptor); @Nonnull static ArrayList<Message> arrayListFromJsonResource(String path,
                                                 PMessageDescriptor<Message, Field> descriptor); @Nonnull static Message fromResource(String resource,
                         PMessageDescriptor<Message, Field> descriptor,
                         Serializer serializer); @Nonnull static ArrayList<Message> arrayListFromResource(String path,
                                             PMessageDescriptor<Message, Field> descriptor,
                                             Serializer serializer); @Nonnull static String debugString(Message message); @Nonnull static Message parseDebugString(String string, PMessageDescriptor<Message, Field> descriptor); @SuppressWarnings("unchecked") @Nonnull static Optional<T> optionalInMessage(PMessage message, PField... fields);  }
@Test public void testOptionalInMessage() { DefaultFields defaultFields = DefaultFields .builder() .setCompactValue(CompactFields.builder() .setId(1234) .setLabel("bar") .build()) .build(); String label = ProvidenceHelper .<String>optionalInMessage(defaultFields, DefaultFields._Field.COMPACT_VALUE, CompactFields._Field.LABEL).orElseThrow(() -> new AssertionError( "No label")); assertThat(label, is("bar")); }