You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2019/01/17 14:16:41 UTC

[myfaces-tobago] branch master updated: TOBAGO-1975: may be more robust against null pointers

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/master by this push:
     new 8fb1cad  TOBAGO-1975: <tc:selectItems> may be more robust against null pointers
8fb1cad is described below

commit 8fb1cad8a9206de19723a34f469028ae5f6546f3
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Thu Jan 17 13:29:23 2019 +0100

    TOBAGO-1975: <tc:selectItems> may be more robust against null pointers
---
 .../myfaces/tobago/internal/util/SelectItemUtils.java | 19 ++++++++++++++-----
 .../tobago/example/demo/SuggestController.java        |  7 +++++--
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/SelectItemUtils.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/SelectItemUtils.java
index 74a1b5e..89a4180 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/SelectItemUtils.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/SelectItemUtils.java
@@ -23,6 +23,8 @@ import org.apache.myfaces.tobago.component.Attributes;
 import org.apache.myfaces.tobago.component.Visual;
 import org.apache.myfaces.tobago.context.Markup;
 import org.apache.myfaces.tobago.util.ComponentUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.el.ValueExpression;
 import javax.faces.component.UIComponent;
@@ -30,6 +32,7 @@ import javax.faces.component.UISelectItem;
 import javax.faces.component.UISelectItems;
 import javax.faces.context.FacesContext;
 import javax.faces.model.SelectItem;
+import java.lang.invoke.MethodHandles;
 import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -44,6 +47,8 @@ import java.util.NoSuchElementException;
  */
 public class SelectItemUtils {
 
+  private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
   /**
    * Creates a list of SelectItems to use for rendering.
    */
@@ -232,17 +237,21 @@ public class SelectItemUtils {
 
           // Spec: When iterating over the select items, toString()
           // must be called on the string rendered attribute values
-          Object itemLabel = ComponentUtils.getAttribute(currentUISelectItems, Attributes.itemLabel);
-          if (itemLabel == null) {
+          final Object itemLabelObject = ComponentUtils.getAttribute(currentUISelectItems, Attributes.itemLabel);
+          final String itemLabel;
+          if (itemLabelObject != null) {
+            itemLabel = itemLabelObject.toString();
+          } else if (itemValue != null) {
             itemLabel = itemValue.toString();
           } else {
-            itemLabel = itemLabel.toString();
+            LOG.warn("Label string can't be created!");
+            itemLabel = "???";
           }
           Object itemDescription = ComponentUtils.getAttribute(currentUISelectItems, Attributes.itemDescription);
           if (itemDescription != null) {
             itemDescription = itemDescription.toString();
           }
-          final Boolean itemDisabled
+          final boolean itemDisabled
               = ComponentUtils.getBooleanAttribute(currentUISelectItems, Attributes.itemDisabled, false);
           final String itemImage = ComponentUtils.getStringAttribute(currentUISelectItems, Attributes.itemImage);
           final Markup markup;
@@ -256,7 +265,7 @@ public class SelectItemUtils {
 // TBD ?
 //        Object noSelectionValue = attributeMap.get(NO_SELECTION_VALUE_PROP);
           item = new org.apache.myfaces.tobago.model.SelectItem(
-              itemValue, (String) itemLabel, (String) itemDescription, itemDisabled, itemImage, markup);
+              itemValue, itemLabel, (String) itemDescription, itemDisabled, itemImage, markup);
 
           // remove the value with the key from var from the request map, if previously written
           if (wroteRequestMapVarValue) {
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SuggestController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SuggestController.java
index 4ebf896..c1f5459 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SuggestController.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SuggestController.java
@@ -62,11 +62,14 @@ public class SuggestController implements Serializable {
 
   public List<String> getSolarObjects() {
     final String substring = query != null ? query : "";
-    LOG.info("Creating items for substring: '" + substring + "'");
-    return solarObjects.stream().filter(s -> StringUtils.containsIgnoreCase(s, substring)).collect(Collectors.toList());
+    final List<String> filtered =
+        solarObjects.stream().filter(s -> StringUtils.containsIgnoreCase(s, substring)).collect(Collectors.toList());
+    LOG.info("Found {} items for substring: '{}'", filtered.size(), substring);
+    return filtered;
   }
 
   public List<String> getAllSolarObjects() {
+    LOG.info("Found all {} items", solarObjects.size());
     return solarObjects;
   }