1. /*
  2. * Copyright 2001,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.introspection;
  17. import java.lang.reflect.Method;
  18. import org.apache.commons.logging.Log;
  19. /**
  20. * This basic function of this class is to return a Method
  21. * object for a particular class given the name of a method
  22. * and the parameters to the method in the form of an Object[]
  23. *
  24. * The first time the Introspector sees a
  25. * class it creates a class method map for the
  26. * class in question. Basically the class method map
  27. * is a Hastable where Method objects are keyed by a
  28. * concatenation of the method name and the names of
  29. * classes that make up the parameters.
  30. *
  31. * For example, a method with the following signature:
  32. *
  33. * public void method(String a, StringBuffer b)
  34. *
  35. * would be mapped by the key:
  36. *
  37. * "method" + "java.lang.String" + "java.lang.StringBuffer"
  38. *
  39. * This mapping is performed for all the methods in a class
  40. * and stored for
  41. * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
  42. * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
  43. * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
  44. * @author <a href="mailto:paulo.gaspar@krankikom.de">Paulo Gaspar</a>
  45. * @version $Id: Introspector.java,v 1.6 2004/08/23 13:50:00 dion Exp $
  46. */
  47. public class Introspector extends IntrospectorBase
  48. {
  49. /**
  50. * define a public string so that it can be looked for
  51. * if interested
  52. */
  53. public static final String CACHEDUMP_MSG =
  54. "Introspector : detected classloader change. Dumping cache.";
  55. /**
  56. * our engine runtime services
  57. */
  58. private Log rlog = null;
  59. /**
  60. * Recieves our RuntimeServices object
  61. */
  62. public Introspector(Log logger)
  63. {
  64. this.rlog = logger;
  65. }
  66. /**
  67. * Gets the method defined by <code>name</code> and
  68. * <code>params</code> for the Class <code>c</code>.
  69. *
  70. * @param c Class in which the method search is taking place
  71. * @param name Name of the method being searched for
  72. * @param params An array of Objects (not Classes) that describe the
  73. * the parameters
  74. *
  75. * @return The desired Method object.
  76. */
  77. public Method getMethod(Class c, String name, Object[] params)
  78. throws Exception
  79. {
  80. /*
  81. * just delegate to the base class
  82. */
  83. try
  84. {
  85. return super.getMethod( c, name, params );
  86. }
  87. catch( MethodMap.AmbiguousException ae )
  88. {
  89. /*
  90. * whoops. Ambiguous. Make a nice log message and return null...
  91. */
  92. String msg = "Introspection Error : Ambiguous method invocation "
  93. + name + "( ";
  94. for (int i = 0; i < params.length; i++)
  95. {
  96. if ( i > 0)
  97. msg += ", ";
  98. msg += params[i].getClass().getName();
  99. }
  100. msg = msg + ") for class " + c;
  101. rlog.error( msg );
  102. }
  103. return null;
  104. }
  105. /**
  106. * Clears the classmap and classname
  107. * caches, and logs that we did so
  108. */
  109. protected void clearCache()
  110. {
  111. super.clearCache();
  112. rlog.info( CACHEDUMP_MSG );
  113. }
  114. }