Re: Does FM support varargs or arrays in method calls?

"Newman, John W" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <EE3D0D387D01C8498EC1CED489DEAB25377A9F68FC@msxmbxnsprd01.acct.upmchs.net>
Yeah I tried 2.4 about 2 months ago and it wasn't stable enough at the time.  If a backport of that feature to 2.3.x isn't too much work that would be cool.

For now, I only have 3 places that call this method, and at most they'll pass in 5 parameters.  So I guess temporarily I can put up with this crap,

        public <E extends Entity> List<E> list(Class<E> entityClass,
                        String queryName, List<Object> queryParams) {
                switch (queryParams.size()) {
                        case 0:
                                return entityManager.findByNamedQuery(entityClass, queryName);
                        case 1:
                                return entityManager.findByNamedQuery(entityClass, queryName,
                                                queryParams.get(0));
                        case 2:
                                return entityManager.findByNamedQuery(entityClass, queryName,
                                                queryParams.get(0), queryParams.get(1));
                        case 3:
                                return entityManager.findByNamedQuery(entityClass, queryName,
                                                queryParams.get(0), queryParams.get(1), queryParams
                                                                .get(2));
                        case 4:
                                return entityManager.findByNamedQuery(entityClass, queryName,
                                                queryParams.get(0), queryParams.get(1), queryParams
                                                                .get(2), queryParams.get(3));
                        case 5:
                                return entityManager
                                                .findByNamedQuery(entityClass, queryName, queryParams
                                                                .get(0), queryParams.get(1),
                                                                queryParams.get(2), queryParams.get(3),
                                                                queryParams.get(4));
                        default:
                                throw new IllegalArgumentException(
                                                "More than 5 query parameters is currently not supported.");
                }
        }

That is really really bad, but as far as I can tell it's my only option at this point.  Any other ideas short of that mess or a backport?  I can't help but wonder if there isn't a feature of the language for this.  Collections should be autoboxed! :P

A google search led me here http://constc.blogspot.com/2007/04/calling-java-vararg-methods-from.html
Sounds like fun!  If I get bored this afternoon I'm gonna look in your svn to see how that all works, quite an interesting concept with for sure a very challenging implementation.

Thanks again,
John


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Daniel Dekany
Sent: Thursday, December 06, 2007 6:22 AM
To: Attila Szegedi
Cc: FreeMarker-user
Subject: Re: [FreeMarker-user] Does FM support varargs or arrays in method calls?

Thursday, December 6, 2007, 9:43:28 AM, Attila Szegedi wrote:

> We've added vararg handling to 2.4 (which is not yet released).
> There's currently no vararg handling in 2.3. Can you try using 2.4?
> You can grab the latest 2.4 build from here:

That I don't recommend, as it is not stable.

> - go to <http://freemarker.org:8085/browse/FM-TRUNK/latest>
> - click "Artifacts" tab
> - download the freemarker.jar behind the "Library" link
>
> Failing that, I can backport vararg handling to 2.3.x branch.
>
> Attila.
>
> On 2007.12.05., at 23:17, Newman, John W wrote:
>
>> Hi,
>>
>> This is interesting, I might be in trouble here - hopefully I'm just
>> missing something.   Consider this,
>>
>> class WrappedObject  {
>>      public <E extends Entity> java.util.List<E>
>> findByNamedQuery(Class<E> entityClass,
>>                  String query, Object... queryParams) {
>>            return entityManager.findByNamedQuery(entityClass, query,
>> queryParams);
>>      }
>> }
>>
>>
>> class EntityManager  {
>>      public <E extends Entity> java.util.List<E>
>> findByNamedQuery(Class<E> entityClass,
>>                  String query, Object... queryParams) {
>>            // do hibernate stuff here
>>      }
>> }
>>
>>
>> [#assign results = wrappedObject.findByNamedQuery(${class},
>> "findByFirstAndLastName", ["John", "Smith"]) /]
>>
>> Should work right?
>>
>> : freemarker.template.TemplateModelException: No signature of method
>> findByNamedQuery matches
>> (java
>> .lang.Class,java.lang.String,freemarker.ext.beans.SequenceAdapter)
>>
>> If I forget about the varargs and change the wrapped objects method
>> to use Object[] queryParams, I get the exact same exception.
>> However if I change it to List<Object>, ["John", "Smith"] matches
>> the rule, and the method gets invoked.
>>
>> That's ok, but I can't change the API of the EntityManager class -
>> we're stuck with varargs there.  So,
>>
>>
>> class WrappedObject  {
>>      public <E extends Entity> java.util.List<E>
>> findByNamedQuery(Class<E> entityClass,
>>                  String query, List<Object> queryParams) {
>>            return entityManager.findByNamedQuery(entityClass, query,
>> queryParams == null ? queryParams : queryParams.toArray(new
>> Object[queryParams.size()]));
>>      }
>> }
>>
>> I really thought that would work.  Unfortunately, all it does is
>> pass in 1 parameter to entityManager's method, an Object[] - not
>> Object, Object.  Interesting.  Any ideas?  Is there different syntax
>> for arrays & lists?
>>
>> Any help is greatly appreciated.
>>
>> Thanks,
>> john
>> -------------------------------------------------------------------------
>> SF.Net email is sponsored by: The Future of Linux Business White Paper
>> from Novell.  From the desktop to the data center, Linux is going
>> mainstream.  Let it simplify your IT future.
>> http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4_______________________________________________
>> FreeMarker-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>
> Attila.
>
> --
> home: http://www.szegedi.org
> weblog: http://constc.blogspot.com
>
>
>
>
> -------------------------------------------------------------------------
> SF.Net email is sponsored by: The Future of Linux Business White Paper
> from Novell.  From the desktop to the data center, Linux is going
> mainstream.  Let it simplify your IT future.
> http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user

--
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.