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