Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.jexl.parser.ASTSizeFunction |
81% |
87% |

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

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.