You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wicket.apache.org by Martin Grigorov <mg...@apache.org> on 2012/11/25 15:27:31 UTC

Re: git commit: add a serialization-safe class wrapper

Hi,


On Sat, Nov 24, 2012 at 8:16 PM, <iv...@apache.org> wrote:

> Updated Branches:
>   refs/heads/master 48044610a -> 65dd7b32e
>
>
> add a serialization-safe class wrapper
>
>
> Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
> Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/65dd7b32
> Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/65dd7b32
> Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/65dd7b32
>
> Branch: refs/heads/master
> Commit: 65dd7b32ea4f91862353ab0f7bafe3bca5e271ea
> Parents: 4804461
> Author: Igor Vaynberg <ig...@gmail.com>
> Authored: Sat Nov 24 11:16:32 2012 -0800
> Committer: Igor Vaynberg <ig...@gmail.com>
> Committed: Sat Nov 24 11:16:32 2012 -0800
>
> ----------------------------------------------------------------------
>  .../wicket/util/reference/ClassReference.java      |   72 +++++++++++++++
>  1 files changed, 72 insertions(+), 0 deletions(-)
> ----------------------------------------------------------------------
>
>
>
> http://git-wip-us.apache.org/repos/asf/wicket/blob/65dd7b32/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
> ----------------------------------------------------------------------
> diff --git
> a/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
> b/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
> new file mode 100644
> index 0000000..7653633
> --- /dev/null
> +++
> b/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
> @@ -0,0 +1,72 @@
> +package org.apache.wicket.util.reference;
> +
> +import java.io.Serializable;
> +import java.lang.ref.WeakReference;
> +
> +import org.apache.wicket.Application;
> +
> +/**
> + * A serialization-safe reference to a {@link Class}
> + *
> + * @author igor
> + *
> + * @param <T>
> + *            type of class
> + */
> +public class ClassReference<T> implements Serializable
> +{
> +       private transient WeakReference<Class<? extends T>> cache;
> +       private final String name;
> +
> +       /**
> +        * Constructor
> +        *
> +        * @param clazz
> +        */
> +       public ClassReference(Class<? extends T> clazz)
> +       {
> +               name = clazz.getName();
> +               cache(clazz);
> +       }
> +
> +       /**
> +        * @return the {@link Class} stored in this reference
> +        */
> +       @SuppressWarnings("unchecked")
> +       public Class<? extends T> get()
> +       {
> +               Class<? extends T> clazz = cache != null ? cache.get() :
> null;
> +               if (clazz == null)
> +               {
> +                       try
> +                       {
> +                               clazz = (Class<? extends
> T>)Application.get()
> +                                       .getApplicationSettings()
> +                                       .getClassResolver()
> +                                       .resolveClass(name);
>

Issue 1:
org.apache.wicket.core.util.lang.WicketObjects#resolveClass(String) does
almost the same.
Differences:
1) it falls back to the context class loader
2) it just logs a warning and returns null instead

Question: do we want to re-use it here ?


Issue 2:
org.apache.wicket.util.ClassProvider is something very similar to
ClassReference.
problem 1: ClassProvider is just broken in its current state because it
doesn't resolve the class from its name when the weakRef is null.
problem 2: Its only usage is
at org.apache.wicket.SystemMapper.HomePageProvider and it really doesn't
need ClassProvider but just IProvider<Class<? extends Page>>

Question: should we merge ClassProvider and ClassReference into one ?
I think the easiest way is to just deprecate ClassProvider.

+                       }
> +                       catch (ClassNotFoundException e)
> +                       {
> +                               throw new RuntimeException("Could not
> resolve class: " + name, e);
> +                       }
> +                       cache(clazz);
> +               }
> +               return clazz;
> +       }
> +
> +       private void cache(Class<? extends T> clazz)
> +       {
> +               cache = new WeakReference<Class<? extends T>>(clazz);
> +       }
> +
> +       /**
> +        * Diamond operator factory
> +        *
> +        * @param clazz
> +        * @return class reference
> +        */
> +       public static <T> ClassReference<T> of(Class<T> clazz)
> +       {
> +               return new ClassReference<T>(clazz);
> +       }
> +}
> \ No newline at end of file
>
>


-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>

Re: git commit: add a serialization-safe class wrapper

Posted by Martin Grigorov <mg...@apache.org>.
On Sun, Nov 25, 2012 at 9:01 PM, Igor Vaynberg <iv...@apache.org> wrote:

> On Sun, Nov 25, 2012 at 6:27 AM, Martin Grigorov <mg...@apache.org>
> wrote:
> > Hi,
> >
> >
> > On Sat, Nov 24, 2012 at 8:16 PM, <iv...@apache.org> wrote:
> >>
> >> Updated Branches:
> >>   refs/heads/master 48044610a -> 65dd7b32e
> >>
> >>
> >> add a serialization-safe class wrapper
> >>
> >>
> >> Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
> >> Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/65dd7b32
> >> Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/65dd7b32
> >> Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/65dd7b32
> >>
> >> Branch: refs/heads/master
> >> Commit: 65dd7b32ea4f91862353ab0f7bafe3bca5e271ea
> >> Parents: 4804461
> >> Author: Igor Vaynberg <ig...@gmail.com>
> >> Authored: Sat Nov 24 11:16:32 2012 -0800
> >> Committer: Igor Vaynberg <ig...@gmail.com>
> >> Committed: Sat Nov 24 11:16:32 2012 -0800
> >>
> >> ----------------------------------------------------------------------
> >>  .../wicket/util/reference/ClassReference.java      |   72
> +++++++++++++++
> >>  1 files changed, 72 insertions(+), 0 deletions(-)
> >> ----------------------------------------------------------------------
> >>
> >>
> >>
> >>
> http://git-wip-us.apache.org/repos/asf/wicket/blob/65dd7b32/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
> >> ----------------------------------------------------------------------
> >> diff --git
> >>
> a/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
> >>
> b/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
> >> new file mode 100644
> >> index 0000000..7653633
> >> --- /dev/null
> >> +++
> >>
> b/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
> >> @@ -0,0 +1,72 @@
> >> +package org.apache.wicket.util.reference;
> >> +
> >> +import java.io.Serializable;
> >> +import java.lang.ref.WeakReference;
> >> +
> >> +import org.apache.wicket.Application;
> >> +
> >> +/**
> >> + * A serialization-safe reference to a {@link Class}
> >> + *
> >> + * @author igor
> >> + *
> >> + * @param <T>
> >> + *            type of class
> >> + */
> >> +public class ClassReference<T> implements Serializable
> >> +{
> >> +       private transient WeakReference<Class<? extends T>> cache;
> >> +       private final String name;
> >> +
> >> +       /**
> >> +        * Constructor
> >> +        *
> >> +        * @param clazz
> >> +        */
> >> +       public ClassReference(Class<? extends T> clazz)
> >> +       {
> >> +               name = clazz.getName();
> >> +               cache(clazz);
> >> +       }
> >> +
> >> +       /**
> >> +        * @return the {@link Class} stored in this reference
> >> +        */
> >> +       @SuppressWarnings("unchecked")
> >> +       public Class<? extends T> get()
> >> +       {
> >> +               Class<? extends T> clazz = cache != null ? cache.get() :
> >> null;
> >> +               if (clazz == null)
> >> +               {
> >> +                       try
> >> +                       {
> >> +                               clazz = (Class<? extends
> >> T>)Application.get()
> >> +                                       .getApplicationSettings()
> >> +                                       .getClassResolver()
> >> +                                       .resolveClass(name);
> >
> >
> > Issue 1:
> > org.apache.wicket.core.util.lang.WicketObjects#resolveClass(String) does
> > almost the same.
> > Differences:
> > 1) it falls back to the context class loader
> > 2) it just logs a warning and returns null instead
> >
> > Question: do we want to re-use it here ?
>
> done. i dont much like the idea of delegating to the context because
> this class should fail outside of wicket context, but it doesnt matter
> that much i guess.
>
> > Issue 2:
> > org.apache.wicket.util.ClassProvider is something very similar to
> > ClassReference.
> > problem 1: ClassProvider is just broken in its current state because it
> > doesn't resolve the class from its name when the weakRef is null.
>
> yes, it seems very strange and very broken.
>
> > problem 2: Its only usage is at
> > org.apache.wicket.SystemMapper.HomePageProvider and it really doesn't
> need
> > ClassProvider but just IProvider<Class<? extends Page>>
> >
> > Question: should we merge ClassProvider and ClassReference into one ?
> > I think the easiest way is to just deprecate ClassProvider.
>
> i modified classreference to implement iprovider so it can now replace
> classprovider as a drop in. want to do it?
>

Done.
Thanks!

>
> -igor
>
> >
> >> +                       }
> >> +                       catch (ClassNotFoundException e)
> >> +                       {
> >> +                               throw new RuntimeException("Could not
> >> resolve class: " + name, e);
> >> +                       }
> >> +                       cache(clazz);
> >> +               }
> >> +               return clazz;
> >> +       }
> >> +
> >> +       private void cache(Class<? extends T> clazz)
> >> +       {
> >> +               cache = new WeakReference<Class<? extends T>>(clazz);
> >> +       }
> >> +
> >> +       /**
> >> +        * Diamond operator factory
> >> +        *
> >> +        * @param clazz
> >> +        * @return class reference
> >> +        */
> >> +       public static <T> ClassReference<T> of(Class<T> clazz)
> >> +       {
> >> +               return new ClassReference<T>(clazz);
> >> +       }
> >> +}
> >> \ No newline at end of file
> >>
> >
> >
> >
> > --
> > Martin Grigorov
> > jWeekend
> > Training, Consulting, Development
> > http://jWeekend.com
> >
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>

Re: git commit: add a serialization-safe class wrapper

Posted by Igor Vaynberg <iv...@apache.org>.
On Sun, Nov 25, 2012 at 6:27 AM, Martin Grigorov <mg...@apache.org> wrote:
> Hi,
>
>
> On Sat, Nov 24, 2012 at 8:16 PM, <iv...@apache.org> wrote:
>>
>> Updated Branches:
>>   refs/heads/master 48044610a -> 65dd7b32e
>>
>>
>> add a serialization-safe class wrapper
>>
>>
>> Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
>> Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/65dd7b32
>> Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/65dd7b32
>> Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/65dd7b32
>>
>> Branch: refs/heads/master
>> Commit: 65dd7b32ea4f91862353ab0f7bafe3bca5e271ea
>> Parents: 4804461
>> Author: Igor Vaynberg <ig...@gmail.com>
>> Authored: Sat Nov 24 11:16:32 2012 -0800
>> Committer: Igor Vaynberg <ig...@gmail.com>
>> Committed: Sat Nov 24 11:16:32 2012 -0800
>>
>> ----------------------------------------------------------------------
>>  .../wicket/util/reference/ClassReference.java      |   72 +++++++++++++++
>>  1 files changed, 72 insertions(+), 0 deletions(-)
>> ----------------------------------------------------------------------
>>
>>
>>
>> http://git-wip-us.apache.org/repos/asf/wicket/blob/65dd7b32/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
>> ----------------------------------------------------------------------
>> diff --git
>> a/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
>> b/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
>> new file mode 100644
>> index 0000000..7653633
>> --- /dev/null
>> +++
>> b/wicket-core/src/main/java/org/apache/wicket/util/reference/ClassReference.java
>> @@ -0,0 +1,72 @@
>> +package org.apache.wicket.util.reference;
>> +
>> +import java.io.Serializable;
>> +import java.lang.ref.WeakReference;
>> +
>> +import org.apache.wicket.Application;
>> +
>> +/**
>> + * A serialization-safe reference to a {@link Class}
>> + *
>> + * @author igor
>> + *
>> + * @param <T>
>> + *            type of class
>> + */
>> +public class ClassReference<T> implements Serializable
>> +{
>> +       private transient WeakReference<Class<? extends T>> cache;
>> +       private final String name;
>> +
>> +       /**
>> +        * Constructor
>> +        *
>> +        * @param clazz
>> +        */
>> +       public ClassReference(Class<? extends T> clazz)
>> +       {
>> +               name = clazz.getName();
>> +               cache(clazz);
>> +       }
>> +
>> +       /**
>> +        * @return the {@link Class} stored in this reference
>> +        */
>> +       @SuppressWarnings("unchecked")
>> +       public Class<? extends T> get()
>> +       {
>> +               Class<? extends T> clazz = cache != null ? cache.get() :
>> null;
>> +               if (clazz == null)
>> +               {
>> +                       try
>> +                       {
>> +                               clazz = (Class<? extends
>> T>)Application.get()
>> +                                       .getApplicationSettings()
>> +                                       .getClassResolver()
>> +                                       .resolveClass(name);
>
>
> Issue 1:
> org.apache.wicket.core.util.lang.WicketObjects#resolveClass(String) does
> almost the same.
> Differences:
> 1) it falls back to the context class loader
> 2) it just logs a warning and returns null instead
>
> Question: do we want to re-use it here ?

done. i dont much like the idea of delegating to the context because
this class should fail outside of wicket context, but it doesnt matter
that much i guess.

> Issue 2:
> org.apache.wicket.util.ClassProvider is something very similar to
> ClassReference.
> problem 1: ClassProvider is just broken in its current state because it
> doesn't resolve the class from its name when the weakRef is null.

yes, it seems very strange and very broken.

> problem 2: Its only usage is at
> org.apache.wicket.SystemMapper.HomePageProvider and it really doesn't need
> ClassProvider but just IProvider<Class<? extends Page>>
>
> Question: should we merge ClassProvider and ClassReference into one ?
> I think the easiest way is to just deprecate ClassProvider.

i modified classreference to implement iprovider so it can now replace
classprovider as a drop in. want to do it?

-igor

>
>> +                       }
>> +                       catch (ClassNotFoundException e)
>> +                       {
>> +                               throw new RuntimeException("Could not
>> resolve class: " + name, e);
>> +                       }
>> +                       cache(clazz);
>> +               }
>> +               return clazz;
>> +       }
>> +
>> +       private void cache(Class<? extends T> clazz)
>> +       {
>> +               cache = new WeakReference<Class<? extends T>>(clazz);
>> +       }
>> +
>> +       /**
>> +        * Diamond operator factory
>> +        *
>> +        * @param clazz
>> +        * @return class reference
>> +        */
>> +       public static <T> ClassReference<T> of(Class<T> clazz)
>> +       {
>> +               return new ClassReference<T>(clazz);
>> +       }
>> +}
>> \ No newline at end of file
>>
>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com
>