You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Christoph Reck <Ch...@dlr.de> on 2000/12/07 17:18:13 UTC

[PATCH] MethodMap fixed!

The problem with introspection not locating the right method
was caused by Class.isAssignableFrom() not considering that a
primitive parameter can be assigned from the corresponding
java.lang class.

The patch also fixes what kept the $formatter.isNull(...) from 
working, because NULL can be passed to any non-primitive 
parameter.

Diff -u for the file MethodMap.java
----------------------------- cut here -------------------------------
--- Apache-Velocity-20001206/src/java/org/apache/velocity/util/introspection/MethodMap.java      Fri Nov 10 04:27:39 2000
+++ Apache-Velocity-20001206/bin/src/org/apache/velocity/util/introspection/MethodMap.java       Thu Dec  7 16:58:52 2000
@@ -124,7 +124,20 @@
                     if (j >= parameterTypes.length)
                         return method;
 
-                    if (!parameterTypes[j].isAssignableFrom(params[j].getClass()))
+                    Class c = parameterTypes[j];
+                    Object p = params[j];
+                    if ( c.isPrimitive() )
+                    {
+                        try
+                        {
+                            if ( c != p.getClass().getField("TYPE").get(p) )
+                                break;
+                        } catch (Exception ex) {
+                            break; // p is not a primitive derivate
+                        }
+                    }
+                    else if ( (p != null) &&
+                              !c.isAssignableFrom( p.getClass() ) )
                         break;
                 }
             }
----------------------------- cut here -------------------------------

:) Christoph

Re: [PATCH] MethodMap fixed!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Christoph Reck wrote:
> 
> The problem with introspection not locating the right method
> was caused by Class.isAssignableFrom() not considering that a
> primitive parameter can be assigned from the corresponding
> java.lang class.
> 
> The patch also fixes what kept the $formatter.isNull(...) from
> working, because NULL can be passed to any non-primitive
> parameter.

excellent.

> 
> Diff -u for the file MethodMap.java
> ----------------------------- cut here -------------------------------
> --- Apache-Velocity-20001206/src/java/org/apache/velocity/util/introspection/MethodMap.java      Fri Nov 10 04:27:39 2000
> +++ Apache-Velocity-20001206/bin/src/org/apache/velocity/util/introspection/MethodMap.java       Thu Dec  7 16:58:52 2000
> @@ -124,7 +124,20 @@
>                      if (j >= parameterTypes.length)
>                          return method;
> 
> -                    if (!parameterTypes[j].isAssignableFrom(params[j].getClass()))
> +                    Class c = parameterTypes[j];
> +                    Object p = params[j];
> +                    if ( c.isPrimitive() )
> +                    {
> +                        try
> +                        {
> +                            if ( c != p.getClass().getField("TYPE").get(p) )
> +                                break;
> +                        } catch (Exception ex) {
> +                            break; // p is not a primitive derivate
> +                        }
> +                    }
> +                    else if ( (p != null) &&
> +                              !c.isAssignableFrom( p.getClass() ) )
>                          break;
>                  }
>              }
> ----------------------------- cut here -------------------------------
> 
> :) Christoph

-- 
Geir Magnusson Jr.                               geirm@optonline.com

Re: [PATCH] MethodMap fixed!

Posted by jv...@periapt.com.

On Thu, 7 Dec 2000, Christoph Reck wrote:

> The problem with introspection not locating the right method
> was caused by Class.isAssignableFrom() not considering that a
> primitive parameter can be assigned from the corresponding
> java.lang class.
> 
> The patch also fixes what kept the $formatter.isNull(...) from 
> working, because NULL can be passed to any non-primitive 
> parameter.

Thanks, I will apply the patch and test. Looks fine, did you
run the testbed?

jvz.
 
> Diff -u for the file MethodMap.java
> ----------------------------- cut here -------------------------------
> --- Apache-Velocity-20001206/src/java/org/apache/velocity/util/introspection/MethodMap.java      Fri Nov 10 04:27:39 2000
> +++ Apache-Velocity-20001206/bin/src/org/apache/velocity/util/introspection/MethodMap.java       Thu Dec  7 16:58:52 2000
> @@ -124,7 +124,20 @@
>                      if (j >= parameterTypes.length)
>                          return method;
>  
> -                    if (!parameterTypes[j].isAssignableFrom(params[j].getClass()))
> +                    Class c = parameterTypes[j];
> +                    Object p = params[j];
> +                    if ( c.isPrimitive() )
> +                    {
> +                        try
> +                        {
> +                            if ( c != p.getClass().getField("TYPE").get(p) )
> +                                break;
> +                        } catch (Exception ex) {
> +                            break; // p is not a primitive derivate
> +                        }
> +                    }
> +                    else if ( (p != null) &&
> +                              !c.isAssignableFrom( p.getClass() ) )
>                          break;
>                  }
>              }
> ----------------------------- cut here -------------------------------
> 
> :) Christoph
> 


Re: [PATCH] MethodMap fixed!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Christoph Reck wrote:
> 
> On my solaris box I cannot run the testbed. Running
> "build-velocity.sh test" I get:
>         Buildfile: build-velocity.xml
> 
>         test:
>         Running JUnit template tests for Velocity ...
>         Could not invoke the suite() method
> 
> I'm using the 20001206 snapshot. I guess it might have something
> to do with JTest 3.2 and the xerces/xalan 1.2.1 jars?
> What can be done to get it running again? (restoring xerces breaks ant...)
> Where can I get a JTest compatible with the new xerces?
> Is there a way of calling JTest with mannualy setup classpath?

I'm not so sure that's the reason.  I hope it is, but not convinced. 
When I was doing the #set() recently, I ran into the same thing.  I was
seeing some bizarre behavior / side-effects from the parser at that
time, and when I backed out / modified parser changes it worked again. 
I didn't have the time to chase it down, but I guess i should have. :)

Is that a clean 20001206 snapshot with just your patches?  Or have you
modified other things?  

geir

> :) Christoph
> 
> jvanzyl@periapt.com wrote:
> >
> > On Thu, 7 Dec 2000, Christoph Reck wrote:
> >
> > > The problem with introspection not locating the right method
> > > was caused by Class.isAssignableFrom() not considering that a
> > > primitive parameter can be assigned from the corresponding
> > > java.lang class.
> > >
> > > The patch also fixes what kept the $formatter.isNull(...) from
> > > working, because NULL can be passed to any non-primitive
> > > parameter.
> >
> > That passes the testbed! Do you think you could add a
> > test template that demonstrates what you fixed? Add
> > a method to TestProvider that would not have been
> > introspected correctly before your patch was applied.
> >
> > jvz.
> 
> I'll provide it when JTest is running here....

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Re: [PATCH] MethodMap fixed!

Posted by Christoph Reck <Ch...@dlr.de>.
On my solaris box I cannot run the testbed. Running 
"build-velocity.sh test" I get:
	Buildfile: build-velocity.xml

	test:
	Running JUnit template tests for Velocity ...
	Could not invoke the suite() method

I'm using the 20001206 snapshot. I guess it might have something
to do with JTest 3.2 and the xerces/xalan 1.2.1 jars? 
What can be done to get it running again? (restoring xerces breaks ant...)
Where can I get a JTest compatible with the new xerces? 
Is there a way of calling JTest with mannualy setup classpath?

:) Christoph

jvanzyl@periapt.com wrote:
> 
> On Thu, 7 Dec 2000, Christoph Reck wrote:
> 
> > The problem with introspection not locating the right method
> > was caused by Class.isAssignableFrom() not considering that a
> > primitive parameter can be assigned from the corresponding
> > java.lang class.
> >
> > The patch also fixes what kept the $formatter.isNull(...) from
> > working, because NULL can be passed to any non-primitive
> > parameter.
> 
> That passes the testbed! Do you think you could add a
> test template that demonstrates what you fixed? Add
> a method to TestProvider that would not have been
> introspected correctly before your patch was applied.
> 
> jvz.

I'll provide it when JTest is running here....

Re: [PATCH] MethodMap fixed!

Posted by jv...@periapt.com.

> If you insist I should send patches let me know and I'll
> prepare them.

I'm not insisting anything :-) But if you want to provide
patches to fix the two items listed below that would
be great!
 
> Two problems need to be addressed:
> 
> 1. Methods with primitive parameters combined with supperclass, e.g.:
>    $list.set(0, "First Element") did not work, beacuse set(int, Object)
>    did not match the signature of set(int, String).
>    Previously chop(String, int) worked because the ClassMap populated 
>    the methodCache with the 'int' already expanded to 'java.lang.Integer'.
> 
>     TestProvider.print(int index, Object value)
>     {
>         return "" + int + ":" + value.toString();
>     }
> 
>    Used as:
>     $provider.print(0, "test")
> 
> 2. Passing a null argument to a method (from VelocityFormatter)
> 
>     public Object isNull(Object o, Object dflt)
>     {
>         return (o == null) ? dflt : o;
>     }
> 
>    Used as:
>     $provider.isNull($provider.Null, "using default")
>     $provider.isNull($provider.me(), "Should not show this")
>    (note the ugly () after "me". This will stay if we agree
>     keeping the restriction on ASTIndentifier to access beans.
>     The () magically makes it into a ASTMethod and then it 
>     works).
> The TestProvider.setState(Boolean state) is obsolete.

Yup, it can be nuked.

jvz.


Re: [PATCH] MethodMap fixed!

Posted by Christoph Reck <Ch...@dlr.de>.
> Do you think you could add a
> test template that demonstrates what you fixed? Add
> a method to TestProvider that would not have been
> introspected correctly before your patch was applied.

It seems to be harder to supply the patches to the testbed,
than for you to patch it with the hints give here.

If you insist I should send patches let me know and I'll
prepare them.

Two problems need to be addressed:

1. Methods with primitive parameters combined with supperclass, e.g.:
   $list.set(0, "First Element") did not work, beacuse set(int, Object)
   did not match the signature of set(int, String).
   Previously chop(String, int) worked because the ClassMap populated 
   the methodCache with the 'int' already expanded to 'java.lang.Integer'.

    TestProvider.print(int index, Object value)
    {
        return "" + int + ":" + value.toString();
    }

   Used as:
    $provider.print(0, "test")

2. Passing a null argument to a method (from VelocityFormatter)

    public Object isNull(Object o, Object dflt)
    {
        return (o == null) ? dflt : o;
    }

   Used as:
    $provider.isNull($provider.Null, "using default")
    $provider.isNull($provider.me(), "Should not show this")
   (note the ugly () after "me". This will stay if we agree
    keeping the restriction on ASTIndentifier to access beans.
    The () magically makes it into a ASTMethod and then it 
    works).

Note that one Java-imminent problem remains: A signature with
an Object[] will not accept a parameter String[]. This type of
mapping would be very expensive...

The TestProvider.setState(Boolean state) is obsolete.

:) Christoph

Re: [PATCH] MethodMap fixed!

Posted by jv...@periapt.com.

On Thu, 7 Dec 2000, Christoph Reck wrote:

> The problem with introspection not locating the right method
> was caused by Class.isAssignableFrom() not considering that a
> primitive parameter can be assigned from the corresponding
> java.lang class.
> 
> The patch also fixes what kept the $formatter.isNull(...) from 
> working, because NULL can be passed to any non-primitive 
> parameter.

That passes the testbed! Do you think you could add a
test template that demonstrates what you fixed? Add
a method to TestProvider that would not have been 
introspected correctly before your patch was applied.

jvz.