Save This Page
Home » freemarker-2.3.13 » freemarker.ext.beans » [javadoc | source]
    1   /*
    2    * Copyright (c) 2003 The Visigoth Software Society. All rights
    3    * reserved.
    4    *
    5    * Redistribution and use in source and binary forms, with or without
    6    * modification, are permitted provided that the following conditions
    7    * are met:
    8    *
    9    * 1. Redistributions of source code must retain the above copyright
   10    *    notice, this list of conditions and the following disclaimer.
   11    *
   12    * 2. Redistributions in binary form must reproduce the above copyright
   13    *    notice, this list of conditions and the following disclaimer in
   14    *    the documentation and/or other materials provided with the
   15    *    distribution.
   16    *
   17    * 3. The end-user documentation included with the redistribution, if
   18    *    any, must include the following acknowledgement:
   19    *       "This product includes software developed by the
   20    *        Visigoth Software Society (http://www.visigoths.org/)."
   21    *    Alternately, this acknowledgement may appear in the software itself,
   22    *    if and wherever such third-party acknowledgements normally appear.
   23    *
   24    * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the 
   25    *    project contributors may be used to endorse or promote products derived
   26    *    from this software without prior written permission. For written
   27    *    permission, please contact visigoths@visigoths.org.
   28    *
   29    * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
   30    *    nor may "FreeMarker" or "Visigoth" appear in their names
   31    *    without prior written permission of the Visigoth Software Society.
   32    *
   33    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   34    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   35    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   36    * DISCLAIMED.  IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
   37    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   38    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   39    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   40    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   41    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   42    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   43    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44    * SUCH DAMAGE.
   45    * ====================================================================
   46    *
   47    * This software consists of voluntary contributions made by many
   48    * individuals on behalf of the Visigoth Software Society. For more
   49    * information on the Visigoth Software Society, please see
   50    * http://www.visigoths.org/
   51    */
   52   
   53   package freemarker.ext.beans;
   54   
   55   import java.lang.reflect.Field;
   56   import java.lang.reflect.Method;
   57   import java.lang.reflect.Modifier;
   58   import java.util.HashMap;
   59   import java.util.Iterator;
   60   import java.util.Map;
   61   
   62   import freemarker.log.Logger;
   63   import freemarker.template.TemplateCollectionModel;
   64   import freemarker.template.TemplateHashModelEx;
   65   import freemarker.template.TemplateModel;
   66   import freemarker.template.TemplateModelException;
   67   
   68   /**
   69    * Wraps the static fields and methods of a class in a
   70    * {@link freemarker.template.TemplateHashModel}.
   71    * Fields are wrapped using {@link BeansWrapper#wrap(Object)}, and
   72    * methods are wrapped into an appropriate {@link freemarker.template.TemplateMethodModelEx} instance.
   73    * Unfortunately, there is currently no support for bean property-style
   74    * calls of static methods, similar to that in {@link BeanModel}.
   75    * @author Attila Szegedi
   76    * @version $Id: StaticModel.java,v 1.24.2.1 2006/11/12 10:23:02 szegedia Exp $
   77    */
   78   final class StaticModel implements TemplateHashModelEx
   79   {
   80       private static final Logger logger = Logger.getLogger("freemarker.beans");
   81       private final Class clazz;
   82       private final BeansWrapper wrapper;
   83       private final Map map = new HashMap();
   84   
   85       StaticModel(Class clazz, BeansWrapper wrapper) throws TemplateModelException
   86       {
   87           this.clazz = clazz;
   88           this.wrapper = wrapper;
   89           populate();
   90       }
   91   
   92       /**
   93        * Returns the field or method named by the <tt>key</tt>
   94        * parameter.
   95        */
   96       public TemplateModel get(String key) throws TemplateModelException
   97       {
   98           Object model = map.get(key);
   99           // Simple method, overloaded method or final field -- these have cached 
  100           // template models
  101           if (model instanceof TemplateModel)
  102               return (TemplateModel) model;
  103           // Non-final field; this must be evaluated on each call.
  104           if (model instanceof Field)
  105           {
  106               try
  107               {
  108                   return wrapper.getOuterIdentity().wrap(((Field) model).get(null));
  109               }
  110               catch (IllegalAccessException e)
  111               {
  112                   throw new TemplateModelException(
  113                       "Illegal access for field " + key + " of class " + clazz.getName());
  114               }
  115           }
  116   
  117           throw new TemplateModelException(
  118               "No such key: " + key + " in class " + clazz.getName());
  119       }
  120   
  121       /**
  122        * Returns true if there is at least one public static
  123        * field or method in the underlying class.
  124        */
  125       public boolean isEmpty()
  126       {
  127           return map.isEmpty();
  128       }
  129   
  130       public int size()
  131       {
  132           return map.size();
  133       }
  134       
  135       public TemplateCollectionModel keys() throws TemplateModelException
  136       {
  137           return (TemplateCollectionModel)wrapper.getOuterIdentity().wrap(map.keySet());
  138       }
  139       
  140       public TemplateCollectionModel values() throws TemplateModelException
  141       {
  142           return (TemplateCollectionModel)wrapper.getOuterIdentity().wrap(map.values());
  143       }
  144   
  145       private void populate() throws TemplateModelException
  146       {
  147           if (!Modifier.isPublic(clazz.getModifiers()))
  148           {
  149               throw new TemplateModelException(
  150                   "Can't wrap the non-public class " + clazz.getName());
  151           }
  152           
  153           if(wrapper.getExposureLevel() == BeansWrapper.EXPOSE_NOTHING)
  154           {
  155               return;
  156           }
  157   
  158           Field[] fields = clazz.getFields();
  159           for (int i = 0; i < fields.length; ++i)
  160           {
  161               Field field = fields[i];
  162               int mod = field.getModifiers();
  163               if (Modifier.isPublic(mod) && Modifier.isStatic(mod))
  164               {
  165                   if (Modifier.isFinal(mod))
  166                       try
  167                       {
  168                           // public static final fields are evaluated once and
  169                           // stored in the map
  170                           map.put(field.getName(), wrapper.getOuterIdentity().wrap(field.get(null)));
  171                       }
  172                       catch (IllegalAccessException e)
  173                       {
  174                           // Intentionally ignored
  175                       }
  176                   else
  177                       // This is a special flagging value: Field in the map means
  178                       // that this is a non-final field, and it must be evaluated
  179                       // on each get() call.
  180                       map.put(field.getName(), field);
  181               }
  182           }
  183           if(wrapper.getExposureLevel() < BeansWrapper.EXPOSE_PROPERTIES_ONLY)
  184           {
  185               Method[] methods = clazz.getMethods();
  186               for (int i = 0; i < methods.length; ++i)
  187               {
  188                   Method method = methods[i];
  189                   int mod = method.getModifiers();
  190                   if (Modifier.isPublic(mod) && Modifier.isStatic(mod) && wrapper.isSafeMethod(method))
  191                   {
  192                       String name = method.getName();
  193                       Object obj = map.get(name);
  194                       if (obj instanceof Method)
  195                       {
  196                           MethodMap methodMap = new MethodMap(name, wrapper);
  197                           methodMap.addMember((Method) obj);
  198                           methodMap.addMember(method);
  199                           map.put(name, methodMap);
  200                       }
  201                       else if(obj instanceof MethodMap)
  202                       {
  203                           MethodMap methodMap = (MethodMap) obj;
  204                           methodMap.addMember(method);
  205                       }
  206                       else
  207                       {
  208                           if(obj != null)
  209                           {
  210                               logger.info("Overwriting value [" + obj + "] for " +
  211                                       " key '" + name + "' with [" + method + 
  212                                       "] in static model for " + clazz.getName());
  213                           }
  214                           map.put(name, method);
  215                       }
  216                   }
  217               }
  218               for (Iterator entries = map.entrySet().iterator(); entries.hasNext();)
  219               {
  220                   Map.Entry entry = (Map.Entry) entries.next();
  221                   Object value = entry.getValue();
  222                   if (value instanceof Method)
  223                   {
  224                       Method method = (Method)value;
  225                       entry.setValue(new SimpleMethodModel(null, method, 
  226                               method.getParameterTypes(), wrapper));
  227                   }
  228                   else if (value instanceof MethodMap)
  229                   {
  230                       entry.setValue(new OverloadedMethodModel(null, 
  231                               (MethodMap)value));
  232                   }
  233               }
  234           }
  235       }
  236   }

Save This Page
Home » freemarker-2.3.13 » freemarker.ext.beans » [javadoc | source]