001: /*
002: * Copyright 2002-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jexl.parser;
017:
018: import java.lang.reflect.Array;
019: import java.util.Collection;
020: import java.util.Map;
021:
022: import org.apache.commons.jexl.JexlContext;
023: import org.apache.commons.jexl.util.Introspector;
024: import org.apache.commons.jexl.util.introspection.Info;
025: import org.apache.commons.jexl.util.introspection.VelMethod;
026:
027: /**
028: * generalized size() function for all classes we can think of.
029: *
030: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
031: * @author <a href="hw@kremvax.net">Mark H. Wilkinson</a>
032: * @version $Id: ASTSizeFunction.java 398324 2006-04-30 12:20:24Z dion $
033: */
034: public class ASTSizeFunction extends SimpleNode {
035: /**
036: * Create the node given an id.
037: *
038: * @param id node id.
039: */
040: public ASTSizeFunction(int id) {
041: super (id);
042: }
043:
044: /**
045: * Create a node with the given parser and id.
046: *
047: * @param p a parser.
048: * @param id node id.
049: */
050: public ASTSizeFunction(Parser p, int id) {
051: super (p, id);
052: }
053:
054: /** {@inheritDoc} */
055: public Object jjtAccept(ParserVisitor visitor, Object data) {
056: return visitor.visit(this , data);
057: }
058:
059: /** {@inheritDoc} */
060: public Object value(JexlContext jc) throws Exception {
061: SimpleNode arg = (SimpleNode) jjtGetChild(0);
062:
063: Object val = arg.value(jc);
064:
065: if (val == null) {
066: throw new Exception("size() : null arg");
067: }
068:
069: return new Integer(ASTSizeFunction.sizeOf(val));
070: }
071:
072: /**
073: * Calculate the <code>size</code> of various types: Collection, Array, Map, String,
074: * and anything that has a int size() method.
075: *
076: * @param val the object to get the size of.
077: * @return the size of val
078: * @throws Exception if the size cannot be determined.
079: */
080: public static int sizeOf(Object val) throws Exception {
081: if (val instanceof Collection) {
082: return ((Collection) val).size();
083: } else if (val.getClass().isArray()) {
084: return Array.getLength(val);
085: } else if (val instanceof Map) {
086: return ((Map) val).size();
087: } else if (val instanceof String) {
088: return ((String) val).length();
089: } else {
090: // check if there is a size method on the object that returns an
091: // integer
092: // and if so, just use it
093: Object[] params = new Object[0];
094: Info velInfo = new Info("", 1, 1);
095: VelMethod vm = Introspector.getUberspect().getMethod(val,
096: "size", params, velInfo);
097: if (vm != null && vm.getReturnType() == Integer.TYPE) {
098: Integer result = (Integer) vm.invoke(val, params);
099: return result.intValue();
100: }
101: throw new Exception("size() : unknown type : "
102: + val.getClass());
103: }
104: }
105:
106: }
|