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. import java.lang.reflect.InvocationTargetException;
  18. import org.apache.commons.jexl.util.introspection.Introspector;
  19. import org.apache.commons.logging.Log;
  20. /**
  21. * Returned the value of object property when executed.
  22. */
  23. public class PropertyExecutor extends AbstractExecutor
  24. {
  25. protected Introspector introspector = null;
  26. protected String methodUsed = null;
  27. public PropertyExecutor(Log r, Introspector ispctr,
  28. Class clazz, String property)
  29. {
  30. rlog = r;
  31. introspector = ispctr;
  32. discover(clazz, property);
  33. }
  34. protected void discover(Class clazz, String property)
  35. {
  36. /*
  37. * this is gross and linear, but it keeps it straightforward.
  38. */
  39. try
  40. {
  41. char c;
  42. StringBuffer sb;
  43. Object[] params = { };
  44. /*
  45. * start with get<property>
  46. * this leaves the property name
  47. * as is...
  48. */
  49. sb = new StringBuffer("get");
  50. sb.append(property);
  51. methodUsed = sb.toString();
  52. method = introspector.getMethod(clazz, methodUsed, params);
  53. if (method != null)
  54. return;
  55. /*
  56. * now the convenience, flip the 1st character
  57. */
  58. sb = new StringBuffer("get");
  59. sb.append(property);
  60. c = sb.charAt(3);
  61. if (Character.isLowerCase(c))
  62. {
  63. sb.setCharAt(3, Character.toUpperCase(c));
  64. }
  65. else
  66. {
  67. sb.setCharAt(3, Character.toLowerCase(c));
  68. }
  69. methodUsed = sb.toString();
  70. method = introspector.getMethod(clazz, methodUsed, params);
  71. if (method != null)
  72. return;
  73. }
  74. catch(Exception e)
  75. {
  76. rlog.error("PROGRAMMER ERROR : PropertyExector() : " + e );
  77. }
  78. }
  79. /**
  80. * Execute method against context.
  81. */
  82. public Object execute(Object o)
  83. throws IllegalAccessException, InvocationTargetException
  84. {
  85. if (method == null)
  86. return null;
  87. return method.invoke(o, null);
  88. }
  89. }