Blob Blame History Raw
From 2b813829d02b89206493520eb86075a43b145db9 Mon Sep 17 00:00:00 2001
From: Roman Vais <rvais@redhat.com>
Date: Tue, 13 Jun 2017 13:24:24 +0200
Subject: [PATCH] ParseValues-NullPointerException-patch

---
 .../java/com/beust/jcommander/Parameterized.java   | 27 ++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/beust/jcommander/Parameterized.java b/src/main/java/com/beust/jcommander/Parameterized.java
index 3264008..e937a66 100644
--- a/src/main/java/com/beust/jcommander/Parameterized.java
+++ b/src/main/java/com/beust/jcommander/Parameterized.java
@@ -13,6 +13,8 @@ import java.lang.reflect.Type;
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Encapsulate a field or a method annotated with @Parameter or @DynamicParameter
@@ -180,12 +182,33 @@ public class Parameterized {
       String fieldName = Character.toLowerCase(name.charAt(3)) + name.substring(4);
       Object result = null;
       try {
-        Field field = method.getDeclaringClass().getDeclaredField(fieldName);
+        Field field = object.getClass().getDeclaredField(fieldName);
         if (field != null) {
           setFieldAccessible(field);
           result = field.get(object);
         }
-      } catch(NoSuchFieldException | IllegalAccessException ex) {
+      } catch(NoSuchFieldException ex) {
+        Class<?> clazz = object.getClass();
+        Field found = null;
+        while (clazz != null && method.getDeclaringClass().isAssignableFrom(clazz)) {
+          try {
+            found = clazz.getDeclaredField(fieldName);
+          } catch (NoSuchFieldException nfex) {
+            // ignore
+          }
+          if (found != null) break;
+          clazz = clazz.getSuperclass();
+        }
+        if (found != null) {
+          setFieldAccessible(found);
+          try {
+            result = found.get(object);
+          } catch (IllegalAccessException iex) {
+            // ignore
+          }
+        }
+
+      } catch(IllegalAccessException ex) {
         // ignore
       }
       return result;
-- 
2.7.4