1 package jfun.yan.xml.nuts; 2 3 import java.lang.reflect.Method ; 4 5 import jfun.yan.Component; 6 import jfun.yan.Components; 7 import jfun.yan.Functions; 8 import jfun.yan.util.ReflectionUtil; 9 15 public class MethodNut extends ArgumentsAndPropertiesNut 16 implements LifecycleDeclaration{ 17 private Class hostclass; 18 private Component cc; 19 private String method_name; 20 private boolean private_access = false; 21 22 public void add(Component cn){ 23 checkDuplicate("component", cc); 24 this.cc = cn; 25 } 26 27 53 public boolean isPrivate_access() { 54 return private_access; 55 } 56 public void setPrivate_access(boolean private_access) { 57 this.private_access = private_access; 58 } 59 public void setClass(Class type){ 60 this.hostclass = type; 62 } 63 public void setComponent(Component c){ 64 this.cc = c; 66 } 67 public void setName(String name){ 68 method_name = name.trim(); 69 } 70 public String getName(){ 71 return method_name; 72 } 73 private String initializer; 74 private String starter; 75 private String stopper; 76 private String disposer; 77 public String getInitializer() { 78 return initializer; 79 } 80 81 public void setInitializer(String initializer) { 82 this.initializer = initializer; 83 } 84 85 public String getStarter() { 86 return starter; 87 } 88 89 public void setStarter(String starter) { 90 this.starter = starter; 91 } 92 93 public String getStopper() { 94 return stopper; 95 } 96 97 public void setStopper(String stopper) { 98 this.stopper = stopper; 99 } 100 101 public String getDisposer() { 102 return disposer; 103 } 104 105 public void setDisposer(String disposer) { 106 this.disposer = disposer; 107 } 108 private Method lookupMethod(Class [] param_types){ 109 if(param_types==null){ 110 final int argcount = getMaxArgsCount(); 111 if(argcount <0){ 112 if(this.getParameterAutowireMode()==null){ 113 return ReflectionUtil.getMethod(hostclass, method_name, 114 null, private_access); 115 } 116 else return ReflectionUtil.getMethod(hostclass, method_name, 117 private_access); 118 } 119 else{ 120 return ReflectionUtil.getMethod(hostclass, method_name, 121 argcount, private_access); 122 } 123 } 124 else{ 125 return ReflectionUtil.getMethod(hostclass, method_name, 126 param_types, private_access); 127 } 128 } 129 private Component lookupComponent(Class [] param_types){ 130 if(param_types==null){ 131 final int argcount = getMaxArgsCount(); 132 if(argcount<0){ 133 return cc.method(method_name, 134 private_access); 135 } 136 else{ 137 return Components.bindMethod(cc, method_name, 138 argcount, private_access); 139 } 140 } 141 else{ 142 return cc.method(method_name, param_types, private_access); 143 } 144 } 145 private Method lookupStaticMethod(Class [] param_types){ 146 if(param_types==null){ 147 final int argcount = getMaxArgsCount(); 148 if(argcount < 0){ 149 return ReflectionUtil.getStaticMethod(hostclass, method_name, 150 private_access); 151 } 152 else{ 153 return ReflectionUtil.getStaticMethod(hostclass, method_name, 154 argcount, private_access); 155 } 156 } 157 else{ 158 return ReflectionUtil.getMethod(hostclass, method_name, param_types, 159 private_access); 160 } 161 } 162 166 protected Component evaluateNoLifecycle(){ 167 checkMandatory("class", hostclass, "component", cc); 168 checkMandatory("name", method_name); 169 Component result; 170 final Class [] param_types = getParameterTypes(); 171 if(cc != null){ 172 if(hostclass != null){ 173 final Method mtd = lookupMethod(param_types); 174 result = cc.method(mtd); 175 } 176 else{ 177 result = lookupComponent(param_types); 178 184 } 185 } 186 else{ 187 final Method mtd = lookupStaticMethod(param_types); 188 result = Components.fun(Functions.static_method(mtd)); 189 192 } 193 return decorateComponent(result); 194 } 196 public Component eval(){ 197 return Util.wrapLifecycle(evaluateNoLifecycle(), this); 198 } 199 } 200 | Popular Tags |