You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Daniel F. Savarese" <df...@savarese.org> on 2004/05/16 21:17:23 UTC

[jelly] tag for accessing static fields

I've been experimenting with Jelly Swing recently after finding Groovy
Swing problematic, trying to see whether it will make GUI prototyping
faster.  I may have missed it, but I couldn't find a tag in any of the
tag libraries to access a static class field.  There's invokeStatic
for invoking static methods.  For the project I'm working on, I
wrote a GetStaticTag that works like this:

 <v:getStatic var="closeOperation" className="javax.swing.JFrame"
              field="EXIT_ON_CLOSE"/>

That allowed me to then do:

 <frame title="Tree Frame" var="frame" size="400,400"
        defaultCloseOperation="${closeOperation}">
   ...
 </frame>

I think it's pretty important to be able to access static fields
and that a getStatic (or some other name) tag would be useful to have
in the core tag library.  If I convert my GetStaticTag class code to
the style convention used in the Jelly source and add a Jelly test
for it, is it all right for me to add it to the source tree?  I've
appended the simple class at the end of this message.  I guess the
question is a two-parter:
  1. Does the Jelly core tag library need a getStatic tag to complement
     getStatic?

     [ ] +1 Yes, it's generally useful.
     [ ] -1 No, it's easy for someone who needs it to make their own tag.

  2. May I commit code for the tag to the repository assuming I convert
     it to the proper style and provide a Jelly test?

     [ ] +1 Go right ahead, we can tweak the code after it's committed.
     [ ] -1 No, we need to agree on what to call the tag, you should
            try more than one class loader in doTag, some other reason ...

daniel

package org.savarese.vserv.jelly;

import org.apache.commons.jelly.*;

public class GetStaticTag extends TagSupport {

  private String __var;
  private String __field;
  private String __className;

  public void setVar(String var) {
    __var = var;
  }

  public void setField(String field) {
    __field = field;
  }

  public void setClassName(String className) {
    __className = className;
  }

  public void doTag(XMLOutput output) throws JellyTagException {
    String message = null;

    if(__var == null)
      message = "var";
    else if(__field == null)
      message = "field";
    else if(__className == null)
      message = "className";

    if(message != null)
      throw new MissingAttributeException(message);

    try {
      Class type     = getClass().getClassLoader().loadClass(__className);
      Object result  = type.getField(__field).get(null);
      JellyContext context = getContext();

      context.setVariable(__var, result);

    } catch(Throwable t) {
      throw new JellyTagException("Could not access " + __className + "." +
                                  __var + ".  Original exception message: " +
                                  t.getMessage(), t);
    }
  }

}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [jelly] tag for accessing static fields

Posted by Paul Libbrecht <pa...@activemath.org>.
On 16-May-04, at 21:17 Uhr, Daniel F. Savarese wrote:
> wrote a GetStaticTag that works like this:
>
>  <v:getStatic var="closeOperation" className="javax.swing.JFrame"
>               field="EXIT_ON_CLOSE"/>
>
> That allowed me to then do:
>
>  <frame title="Tree Frame" var="frame" size="400,400"
>         defaultCloseOperation="${closeOperation}">
>    ...
>  </frame>

Daniel,

I think your addition makes sense.

I keep believing, however, that such a thing should be done by Jexl or 
something else... it makes no sense to need this getStatic, at least 
compared to Java.

Yet an alternative way for this was partially done in 
GridBagConstraintBean: namely it has been tried to have custom 
converters that would decode such (expected) constants into an integer.
Indeed, as the value of the defaultCloseOperation one expects not much 
else than the value of the constants of JFrame that apply for this. The 
fact that they are integers is plain syntactic sugar.

I haven't found the best pattern to write such converters though. Maybe 
I've been missing the understanding of something on BeanUtils...

Feel free to go ahead but let's start this discussion on the usages of 
converters in jelly-attributes since it seems BeanUtils folks are 
"eagerly expecting users" ;-->

thanks

paul


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [jelly] tag for accessing static fields

Posted by di...@multitask.com.au.
Sounds like a good addition.

"Daniel F. Savarese" <df...@savarese.org> wrote on 17/05/2004 05:17:23 AM:

> 
> I've been experimenting with Jelly Swing recently after finding Groovy
> Swing problematic, trying to see whether it will make GUI prototyping
> faster.  I may have missed it, but I couldn't find a tag in any of the
> tag libraries to access a static class field.  There's invokeStatic
> for invoking static methods.  For the project I'm working on, I
> wrote a GetStaticTag that works like this:
> 
>  <v:getStatic var="closeOperation" className="javax.swing.JFrame"
>               field="EXIT_ON_CLOSE"/>
> 
> That allowed me to then do:
> 
>  <frame title="Tree Frame" var="frame" size="400,400"
>         defaultCloseOperation="${closeOperation}">
>    ...
>  </frame>
> 
> I think it's pretty important to be able to access static fields
> and that a getStatic (or some other name) tag would be useful to have
> in the core tag library.  If I convert my GetStaticTag class code to
> the style convention used in the Jelly source and add a Jelly test
> for it, is it all right for me to add it to the source tree?  I've
> appended the simple class at the end of this message.  I guess the
> question is a two-parter:
>   1. Does the Jelly core tag library need a getStatic tag to complement
>      getStatic?
> 
>      [ ] +1 Yes, it's generally useful.
>      [ ] -1 No, it's easy for someone who needs it to make their own 
tag.

+1.

> 
>   2. May I commit code for the tag to the repository assuming I convert
>      it to the proper style and provide a Jelly test?
> 
>      [ ] +1 Go right ahead, we can tweak the code after it's committed.
>      [ ] -1 No, we need to agree on what to call the tag, you should
>             try more than one class loader in doTag, some other reason 
...

+1.
--
dIon Gillard, Multitask Consulting


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org