View Javadoc

    1   /*
    2    * Copyright 2002-2006 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jexl.parser;
    17
    18  import java.lang.reflect.Array;
    19  import java.util.Collection;
    20  import java.util.Map;
    21
    22  import org.apache.commons.jexl.JexlContext;
    23  import org.apache.commons.jexl.util.Introspector;
    24  import org.apache.commons.jexl.util.introspection.Info;
    25  import org.apache.commons.jexl.util.introspection.VelMethod;
    26
    27  /***
    28   * generalized size() function for all classes we can think of.
    29   *
    30   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
    31   * @author <a href="hw@kremvax.net">Mark H. Wilkinson</a>
    32   * @version $Id: ASTSizeFunction.java 398324 2006-04-30 12:20:24Z dion $
    33   */
    34  public class ASTSizeFunction extends SimpleNode {
    35      /***
    36       * Create the node given an id.
    37       *
    38       * @param id node id.
    39       */
    40      public ASTSizeFunction(int id) {
    41          super(id);
    42      }
    43
    44      /***
    45       * Create a node with the given parser and id.
    46       *
    47       * @param p a parser.
    48       * @param id node id.
    49       */
    50      public ASTSizeFunction(Parser p, int id) {
    51          super(p, id);
    52      }
    53
    54      /*** {@inheritDoc} */
    55      public Object jjtAccept(ParserVisitor visitor, Object data) {
    56          return visitor.visit(this, data);
    57      }
    58
    59      /*** {@inheritDoc} */
    60      public Object value(JexlContext jc) throws Exception {
    61          SimpleNode arg = (SimpleNode) jjtGetChild(0);
    62
    63          Object val = arg.value(jc);
    64
    65          if (val == null) {
    66              throw new Exception("size() : null arg");
    67          }
    68
    69          return new Integer(ASTSizeFunction.sizeOf(val));
    70      }
    71
    72      /***
    73       * Calculate the <code>size</code> of various types: Collection, Array, Map, String,
    74       * and anything that has a int size() method.
    75       *
    76       * @param val the object to get the size of.
    77       * @return the size of val
    78       * @throws Exception if the size cannot be determined.
    79       */
    80      public static int sizeOf(Object val) throws Exception {
    81          if (val instanceof Collection) {
    82              return ((Collection) val).size();
    83          } else if (val.getClass().isArray()) {
    84              return Array.getLength(val);
    85          } else if (val instanceof Map) {
    86              return ((Map) val).size();
    87          } else if (val instanceof String) {
    88              return ((String) val).length();
    89          } else {
    90              // check if there is a size method on the object that returns an
    91              // integer
    92              // and if so, just use it
    93              Object[] params = new Object[0];
    94              Info velInfo = new Info("", 1, 1);
    95              VelMethod vm = Introspector.getUberspect().getMethod(val, "size", params, velInfo);
    96              if (vm != null && vm.getReturnType() == Integer.TYPE) {
    97                  Integer result = (Integer) vm.invoke(val, params);
    98                  return result.intValue();
    99              }
    100             throw new Exception("size() : unknown type : " + val.getClass());
    101         }
    102     }
    103
    104 }