From 078d90ed3693279422b470f3c7c45f0415fa1814 Mon Sep 17 00:00:00 2001 From: Brady James Garvin <bgarvin@cse.unl.edu> Date: Thu, 23 Feb 2017 08:38:12 -0600 Subject: [PATCH] Fixed simulated duck typing in App. --- .../cse/soft160/chart_inheritance/App.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/chart_inheritance/src/main/java/edu/unl/cse/soft160/chart_inheritance/App.java b/chart_inheritance/src/main/java/edu/unl/cse/soft160/chart_inheritance/App.java index 087b0a2..0f4d2e3 100644 --- a/chart_inheritance/src/main/java/edu/unl/cse/soft160/chart_inheritance/App.java +++ b/chart_inheritance/src/main/java/edu/unl/cse/soft160/chart_inheritance/App.java @@ -46,14 +46,17 @@ public class App extends JFrame { context.setFont(TEXT_FONT); context.setColor(TEXT_COLOR); context.setColor(Color.BLUE); - try { - chart.getClass().getDeclaredMethod("draw", Graphics2D.class).invoke(chart, context); - } catch (NoSuchMethodException exception) { - throw new RuntimeException(exception); - } catch (InvocationTargetException exception) { - throw new RuntimeException(exception); - } catch (IllegalAccessException exception) { - throw new RuntimeException(exception); + for (Class<?> type = chart.getClass(); type != null; type = type.getSuperclass()) { + try { + type.getDeclaredMethod("draw", Graphics2D.class).invoke(chart, context); + break; + } catch (NoSuchMethodException exception) { + continue; + } catch (InvocationTargetException exception) { + throw new RuntimeException(exception); + } catch (IllegalAccessException exception) { + throw new RuntimeException(exception); + } } } -- GitLab