From 8606feb8596ed8f6cd3ed41160706b692f6f4679 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 23 Aug 2012 10:46:17 +0300 Subject: [PATCH] Make getStateType() find getState methods in superclases (#9315) Also cache the result as the finding logic could be quite expensive with multiple exceptions thrown and caught. --- .../terminal/AbstractClientConnector.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/server/src/com/vaadin/terminal/AbstractClientConnector.java b/server/src/com/vaadin/terminal/AbstractClientConnector.java index 1025297a4f..53b3bb1c9d 100644 --- a/server/src/com/vaadin/terminal/AbstractClientConnector.java +++ b/server/src/com/vaadin/terminal/AbstractClientConnector.java @@ -71,6 +71,8 @@ public abstract class AbstractClientConnector implements ClientConnector { */ private SharedState sharedState; + private Class stateType; + /** * Pending RPC method invocations to be sent. */ @@ -179,26 +181,33 @@ public abstract class AbstractClientConnector implements ClientConnector { } } - /* - * (non-Javadoc) - * - * @see com.vaadin.terminal.gwt.server.ClientConnector#getStateType() - */ @Override public Class getStateType() { + // Lazy load because finding type can be expensive because of the + // exceptions flying around + if (stateType == null) { + stateType = findStateType(); + } + + return stateType; + } + + private Class findStateType() { try { - Method m = null; Class class1 = getClass(); - while (m == null && class1 != null) { - m = class1.getDeclaredMethod("getState", (Class[]) null); - class1 = class1.getSuperclass(); - } - if (m == null) { - throw new NoSuchMethodException(getClass().getCanonicalName() - + ".getState()"); + while (class1 != null) { + try { + Method m = class1.getDeclaredMethod("getState", + (Class[]) null); + Class type = m.getReturnType(); + return type.asSubclass(SharedState.class); + } catch (NoSuchMethodException nsme) { + // Try in superclass instead + class1 = class1.getSuperclass(); + } } - Class type = m.getReturnType(); - return type.asSubclass(SharedState.class); + throw new NoSuchMethodException(getClass().getCanonicalName() + + ".getState()"); } catch (Exception e) { throw new RuntimeException("Error finding state type for " + getClass().getName(), e); -- 2.39.5