1 52 53 package freemarker.ext.beans; 54 55 import java.lang.reflect.InvocationTargetException ; 56 import java.lang.reflect.Method ; 57 import java.lang.reflect.Modifier ; 58 import java.util.List ; 59 60 import freemarker.template.SimpleNumber; 61 import freemarker.template.TemplateMethodModelEx; 62 import freemarker.template.TemplateModel; 63 import freemarker.template.TemplateModelException; 64 import freemarker.template.TemplateSequenceModel; 65 import freemarker.template.utility.Collections12; 66 67 75 final class SimpleMethodModel 76 implements 77 TemplateMethodModelEx, 78 TemplateSequenceModel 79 { 80 private final Object object; 81 private final Method method; 82 private final Class [] argTypes; 83 private final BeansWrapper wrapper; 84 85 91 SimpleMethodModel(Object object, Method method, Class [] argTypes, 92 BeansWrapper wrapper) 93 { 94 this.object = object; 95 this.method = method; 96 this.argTypes = argTypes; 97 this.wrapper = wrapper; 98 } 99 100 103 public Object exec(List arguments) 104 throws 105 TemplateModelException 106 { 107 try 108 { 109 Object [] args = wrapper.unwrapArguments(arguments, argTypes); 110 if(args != null) { 111 BeansWrapper.coerceBigDecimals(method, args); 112 } 113 return wrapper.invokeMethod(object, method, args); 114 } 115 catch(Exception e) 116 { 117 while(e instanceof InvocationTargetException ) 118 { 119 Throwable t = ((InvocationTargetException )e).getTargetException(); 120 if(t instanceof Exception ) 121 { 122 e = (Exception )t; 123 } 124 else 125 { 126 break; 127 } 128 } 129 if((method.getModifiers() & Modifier.STATIC) != 0) 130 { 131 throw new TemplateModelException("Method " + method + 132 " threw an exception", e); 133 } 134 else 135 { 136 throw new TemplateModelException("Method " + method + 137 " threw an exception when invoked on " + object, e); 138 } 139 } 140 } 141 142 public TemplateModel get(int index) 143 throws 144 TemplateModelException 145 { 146 return (TemplateModel) exec(Collections12.singletonList(new SimpleNumber(new Integer (index)))); 147 } 148 149 public int size() throws TemplateModelException 150 { 151 throw new TemplateModelException("?size is unsupported for: " + getClass().getName()); 152 } 153 } 154 | Popular Tags |