1. /*
  2. * Copyright 2002-2004 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. import org.apache.commons.jexl.JexlContext;
  18. import org.apache.commons.jexl.util.Introspector;
  19. import org.apache.commons.jexl.util.introspection.Info;
  20. import org.apache.commons.jexl.util.introspection.VelMethod;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.lang.reflect.Array;
  25. /**
  26. * generalized size() function for all classes we can think of
  27. *
  28. * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  29. * @author <a href="hw@kremvax.net">Mark H. Wilkinson</a>
  30. * @version $Id: ASTSizeFunction.java,v 1.6 2004/08/15 16:01:12 dion Exp $
  31. */
  32. public class ASTSizeFunction extends SimpleNode
  33. {
  34. public ASTSizeFunction(int id)
  35. {
  36. super(id);
  37. }
  38. public ASTSizeFunction(Parser p, int id)
  39. {
  40. super(p, id);
  41. }
  42. /** Accept the visitor. **/
  43. public Object jjtAccept(ParserVisitor visitor, Object data)
  44. {
  45. return visitor.visit(this, data);
  46. }
  47. public Object value(JexlContext jc)
  48. throws Exception
  49. {
  50. SimpleNode arg = (SimpleNode) jjtGetChild(0);
  51. Object val = arg.value(jc);
  52. if (val == null)
  53. {
  54. throw new Exception("size() : null arg");
  55. }
  56. return new Integer(ASTSizeFunction.sizeOf(val));
  57. }
  58. public static int sizeOf(Object val)
  59. throws Exception
  60. {
  61. if (val instanceof List)
  62. {
  63. return ((List)val).size();
  64. }
  65. else if (val.getClass().isArray())
  66. {
  67. return Array.getLength(val);
  68. }
  69. else if (val instanceof Map)
  70. {
  71. return ((Map)val).size();
  72. }
  73. else if (val instanceof String)
  74. {
  75. return ((String)val).length();
  76. }
  77. else if (val instanceof Set)
  78. {
  79. return ((Set)val).size();
  80. }
  81. else {
  82. // check if there is a size method on the object that returns an integer
  83. // and if so, just use it
  84. Object[] params = new Object[0];
  85. Info velInfo = new Info("",1,1);
  86. VelMethod vm = Introspector.getUberspect().getMethod(val, "size", params, velInfo);
  87. if (vm != null && vm.getReturnType() == Integer.TYPE)
  88. {
  89. Integer result = (Integer)vm.invoke(val, params);
  90. return result.intValue();
  91. }
  92. else
  93. {
  94. throw new Exception("size() : unknown type : " + val.getClass());
  95. }
  96. }
  97. }
  98. }