| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.jexl.util.PropertyExecutor |
|
|
| 1 | /* |
|
| 2 | * Copyright 2000-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.util; |
|
| 17 | ||
| 18 | import java.lang.reflect.InvocationTargetException; |
|
| 19 | ||
| 20 | import org.apache.commons.jexl.util.introspection.Introspector; |
|
| 21 | import org.apache.commons.logging.Log; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Returned the value of object property when executed. |
|
| 25 | * @since 1.0 |
|
| 26 | */ |
|
| 27 | public class PropertyExecutor extends AbstractExecutor { |
|
| 28 | ||
| 29 | /** index of the first character of the property. */ |
|
| 30 | private static final int PROPERTY_START_INDEX = 3; |
|
| 31 | /** The JEXL introspector used. */ |
|
| 32 | 26 | protected Introspector introspector = null; |
| 33 | ||
| 34 | /** The method used. */ |
|
| 35 | 26 | protected String methodUsed = null; |
| 36 | ||
| 37 | /** |
|
| 38 | * Constructor. |
|
| 39 | * |
|
| 40 | * @param r The log for this property executor instance. |
|
| 41 | * @param ispctr The JEXL introspector. |
|
| 42 | * @param clazz The class being examined. |
|
| 43 | * @param property The property being addressed. |
|
| 44 | */ |
|
| 45 | public PropertyExecutor(Log r, Introspector ispctr, |
|
| 46 | 26 | Class clazz, String property) { |
| 47 | 26 | rlog = r; |
| 48 | 26 | introspector = ispctr; |
| 49 | ||
| 50 | 26 | discover(clazz, property); |
| 51 | 26 | } |
| 52 | ||
| 53 | /** |
|
| 54 | * Locate the getter method for this property. |
|
| 55 | * |
|
| 56 | * @param clazz The class being analyzed. |
|
| 57 | * @param property Name of the property. |
|
| 58 | */ |
|
| 59 | protected void discover(Class clazz, String property) { |
|
| 60 | /* |
|
| 61 | * this is gross and linear, but it keeps it straightforward. |
|
| 62 | */ |
|
| 63 | ||
| 64 | try { |
|
| 65 | char c; |
|
| 66 | StringBuffer sb; |
|
| 67 | ||
| 68 | 23 | Object[] params = {}; |
| 69 | ||
| 70 | /* |
|
| 71 | * start with get<property> |
|
| 72 | * this leaves the property name |
|
| 73 | * as is... |
|
| 74 | */ |
|
| 75 | 23 | sb = new StringBuffer("get"); |
| 76 | 23 | sb.append(property); |
| 77 | ||
| 78 | 23 | methodUsed = sb.toString(); |
| 79 | ||
| 80 | 23 | method = introspector.getMethod(clazz, methodUsed, params); |
| 81 | ||
| 82 | 23 | if (method != null) { |
| 83 | 0 | return; |
| 84 | } |
|
| 85 | ||
| 86 | /* |
|
| 87 | * now the convenience, flip the 1st character |
|
| 88 | */ |
|
| 89 | ||
| 90 | 23 | sb = new StringBuffer("get"); |
| 91 | 23 | sb.append(property); |
| 92 | ||
| 93 | 23 | c = sb.charAt(PROPERTY_START_INDEX); |
| 94 | ||
| 95 | 23 | if (Character.isLowerCase(c)) { |
| 96 | 23 | sb.setCharAt(PROPERTY_START_INDEX, Character.toUpperCase(c)); |
| 97 | } else { |
|
| 98 | 0 | sb.setCharAt(PROPERTY_START_INDEX, Character.toLowerCase(c)); |
| 99 | } |
|
| 100 | ||
| 101 | 23 | methodUsed = sb.toString(); |
| 102 | 23 | method = introspector.getMethod(clazz, methodUsed, params); |
| 103 | ||
| 104 | 23 | if (method != null) { |
| 105 | 20 | return; |
| 106 | } |
|
| 107 | ||
| 108 | 3 | } catch (Exception e) { |
| 109 | 0 | rlog.error("PROGRAMMER ERROR : PropertyExector() : " + e); |
| 110 | } |
|
| 111 | 3 | } |
| 112 | ||
| 113 | ||
| 114 | /** |
|
| 115 | * {@inheritDoc} |
|
| 116 | */ |
|
| 117 | public Object execute(Object o) |
|
| 118 | throws IllegalAccessException, InvocationTargetException { |
|
| 119 | 22 | if (method == null) { |
| 120 | 0 | return null; |
| 121 | } |
|
| 122 | ||
| 123 | 22 | return method.invoke(o, null); |
| 124 | } |
|
| 125 | } |
|
| 126 |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |