Package org.apache.velocity.util.introspection

Examples of org.apache.velocity.util.introspection.VelMethod


         *  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.
         */

        VelMethod method = null;

        Object [] params = new Object[paramCount];

        try
        {
            /*
             * sadly, we do need recalc the values of the args, as this can
             * change from visit to visit
             */

            Class[] paramClasses = new Class[paramCount];
           
            for (int j = 0; j < paramCount; j++)
            {
                params[j] = jjtGetChild(j + 1).value(context);
               
                if (params[j] != null)
                {
                    paramClasses[j] = params[j].getClass();
                }
            }
               
            /*
             *   check the cache
             */
           
            MethodCacheKey mck = new MethodCacheKey(paramClasses);
            IntrospectionCacheData icd =  context.icacheGet( mck );
           
            /*
             *  like ASTIdentifier, if we have cache information, and the
             *  Class of Object o is the same as that in the cache, we are
             *  safe.
             */

            if ( icd != null && (o != null && icd.contextData == o.getClass()) )
            {

                /*
                 * get the method from the cache
                 */

                method = (VelMethod) icd.thingy;
            }
            else
            {
                /*
                 *  otherwise, do the introspection, and then
                 *  cache it
                 */

                for (int j = 0; j < paramCount; j++)
                    params[j] = jjtGetChild(j + 1).value(context);

                method = rsvc.getUberspect().getMethod(o, methodName, params, new Info(context.getCurrentTemplateName(), getLine(), getColumn()));

                if ((method != null) && (o != null))
                {   
                    icd = new IntrospectionCacheData();
                    icd.contextData = o.getClass();
                    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)
                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( RuntimeException e )
        {
            throw e;
        }
        catch( Exception e )
        {
            /*
             *  can come from the doIntropection() also, from Introspector
             */

            log.error("ASTMethod.execute() : exception from introspection", e);
            return null;
        }

        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);
           
            if (obj == null)
            {
                if( method.getReturnType() == Void.TYPE)
                {
                    return "";
                }
            }
           
View Full Code Here


         *  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.
         */

        VelMethod method = null;

        Object [] params = new Object[paramCount];

        try
        {
            /*
             * sadly, we do need recalc the values of the args, as this can
             * change from visit to visit
             */

            final Class[] paramClasses = paramCount > 0 ? new Class[paramCount] : ArrayUtils.EMPTY_CLASS_ARRAY;

            for (int j = 0; j < paramCount; j++)
            {
                params[j] = jjtGetChild(j + 1).value(context);

                if (params[j] != null)
                {
                    paramClasses[j] = params[j].getClass();
                }
            }

            /*
             *   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 o is the same as that in the cache, we are
             *  safe.
             */

            if ( icd != null && (o != null && icd.contextData == o.getClass()) )
            {

                /*
                 * get the method from the cache
                 */

                method = (VelMethod) icd.thingy;
            }
            else
            {
                /*
                 *  otherwise, do the introspection, and then
                 *  cache it
                 */

                method = rsvc.getUberspect().getMethod(o, methodName, params, new Info(context.getCurrentTemplateName(), getLine(), getColumn()));

                if ((method != null) && (o != null))
                {
                    icd = new IntrospectionCacheData();
                    icd.contextData = o.getClass();
                    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)
            {
                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( RuntimeException e )
        {
            throw e;
        }
        catch( Exception e )
        {
            /*
             *  can come from the doIntropection() also, from Introspector
             */

            log.error("ASTMethod.execute() : exception from introspection", e);
            return null;
        }

        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);

            if (obj == null)
            {
                if( method.getReturnType() == Void.TYPE)
                {
                    return "";
                }
            }

View Full Code Here

public class UberspectTestImpl extends UberspectImpl
{

    public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception
    {
        VelMethod method = super.getMethod(obj, methodName, args, i);

        if (method == null)
        {
            if (obj == null)
                throw new UberspectTestException("Can't call method '" + methodName + "' on null object",i);
View Full Code Here

         *  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.
         */

        VelMethod method = null;

        Object [] params = new Object[paramCount];

        try
        {
            /*
             * sadly, we do need recalc the values of the args, as this can
             * change from visit to visit
             */

            final Class[] paramClasses = paramCount > 0 ? new Class[paramCount] : ArrayUtils.EMPTY_CLASS_ARRAY;

            for (int j = 0; j < paramCount; j++)
            {
                params[j] = jjtGetChild(j + 1).value(context);

                if (params[j] != null)
                {
                    paramClasses[j] = params[j].getClass();
                }
            }

            /*
             *   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 o is the same as that in the cache, we are
             *  safe.
             */

            if ( icd != null && (o != null && icd.contextData == o.getClass()) )
            {

                /*
                 * get the method from the cache
                 */

                method = (VelMethod) icd.thingy;
            }
            else
            {
                /*
                 *  otherwise, do the introspection, and then
                 *  cache it
                 */

                for (int j = 0; j < paramCount; j++)
                    params[j] = jjtGetChild(j + 1).value(context);

                method = rsvc.getUberspect().getMethod(o, methodName, params, new Info(context.getCurrentTemplateName(), getLine(), getColumn()));

                if ((method != null) && (o != null))
                {
                    icd = new IntrospectionCacheData();
                    icd.contextData = o.getClass();
                    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)
            {
                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( RuntimeException e )
        {
            throw e;
        }
        catch( Exception e )
        {
            /*
             *  can come from the doIntropection() also, from Introspector
             */

            log.error("ASTMethod.execute() : exception from introspection", e);
            return null;
        }

        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);

            if (obj == null)
            {
                if( method.getReturnType() == Void.TYPE)
                {
                    return "";
                }
            }

View Full Code Here

   */   
  public static VelMethod getMethod(String methodName, Object[] params,
                                    Class[] paramClasses, Object o, InternalContextAdapter context,
                                    SimpleNode node, boolean strictRef)
  {
    VelMethod method = null;
    try
    {
      /*
       * check the cache
       */
 
View Full Code Here

    {
      if (argument instanceof Integer && ((Integer)argument).intValue() < 0)
      {
          // The index value is a negative number, $foo[-1], so we want to actually
          // Index [size - value], so try and call the size method.
          VelMethod method = ClassUtils.getMethod("size", noParams, noTypes,
                             o, context, node, false);
          if (method == null)
          {
              // The object doesn't have a size method, so there is no notion of "at the end"
              throw new VelocityException(
                "A 'size()' method required for negative value "
                 + ((Integer)argument).intValue() + " does not exist for class '"
                 + o.getClass().getName() + "' at " + Log.formatFileString(node));
          }            

          Object size = null;
          try
          {
              size = method.invoke(o, noParams);
          }
          catch (Exception e)
          {
              throw new VelocityException("Error trying to calls the 'size()' method on '"
                + o.getClass().getName() + "' at " + Log.formatFileString(node), e);
View Full Code Here

        // If negative, turn -1 into size - 1
        argument = adjMinusIndexArg(argument, o, context, this);
        Object [] params = {argument};
        Class[] paramClasses = {argument == null ? null : argument.getClass()};

        VelMethod method = ClassUtils.getMethod(methodName, params, paramClasses,
                                                o, context, this, strictRef);

        if (method == null) return null;
   
        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);

            if (obj == null)
            {
                if( method.getReturnType() == Void.TYPE)
                {
                    return "";
                }
            }

View Full Code Here

            {
                paramClasses[j] = params[j].getClass();
            }
        }
           
        VelMethod method = ClassUtils.getMethod(methodName, params, paramClasses,
            o, context, this, strictRef);
        if (method == null) return null;

        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);

            if (obj == null)
            {
                if( method.getReturnType() == Void.TYPE)
                {
                    return "";
                }
            }

View Full Code Here

            Object [] params = {argument, value};
            Class[] paramClasses = {params[0] == null ? null : params[0].getClass(),
                                    params[1] == null ? null : params[1].getClass()};

            String methodName = "set";
            VelMethod method = ClassUtils.getMethod(methodName, params, paramClasses,
                result, context, astIndex, false);
           
            if (method == null)
            {
                // If we can't find a 'set' method, lets try 'put',  This warrents a little
                // investigation performance wise... if the user is using the hash
                // form $foo["blaa"], then it may be expensive to first try and fail on 'set'
                // then go to 'put'?  The problem is that getMethod will try the cache, then
                // perform introspection on 'result' for 'set'
                methodName = "put";
                method = ClassUtils.getMethod(methodName, params, paramClasses,
                      result, context, astIndex, false);
            }  
           
            if (method == null)
            {
                // couldn't find set or put method, so bail
                if (strictRef)
                {
                    throw new VelocityException(
                        "Found neither a 'set' or 'put' method with param types '("
                        + printClass(paramClasses[0]) + "," + printClass(paramClasses[1])
                        + ")' on class '" + result.getClass().getName()
                        + "' at " + Log.formatFileString(astIndex));
                }
                return false;
            }
         
            try
            {
                method.invoke(result, params);
            }
            catch(RuntimeException e)
            {
                // Kludge since invoke throws Exception, pass up Runtimes
                throw e;
View Full Code Here

{

    public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i)
        throws Exception
    {
        VelMethod method = super.getMethod(obj, methodName, args, i);

        if (method == null)
        {
            if (obj == null)
                throw new UberspectTestException("Can't call method '" + methodName + "' on null object",i);
View Full Code Here

TOP

Related Classes of org.apache.velocity.util.introspection.VelMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.