3434import java .lang .reflect .InvocationTargetException ;
3535import java .lang .reflect .Method ;
3636import java .util .Arrays ;
37+ import java .util .Collections ;
3738import java .util .List ;
3839import java .util .Map ;
3940import java .util .Queue ;
@@ -502,8 +503,16 @@ public Builder excludeMethod(String methodName) {
502503 * the predefined list of the default values per type will be used:
503504 * <ul>
504505 * <li>an empty string is used for the {@code String};
506+ * <li>the result of the {@link Collections#emptyList()} call for the types
507+ * derived from {@link List};</li>
508+ * <li>the result of the {@link Collections#emptySet()} call for the types
509+ * derived from {@link Set};</li>
510+ * <li>the result of the {@link Collections#emptyMap()} call for the types
511+ * derived from {@link Map};</li>
512+ * <li>the result of the {@link com.google.common.collect.Queues#newPriorityQueue()} call
513+ * for the types derived from {@link Queue};</li>
505514 * <li>the result of the {@link com.google.common.base.Defaults#defaultValue(Class)} call
506- * is for the primitives and related wrapper types. </li>
515+ * is for the primitives and related wrapper types; </li>
507516 * <li>the result of {@code getDefaultInstance} call for the types
508517 * derived from {@link Message}.</li>
509518 * </ul>
@@ -565,12 +574,11 @@ public NullToleranceTest build() {
565574 }
566575
567576 private void addDefaultTypeValues () {
568-
569577 final Customizer <String > stringCustomizer = new Customizer <>(STRING_DEFAULT_VALUE , defaultValues );
570- final Customizer <Queue > queueCustomizer = new Customizer (newPriorityQueue (), defaultValues );
571- final Customizer <Set > setCustomizer = new Customizer (emptySet (), defaultValues );
572- final Customizer <List > listCustomizer = new Customizer (emptyList (), defaultValues );
573- final Customizer <Map > mapCustomizer = new Customizer (emptyMap (), defaultValues );
578+ final Customizer <Queue > queueCustomizer = new Customizer <> (newPriorityQueue (), defaultValues );
579+ final Customizer <Set > setCustomizer = new Customizer <> (emptySet (), defaultValues );
580+ final Customizer <List > listCustomizer = new Customizer <> (emptyList (), defaultValues );
581+ final Customizer <Map > mapCustomizer = new Customizer <> (emptyMap (), defaultValues );
574582 final String defaultStringValue = stringCustomizer .getCustomizedValue (String .class );
575583 defaultValues .put (String .class , defaultStringValue );
576584 final Queue <?> defaultQueue = queueCustomizer .getCustomizedValue (Queue .class );
@@ -588,16 +596,18 @@ private static class Customizer<T> {
588596 private final T defaultValue ;
589597 private final Map <Class <?>, ?> defaultValues ;
590598
591- public Customizer (T defaultValue , Map <Class <?>, ?> defaultValues ) {
599+ < B extends T > Customizer (B defaultValue , Map <Class <?>, ?> defaultValues ) {
592600 this .defaultValue = defaultValue ;
593601 this .defaultValues = defaultValues ;
594602 }
595603
596- public T getCustomizedValue (Class <T > typeOfInterest ) {
604+ T getCustomizedValue (Class <T > typeOfInterest ) {
597605 for (Map .Entry <Class <?>, ?> entry : defaultValues .entrySet ()) {
598606 final boolean customValuePresent = typeOfInterest .isAssignableFrom (entry .getKey ());
599607 if (customValuePresent ) {
600- return (T ) entry .getValue ();
608+ @ SuppressWarnings ("unchecked" ) // It's OK, since we check for the type compliance above.
609+ final T result = (T ) entry .getValue ();
610+ return result ;
601611 }
602612 }
603613 return defaultValue ;
0 commit comments