public IVelPropertySet GetPropertySet(object obj, string identifier, object arg, Info i) { Type type = obj.GetType(); IVelMethod velMethod = null; IVelPropertySet result; try { object[] args = new object[] { arg }; try { velMethod = this.GetMethod(obj, "set" + identifier, args, i); if (velMethod == null) { throw new MethodAccessException(); } } catch (MethodAccessException) { StringBuilder stringBuilder = new StringBuilder("set"); stringBuilder.Append(identifier); if (char.IsLower(stringBuilder[3])) { stringBuilder[3] = char.ToUpper(stringBuilder[3]); } else { stringBuilder[3] = char.ToLower(stringBuilder[3]); } velMethod = this.GetMethod(obj, stringBuilder.ToString(), args, i); if (velMethod == null) { throw; } } } catch (MethodAccessException) { if (typeof(IDictionary).IsAssignableFrom(type)) { object[] args = new object[] { new object(), new object() }; velMethod = this.GetMethod(obj, "Add", args, i); if (velMethod != null) { result = new UberspectImpl.VelSetterImpl(velMethod, identifier); return(result); } } } result = ((velMethod != null) ? new UberspectImpl.VelSetterImpl(velMethod) : null); return(result); }
public VelSetterImpl(IVelMethod velMethod, string key) { this.velMethod = velMethod; putKey = key; }
public VelSetterImpl(IVelMethod velMethod) { this.velMethod = velMethod; }
/// <summary> /// Property setter. /// </summary> public IVelPropertySet GetPropertySet(Object obj, String identifier, Object arg, Info i) { Type type = obj.GetType(); IVelMethod method = null; try { /* * first, we introspect for the set<identifier> setter method */ Object[] parameters = new Object[] { arg }; try { method = GetMethod(obj, string.Format("set{0}", identifier), parameters, i); if (method == null) { throw new MethodAccessException(); } } catch (MethodAccessException) { StringBuilder sb = new StringBuilder("set"); sb.Append(identifier); if (Char.IsLower(sb[3])) { sb[3] = Char.ToUpper(sb[3]); } else { sb[3] = Char.ToLower(sb[3]); } method = GetMethod(obj, sb.ToString(), parameters, i); if (method == null) { throw; } } } catch (MethodAccessException) { // right now, we only support the IDictionary interface if (typeof(IDictionary).IsAssignableFrom(type)) { Object[] parameters = new Object[] { new Object(), new Object() }; method = GetMethod(obj, "Add", parameters, i); if (method != null) { return(new VelSetterImpl(method, identifier)); } } } return((method != null) ? new VelSetterImpl(method) : null); }
public VelSetterImpl(IVelMethod velmethod, string key) { vm = velmethod; putKey = key; }
public VelSetterImpl(IVelMethod velmethod) { vm = velmethod; }
public VelSetterImpl(IVelMethod velmethod, string key) { this.vm = velmethod; this.putKey = key; }
/// <summary> invokes the method. Returns null if a problem, the /// parameters return if the method returns something, or /// an empty string "" if the method returns void /// </summary> /// <param name="instance"> /// </param> /// <param name="context"> /// </param> /// <returns> Result or null. /// </returns> /// <throws> MethodInvocationException </throws> public override object Execute(object o, IInternalContextAdapter context) { /* * new strategy (strategery!) for introspection. Since we want * to be thread- as well as context-safe, we *must* do it now, * at execution time. There can be no in-node caching, * but if we are careful, we can do it in the context. */ IVelMethod method = null; object[] params_Renamed = new object[paramCount]; try { /* * sadly, we do need recalc the values of the args, as this can * change from visit to visit */ System.Type[] paramClasses = paramCount > 0 ? new System.Type[paramCount] : new System.Collections.Generic.List <Type>().ToArray(); for (int j = 0; j < paramCount; j++) { params_Renamed[j] = GetChild(j + 1).Value(context); if (params_Renamed[j] != null) { paramClasses[j] = params_Renamed[j].GetType(); } } /* * check the cache */ MethodCacheKey mck = new MethodCacheKey(methodName, paramClasses); IntrospectionCacheData icd = context.ICacheGet(mck); /* * like ASTIdentifier, if we have cache information, and the * Class of Object instance is the same as that in the cache, we are * safe. */ if (icd != null && (o != null && icd.ContextData == o.GetType())) { /* * Get the method from the cache */ method = (IVelMethod)icd.Thingy; } else { /* * otherwise, do the introspection, and then * cache it */ method = rsvc.Uberspect.GetMethod(o, methodName, params_Renamed, new Info(TemplateName, Line, Column)); if ((method != null) && (o != null)) { icd = new IntrospectionCacheData(); icd.ContextData = o.GetType(); icd.Thingy = method; context.ICachePut(mck, icd); } } /* * if we still haven't gotten the method, either we are calling * a method that doesn't exist (which is fine...) or I screwed * it up. */ if (method == null) { if (strictRef) { // Create a parameter list for the exception Error message System.Text.StringBuilder plist = new System.Text.StringBuilder(); for (int i = 0; i < params_Renamed.Length; i++) { System.Type param = paramClasses[i]; plist.Append(param == null ? "null" : param.FullName); if (i < params_Renamed.Length - 1) { plist.Append(", "); } } throw new MethodInvocationException("Object '" + o.GetType().FullName + "' does not contain method " + methodName + "(" + plist + ")", null, methodName, uberInfo.TemplateName, uberInfo.Line, uberInfo.Column); } else { return(null); } } } catch (MethodInvocationException mie) { /* * this can come from the doIntrospection(), as the arg values * are evaluated to find the right method signature. We just * want to propogate it here, not do anything fancy */ throw mie; } /** * pass through application level runtime exceptions */ catch (System.SystemException e) { throw e; } catch (System.Exception e) { /* * can come from the doIntropection() also, from Introspector */ string msg = "ASTMethod.Execute() : exception from introspection"; log.Error(msg, e); throw new VelocityException(msg, e); } try { /* * Get the returned object. It may be null, and that is * valid for something declared with a void return type. * Since the caller is expecting something to be returned, * as long as things are peachy, we can return an empty * String so ASTReference() correctly figures out that * all is well. */ object obj = method.Invoke(o, params_Renamed); if (obj == null) { if (method.ReturnType == System.Type.GetType("System.Void")) { return(""); } } return(obj); } catch (System.Reflection.TargetInvocationException ite) { return(HandleInvocationException(o, context, ite.GetBaseException())); } /** Can also be thrown by method invocation **/ catch (System.ArgumentException t) { return(HandleInvocationException(o, context, t)); } /** * pass through application level runtime exceptions */ catch (System.SystemException e) { throw e; } catch (System.Exception e) { string msg = "ASTMethod.Execute() : exception invoking method '" + methodName + "' in " + o.GetType(); log.Error(msg, e); throw new VelocityException(msg, e); } }
public VelSetterImpl(IVelMethod velMethod, string key) { this.velMethod = velMethod; putKey = key; }
public VelSetterImpl(IVelMethod velMethod) { this.velMethod = velMethod; }
/// <summary> {@inheritDoc} /// /// </summary> /// <seealso cref="org.apache.velocity.util.introspection.Uberspect.getMethod(java.lang.Object, java.lang.String,"> /// java.lang.Object[], org.apache.velocity.util.introspection.Info) /// </seealso> //@Override public override IVelMethod GetMethod(object obj, string methodName, object[] args, Info i) { IVelMethod method = leftUberspect.GetMethod(obj, methodName, args, i); return(method != null ? method : rightUberspect.GetMethod(obj, methodName, args, i)); }