1 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 77 final class StaticModel implements TemplateHashModelEx 78 { 79 private static final Logger logger = Logger.getLogger("freemarker.beans"); 80 private final Class clazz; 81 private final BeansWrapper wrapper; 82 private final Map map = new HashMap (); 83 84 StaticModel(Class clazz, BeansWrapper wrapper) throws IllegalAccessException , TemplateModelException 85 { 86 this.clazz = clazz; 87 this.wrapper = wrapper; 88 populate(); 89 } 90 91 95 public TemplateModel get(String key) throws TemplateModelException 96 { 97 Object model = map.get(key); 98 if (model instanceof TemplateModel) 101 return (TemplateModel) model; 102 if (model instanceof Field ) 104 { 105 try 106 { 107 return wrapper.getOuterIdentity().wrap(((Field ) model).get(null)); 108 } 109 catch (IllegalAccessException e) 110 { 111 throw new TemplateModelException( 112 "Illegal access for field " + key + " of class " + clazz.getName()); 113 } 114 } 115 116 throw new TemplateModelException( 117 "No such key: " + key + " in class " + clazz.getName()); 118 } 119 120 124 public boolean isEmpty() 125 { 126 return map.isEmpty(); 127 } 128 129 public int size() 130 { 131 return map.size(); 132 } 133 134 public TemplateCollectionModel keys() throws TemplateModelException 135 { 136 return (TemplateCollectionModel)wrapper.getOuterIdentity().wrap(map.keySet()); 137 } 138 139 public TemplateCollectionModel values() throws TemplateModelException 140 { 141 return (TemplateCollectionModel)wrapper.getOuterIdentity().wrap(map.values()); 142 } 143 144 private void populate() throws IllegalAccessException , TemplateModelException 145 { 146 if (!Modifier.isPublic(clazz.getModifiers())) 147 { 148 throw new IllegalAccessException ( 149 "Can't wrap the non-public class " + clazz.getName()); 150 } 151 152 if(wrapper.getExposureLevel() == BeansWrapper.EXPOSE_NOTHING) 153 { 154 return; 155 } 156 157 Field [] fields = clazz.getFields(); 158 for (int i = 0; i < fields.length; ++i) 159 { 160 Field field = fields[i]; 161 int mod = field.getModifiers(); 162 if (Modifier.isPublic(mod) && Modifier.isStatic(mod)) 163 { 164 if (Modifier.isFinal(mod)) 165 try 166 { 167 map.put(field.getName(), wrapper.getOuterIdentity().wrap(field.get(null))); 170 } 171 catch (IllegalAccessException e) 172 { 173 } 175 else 176 map.put(field.getName(), field); 180 } 181 } 182 if(wrapper.getExposureLevel() < BeansWrapper.EXPOSE_PROPERTIES_ONLY) 183 { 184 Method [] methods = clazz.getMethods(); 185 for (int i = 0; i < methods.length; ++i) 186 { 187 Method method = methods[i]; 188 int mod = method.getModifiers(); 189 if (Modifier.isPublic(mod) && Modifier.isStatic(mod) && wrapper.isSafeMethod(method)) 190 { 191 String name = method.getName(); 192 Object obj = map.get(name); 193 if (obj instanceof Method ) 194 { 195 MethodMap methodMap = new MethodMap(name); 196 methodMap.addMethod((Method ) obj); 197 methodMap.addMethod(method); 198 map.put(name, methodMap); 199 } 200 else if(obj instanceof MethodMap) 201 { 202 MethodMap methodMap = (MethodMap) obj; 203 methodMap.addMethod(method); 204 } 205 else 206 { 207 if(obj != null) 208 { 209 logger.info("Overwriting value [" + obj + "] for " + 210 " key '" + name + "' with [" + method + 211 "] in static model for " + clazz.getName()); 212 } 213 map.put(name, method); 214 } 215 } 216 } 217 for (Iterator entries = map.entrySet().iterator(); entries.hasNext();) 218 { 219 Map.Entry entry = (Map.Entry ) entries.next(); 220 Object value = entry.getValue(); 221 if (value instanceof Method ) 222 { 223 entry.setValue(new SimpleMethodModel(null, (Method ) value, wrapper)); 224 } 225 else if (value instanceof MethodMap) 226 { 227 entry.setValue(new OverloadedMethodModel(null, (MethodMap) value, wrapper)); 228 } 229 } 230 } 231 } 232 } 233 | Popular Tags |