Author: jdonnerstag
Date: Fri Jun 26 16:16:27 2009
New Revision: 788754

URL: http://svn.apache.org/viewvc?rev=788754&view=rev
Log:
backported the fix from 1.4
Issue: WICKET-2092

Modified:
    
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/session/DefaultPageFactory.java

Modified: 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/session/DefaultPageFactory.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/session/DefaultPageFactory.java?rev=788754&r1=788753&r2=788754&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/session/DefaultPageFactory.java
 (original)
+++ 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/session/DefaultPageFactory.java
 Fri Jun 26 16:16:27 2009
@@ -49,47 +49,79 @@
         */
        public final Page newPage(final Class pageClass)
        {
-               try
+               // Try default constructor if one exists
+               Constructor constructor = constructor(pageClass);
+               if (constructor != null)
                {
-                       // throw an exception in case default constructor is 
missing
-                       // => improved error message
-                       final Constructor constructor = 
pageClass.getConstructor((Class[]) null);
-
-                       return newPage(constructor, null);
+                       // return new Page()
+                       return createPage(constructor, null);
                }
-               catch (NoSuchMethodException e)
+
+               // Try to get constructor that takes PageParameters
+               constructor = constructor(pageClass, PageParameters.class);
+               if (constructor != null)
                {
-                       // a bit of a hack here..
-                       Constructor constructor = constructor(pageClass, 
PageParameters.class);
-                       if (constructor != null)
-                       {
-                               return newPage(constructor, new 
PageParameters());
-                       }
-                       else
-                       {
-                               throw new WicketRuntimeException("Unable to 
create page from " + pageClass +
-                                               ". Class does not have a 
default contructor", e);
-                       }
+                       // return new Page(parameters)
+                       return createPage(constructor, new PageParameters());
                }
+
+               throw new WicketRuntimeException("Unable to create page from " 
+ pageClass +
+                       ". Class does neither have a constructor with 
PageParameter nor a default constructor");
        }
 
        /**
         * @see IPageFactory#newPage(Class, PageParameters)
         */
-       public final Page newPage(final Class pageClass, final PageParameters 
parameters)
+       public final Page newPage(final Class pageClass, PageParameters 
parameters)
        {
-               // Try to get constructor that takes PageParameters
-               Constructor constructor = constructor(pageClass, 
PageParameters.class);
+               // If no parameters are provided, try the default constructor 
first, than the PageParameter
+               // constructor with empty parameter list.
+               if (parameters == null)
+               {
+                       return newPage(pageClass);
+               }
 
-               // If we got a PageParameters constructor
+               // If parameters not null, than try to get constructor that 
takes PageParameters
+               Constructor constructor = constructor(pageClass, 
PageParameters.class);
                if (constructor != null)
                {
                        // return new Page(parameters)
-                       return newPage(constructor, parameters);
+                       return createPage(constructor, parameters);
+               }
+
+               // No constructor with PageParameters found. Try default 
constructor.
+               constructor = constructor(pageClass);
+               if (constructor != null)
+               {
+                       // return new Page()
+                       return createPage(constructor, null);
+               }
+
+               throw new WicketRuntimeException("Unable to create page from " 
+ pageClass +
+                       ". Class does neither have a constructor with 
PageParameter nor a default constructor");
+       }
+
+       /**
+        * Looks up a one-arg Page constructor by class and argument type.
+        * 
+        * @param pageClass
+        *            The class of page
+        * @return The page constructor, or null if no one-arg constructor can 
be found taking the given
+        *         argument type.
+        */
+       private final Constructor constructor(final Class pageClass)
+       {
+               try
+               {
+                       // Try to find the constructor
+                       return pageClass.getConstructor(null);
+               }
+               catch (NoSuchMethodException e)
+               {
+                       // Ignore
                }
 
-               // Always try default constructor if one exists
-               return newPage(pageClass);
+               return null;
        }
 
        /**
@@ -129,6 +161,8 @@
 
        /**
         * Creates a new Page using the given constructor and argument.
+        * <p>
+        * Note that you can not create a Page with PageParameters and pass 
null.
         * 
         * @param constructor
         *            The constructor to invoke
@@ -139,14 +173,18 @@
         *             Thrown if the Page cannot be instantiated using the 
given constructor and
         *             argument.
         */
-       private final Page newPage(final Constructor constructor, final Object 
argument)
+       private final Page createPage(final Constructor constructor, final 
PageParameters argument)
        {
                try
                {
                        if (argument != null)
+                       {
                                return (Page)constructor.newInstance(new 
Object[] { argument });
+                       }
                        else
-                               return (Page)constructor.newInstance(new 
Object[] {});
+                       {
+                               return (Page)constructor.newInstance(null);
+                       }
                }
                catch (InstantiationException e)
                {
@@ -160,8 +198,8 @@
                {
                        // honor redirect exception contract defined in 
IPageFactory
                        if (e.getTargetException() instanceof AbortException ||
-                                       e.getTargetException() instanceof 
AuthorizationException ||
-                                       e.getTargetException() instanceof 
MarkupException)
+                               e.getTargetException() instanceof 
AuthorizationException ||
+                               e.getTargetException() instanceof 
MarkupException)
                        {
                                throw (RuntimeException)e.getTargetException();
                        }
@@ -169,12 +207,22 @@
                }
        }
 
-       private String createDescription(Constructor constructor, Object 
argument)
+       /**
+        * 
+        * @param constructor
+        * @param argument
+        * @return description
+        */
+       private String createDescription(Constructor constructor, 
PageParameters argument)
        {
                if (argument != null)
+               {
                        return "Can't instantiate page using constructor " + 
constructor + " and argument " +
-                                       argument;
+                               argument;
+               }
                else
+               {
                        return "Can't instantiate page using constructor " + 
constructor;
+               }
        }
 }
\ No newline at end of file


Reply via email to