]> source.dussan.org Git - vaadin-framework.git/commitdiff
Make getStateType() find getState methods in superclases (#9315)
authorLeif Åstrand <leif@vaadin.com>
Thu, 23 Aug 2012 07:46:17 +0000 (10:46 +0300)
committerLeif Åstrand <leif@vaadin.com>
Thu, 23 Aug 2012 07:46:17 +0000 (10:46 +0300)
Also cache the result as the finding logic could be quite expensive with
multiple exceptions thrown and caught.

server/src/com/vaadin/terminal/AbstractClientConnector.java

index 1025297a4f78d966090da1dab777c0e42ccfa3cc..53b3bb1c9d9663b11c10cc72c1b7b1fe1f8c4f44 100644 (file)
@@ -71,6 +71,8 @@ public abstract class AbstractClientConnector implements ClientConnector {
      */
     private SharedState sharedState;
 
+    private Class<? extends SharedState> 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<? extends SharedState> getStateType() {
+        // Lazy load because finding type can be expensive because of the
+        // exceptions flying around
+        if (stateType == null) {
+            stateType = findStateType();
+        }
+
+        return stateType;
+    }
+
+    private Class<? extends SharedState> 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);