Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package freemarker.ext.beans;
 
 import java.util.Map;
 
Wraps the static fields and methods of a class in a freemarker.template.TemplateHashModel. Fields are wrapped using BeansWrapper.wrap(java.lang.Object), and methods are wrapped into an appropriate freemarker.template.TemplateMethodModelEx instance. Unfortunately, there is currently no support for bean property-style calls of static methods, similar to that in BeanModel.
 
 final class StaticModel implements TemplateHashModelEx
 {
     private static final Logger LOG = Logger.getLogger("freemarker.beans");
     private final Class clazz;
     private final BeansWrapper wrapper;
     private final Map map = new HashMap();
 
     StaticModel(Class clazzBeansWrapper wrapperthrows TemplateModelException
     {
         this. = clazz;
         this. = wrapper;
         populate();
     }

    
Returns the field or method named by the key parameter.
 
     public TemplateModel get(String keythrows TemplateModelException
     {
         Object model = .get(key);
         // Simple method, overloaded method or final field -- these have cached 
         // template models
         if (model instanceof TemplateModel)
             return (TemplateModelmodel;
         // Non-final field; this must be evaluated on each call.
         if (model instanceof Field)
         {
             try
             {
                 return .getOuterIdentity().wrap(((Fieldmodel).get(null));
             }
             catch (IllegalAccessException e)
             {
                 throw new TemplateModelException(
                     "Illegal access for field " + key + " of class " + .getName());
             }
         }
 
         throw new TemplateModelException(
             "No such key: " + key + " in class " + .getName());
     }

    
Returns true if there is at least one public static field or method in the underlying class.
 
     public boolean isEmpty()
     {
         return .isEmpty();
     }
 
     public int size()
     {
         return .size();
     }
     
     {
    }
    
    {
    }
    private void populate() throws TemplateModelException
    {
        if (!Modifier.isPublic(.getModifiers()))
        {
            throw new TemplateModelException(
                "Can't wrap the non-public class " + .getName());
        }
        
        {
            return;
        }
        Field[] fields = .getFields();
        for (int i = 0; i < fields.length; ++i)
        {
            Field field = fields[i];
            int mod = field.getModifiers();
            if (Modifier.isPublic(mod) && Modifier.isStatic(mod))
            {
                if (Modifier.isFinal(mod))
                    try
                    {
                        // public static final fields are evaluated once and
                        // stored in the map
                        .put(field.getName(), .getOuterIdentity().wrap(field.get(null)));
                    }
                    catch (IllegalAccessException e)
                    {
                        // Intentionally ignored
                    }
                else
                    // This is a special flagging value: Field in the map means
                    // that this is a non-final field, and it must be evaluated
                    // on each get() call.
                    .put(field.getName(), field);
            }
        }
        {
            Method[] methods = .getMethods();
            for (int i = 0; i < methods.length; ++i)
            {
                Method method = methods[i];
                int mod = method.getModifiers();
                if (Modifier.isPublic(mod) && Modifier.isStatic(mod)
                        && .getClassIntrospector().isAllowedToExpose(method))
                {
                    String name = method.getName();
                    Object obj = .get(name);
                    if (obj instanceof Method)
                    {
                        OverloadedMethods overloadedMethods = new OverloadedMethods(.is2321Bugfixed());
                        overloadedMethods.addMethod((Methodobj);
                        overloadedMethods.addMethod(method);
                        .put(nameoverloadedMethods);
                    }
                    else if(obj instanceof OverloadedMethods)
                    {
                        OverloadedMethods overloadedMethods = (OverloadedMethodsobj;
                        overloadedMethods.addMethod(method);
                    }
                    else
                    {
                        if(obj != null)
                        {
                            if (.isInfoEnabled()) {
                                .info("Overwriting value [" + obj + "] for " +
                                        " key '" + name + "' with [" + method + 
                                        "] in static model for " + .getName());
                            }
                        }
                        .put(namemethod);
                    }
                }
            }
            for (Iterator entries = .entrySet().iterator(); entries.hasNext();)
            {
                Map.Entry entry = (Map.Entryentries.next();
                Object value = entry.getValue();
                if (value instanceof Method)
                {
                    Method method = (Method)value;
                    entry.setValue(new SimpleMethodModel(nullmethod
                            method.getParameterTypes(), ));
                }
                else if (value instanceof OverloadedMethods)
                {
                    entry.setValue(new OverloadedMethodsModel(null, (OverloadedMethodsvalue));
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X