@Override public Serializable apply(Description description) { // @formatter:off return tryToReadFieldValue(Description.class, fieldName, description) .andThenTry(Serializable.class::cast) .ifFailure(cause -> logger.warn(cause, () -> format("Could not read unique ID for Description; using display name instead: %s", description))) .toOptional() .orElseGet(description::getDisplayName); // @formatter:on }
/** * Register extensions in the supplied registry from fields in the supplied * class that are annotated with {@link RegisterExtension @RegisterExtension}. * * <p>The extensions will be sorted according to {@link Order @Order} semantics * prior to registration. * * @param registry the registry in which to register the extensions; never {@code null} * @param clazz the class or interface in which to find the fields; never {@code null} * @param instance the instance of the supplied class; may be {@code null} * when searching for {@code static} fields in the class */ static void registerExtensionsFromFields(ExtensionRegistry registry, Class<?> clazz, Object instance) { Preconditions.notNull(clazz, "Class must not be null"); Preconditions.notNull(registry, "ExtensionRegistry must not be null"); Predicate<Field> predicate = (instance == null) ? isStaticExtension : isNonStaticExtension; // Ensure that the list is modifiable, since findAnnotatedFields() returns an unmodifiable list. List<Field> fields = new ArrayList<>(findAnnotatedFields(clazz, RegisterExtension.class, predicate)); // Sort fields based on @Order. fields.sort(orderComparator); fields.forEach(field -> { tryToReadFieldValue(field, instance).ifSuccess(value -> { Preconditions.notNull(value, () -> String.format( "Failed to register extension via @RegisterExtension field [%s]: field must not be null when evaluated.", field)); Extension extension = (Extension) value; registry.registerExtension(extension, field); }); }); }