Ejemplo n.º 1
0
            public Object Invoke(Object o, Object value)
            {
                ArrayList al = new ArrayList();

                if (putKey != null)
                {
                    al.Add(putKey);
                    al.Add(value);
                }
                else
                {
                    al.Add(value);
                }

                return(velMethod.Invoke(o, al.ToArray()));
            }
Ejemplo n.º 2
0
        /// <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);
            }
        }