You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ge...@apache.org on 2001/02/01 19:22:33 UTC
cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node ASTReference.java
geirm 01/02/01 10:22:33
Modified: src/java/org/apache/velocity/runtime/parser/node
ASTReference.java
Log:
Added support for using lower-case property identifiers in #set() to match the
ability to get values that way.
So
#set($foo.bar = "hi")
will try to call setbar("hi") and then setBar("hi")
Revision Changes Path
1.20 +24 -3 jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
Index: ASTReference.java
===================================================================
RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- ASTReference.java 2001/01/18 05:08:18 1.19
+++ ASTReference.java 2001/02/01 18:22:30 1.20
@@ -79,7 +79,7 @@
* @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
* @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
- * @version $Id: ASTReference.java,v 1.19 2001/01/18 05:08:18 geirm Exp $
+ * @version $Id: ASTReference.java,v 1.20 2001/02/01 18:22:30 geirm Exp $
*/
public class ASTReference extends SimpleNode
@@ -300,12 +300,33 @@
Class[] params = { value.getClass() };
Class c = result.getClass();
- Method m = c.getMethod("set" + identifier, params);
+ Method m = null;
+ try
+ {
+ m = c.getMethod("set" + identifier, params);
+ }
+ catch( NoSuchMethodException nsme2)
+ {
+ StringBuffer sb = new StringBuffer( "set" );
+ sb.append( identifier );
+
+ if( Character.isLowerCase( sb.charAt(3)))
+ {
+ sb.setCharAt( 3 , Character.toUpperCase( sb.charAt( 3 ) ) );
+ }
+ else
+ {
+ sb.setCharAt( 3 , Character.toLowerCase( sb.charAt( 3 ) ) );
+ }
+
+ m = c.getMethod( sb.toString(), params);
+ }
+
/*
* and if we get here, getMethod() didn't chuck an exception...
*/
-
+
Object[] args = { value };
m.invoke(result, args);
}
Re: cvs
commit:jakarta-velocity/src/java/org/apache/velocity/runtime/parser/nodeASTReference.java
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Christoph Reck wrote:
>
> It would be simpler and faster to just do:
>
> - Method m = c.getMethod("set" + identifier, params);
> + String setterName = "set" +
> + Character.toUpperCase( identifier.charAt(0) ) +
> + identifier.substring(1);
> + Method m = c.getMethod(setterName, params);
>
> This change is to assist users of beans to be able to just
> call #set( $foo.bar = "woobie" ) to call foo.setBar("woobie")
> method. It was sort of strange to be required to do
> #set( $foo.Bar = "woobie" ) and it was already causing some
> problems and questions...
You can do this now. Did you even look at the code commit you are
responding to, or better yet, <gasp> actually try it? :)
What I *do* take away from this is that we should still do both, but
maybe always try setFoo() before setfoo(), as most people will write
setter methods that look like the former, so it would be quicker, on
average.
Now, I am interested in making a clear, simple set of introspection
guidelines for users, so one can author templates that will result in
our first introspectiona attempt being successful, for performance
reasons.
I guess $foo.bar is more 'natural' than $foo.Bar, so that's what people
would do?
But I still we should try the latter on failure, just to be friendly and
consistant.
> It is a bean rule that the character after set is uppercase!
> See http://java.sun.com/beans/docs/spec.html section 8.3 of
> beans_101.pdf.
Is that really a clearly stated rule, or do we just infer that? I agree
with the conclusion, but don't agree that the bean rule states anything
clearly about this... It doesn't matter - the results will be the same
- I'm just quibbling...
> I believe we are not yet in a required backward compatibility state,
> and anyone thad did a setxxx(...) instead of setXxx(...) should
> rewrite his code to conform to standard Java (and beans) conventions.
No. I don't think we should force anyone to do anything. We shouldn't
support things that are dangerous (like introspecting private fields :),
but there is no cost to us, or them, if they do something like setxxx()
We can give a stern warning in the log :
[warning] template=foo.vm [21,3] : Christoph says 'Rewrite your code!'
:)
Just think of it as a cheap way of reducing user frustration and
questions.
geir
> >
> > geirm 01/02/01 10:22:33
> >
> > Modified: src/java/org/apache/velocity/runtime/parser/node
> > ASTReference.java
> > Log:
> > Added support for using lower-case property identifiers in #set() to match the
> > ability to get values that way.
> >
> > So
> >
> > #set($foo.bar = "hi")
> >
> > will try to call setbar("hi") and then setBar("hi")
>
> This is not conform and anyone that did a setbar("hi") should rewrite his
> code.
>
> >
> > Revision Changes Path
> > 1.20 +24 -3 jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
> >
> > Index: ASTReference.java
> > ===================================================================
> > RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java,v
> > retrieving revision 1.19
> > retrieving revision 1.20
> > diff -u -r1.19 -r1.20
> > --- ASTReference.java 2001/01/18 05:08:18 1.19
> > +++ ASTReference.java 2001/02/01 18:22:30 1.20
> > @@ -79,7 +79,7 @@
> > * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
> > * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
> > * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
> > - * @version $Id: ASTReference.java,v 1.19 2001/01/18 05:08:18 geirm Exp $
> > + * @version $Id: ASTReference.java,v 1.20 2001/02/01 18:22:30 geirm Exp $
> > */
> >
> > public class ASTReference extends SimpleNode
> > @@ -300,12 +300,33 @@
> >
> > Class[] params = { value.getClass() };
> > Class c = result.getClass();
> > - Method m = c.getMethod("set" + identifier, params);
> > + Method m = null;
> >
> > + try
> > + {
> > + m = c.getMethod("set" + identifier, params);
> > + }
> > + catch( NoSuchMethodException nsme2)
> > + {
> > + StringBuffer sb = new StringBuffer( "set" );
> > + sb.append( identifier );
> > +
> > + if( Character.isLowerCase( sb.charAt(3)))
> > + {
> > + sb.setCharAt( 3 , Character.toUpperCase( sb.charAt( 3 ) ) );
> > + }
> > + else
> > + {
> > + sb.setCharAt( 3 , Character.toLowerCase( sb.charAt( 3 ) ) );
> > + }
> > +
> > + m = c.getMethod( sb.toString(), params);
> > + }
> > +
> > /*
> > * and if we get here, getMethod() didn't chuck an exception...
> > */
> > -
> > +
> > Object[] args = { value };
> > m.invoke(result, args);
> > }
> >
> >
> >
>
> --
> ==============================================================
> Deutsches Zentrum fuer Luft- und Raumfahrt (DLR)
> Deutsches Fernerkundungs Datenzentrum (DFD)
> DLR-DFD, Muenchner Strasse 20, D-82234 Wessling, Germany
> ============= Currenlty relocated to ESA-ESRIN ===============
> ESA ESRIN Tel: +39 06 941 80 589
> c/o Christoph Reck (DLR) Fax: +39 06 941 80 512
> Via Galileo Galilei mailto:Christoph.Reck@dlr.de
> I-00044 Frascati (Roma) http://www.dfd.dlr.de
> ==============================================================
--
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: cvs commit:
jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node
ASTReference.java
Posted by Christoph Reck <Ch...@dlr.de>.
It would be simpler and faster to just do:
- Method m = c.getMethod("set" + identifier, params);
+ String setterName = "set" +
+ Character.toUpperCase( identifier.charAt(0) ) +
+ identifier.substring(1);
+ Method m = c.getMethod(setterName, params);
This change is to assist users of beans to be able to just
call #set( $foo.bar = "woobie" ) to call foo.setBar("woobie")
method. It was sort of strange to be required to do
#set( $foo.Bar = "woobie" ) and it was already causing some
problems and questions...
It is a bean rule that the character after set is uppercase!
See http://java.sun.com/beans/docs/spec.html section 8.3 of
beans_101.pdf.
I believe we are not yet in a required backward compatibility state,
and anyone thad did a setxxx(...) instead of setXxx(...) should
rewrite his code to conform to standard Java (and beans) conventions.
Otherwise if he really wants to call a setxxx(), then he has to write
it explicetely: #call( $foo.setbar("woobie") )
(here I used my #macro( call $foo )#if($foo)#**##end#end ).
:) Christoph
geirm@apache.org wrote:
>
> geirm 01/02/01 10:22:33
>
> Modified: src/java/org/apache/velocity/runtime/parser/node
> ASTReference.java
> Log:
> Added support for using lower-case property identifiers in #set() to match the
> ability to get values that way.
>
> So
>
> #set($foo.bar = "hi")
>
> will try to call setbar("hi") and then setBar("hi")
This is not conform and anyone that did a setbar("hi") should rewrite his
code.
>
> Revision Changes Path
> 1.20 +24 -3 jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
>
> Index: ASTReference.java
> ===================================================================
> RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java,v
> retrieving revision 1.19
> retrieving revision 1.20
> diff -u -r1.19 -r1.20
> --- ASTReference.java 2001/01/18 05:08:18 1.19
> +++ ASTReference.java 2001/02/01 18:22:30 1.20
> @@ -79,7 +79,7 @@
> * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
> * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
> * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
> - * @version $Id: ASTReference.java,v 1.19 2001/01/18 05:08:18 geirm Exp $
> + * @version $Id: ASTReference.java,v 1.20 2001/02/01 18:22:30 geirm Exp $
> */
>
> public class ASTReference extends SimpleNode
> @@ -300,12 +300,33 @@
>
> Class[] params = { value.getClass() };
> Class c = result.getClass();
> - Method m = c.getMethod("set" + identifier, params);
> + Method m = null;
>
> + try
> + {
> + m = c.getMethod("set" + identifier, params);
> + }
> + catch( NoSuchMethodException nsme2)
> + {
> + StringBuffer sb = new StringBuffer( "set" );
> + sb.append( identifier );
> +
> + if( Character.isLowerCase( sb.charAt(3)))
> + {
> + sb.setCharAt( 3 , Character.toUpperCase( sb.charAt( 3 ) ) );
> + }
> + else
> + {
> + sb.setCharAt( 3 , Character.toLowerCase( sb.charAt( 3 ) ) );
> + }
> +
> + m = c.getMethod( sb.toString(), params);
> + }
> +
> /*
> * and if we get here, getMethod() didn't chuck an exception...
> */
> -
> +
> Object[] args = { value };
> m.invoke(result, args);
> }
>
>
>
--
==============================================================
Deutsches Zentrum fuer Luft- und Raumfahrt (DLR)
Deutsches Fernerkundungs Datenzentrum (DFD)
DLR-DFD, Muenchner Strasse 20, D-82234 Wessling, Germany
============= Currenlty relocated to ESA-ESRIN ===============
ESA ESRIN Tel: +39 06 941 80 589
c/o Christoph Reck (DLR) Fax: +39 06 941 80 512
Via Galileo Galilei mailto:Christoph.Reck@dlr.de
I-00044 Frascati (Roma) http://www.dfd.dlr.de
==============================================================