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.util.Map;
  18. import java.util.Set;
  19. import java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.lang.reflect.Method;
  22. /**
  23. * This basic function of this class is to return a Method
  24. * object for a particular class given the name of a method
  25. * and the parameters to the method in the form of an Object[]
  26. *
  27. * The first time the Introspector sees a
  28. * class it creates a class method map for the
  29. * class in question. Basically the class method map
  30. * is a Hastable where Method objects are keyed by a
  31. * concatenation of the method name and the names of
  32. * classes that make up the parameters.
  33. *
  34. * For example, a method with the following signature:
  35. *
  36. * public void method(String a, StringBuffer b)
  37. *
  38. * would be mapped by the key:
  39. *
  40. * "method" + "java.lang.String" + "java.lang.StringBuffer"
  41. *
  42. * This mapping is performed for all the methods in a class
  43. * and stored for
  44. * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
  45. * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
  46. * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
  47. * @author <a href="mailto:paulo.gaspar@krankikom.de">Paulo Gaspar</a>
  48. * @version $Id: IntrospectorBase.java,v 1.3 2004/02/28 13:45:21 yoavs Exp $
  49. */
  50. public class IntrospectorBase
  51. {
  52. /**
  53. * Holds the method maps for the classes we know about, keyed by
  54. * Class object.
  55. */
  56. protected Map classMethodMaps = new HashMap();
  57. /**
  58. * Holds the qualified class names for the classes
  59. * we hold in the classMethodMaps hash
  60. */
  61. protected Set cachedClassNames = new HashSet();
  62. /**
  63. * Gets the method defined by <code>name</code> and
  64. * <code>params</code> for the Class <code>c</code>.
  65. *
  66. * @param c Class in which the method search is taking place
  67. * @param name Name of the method being searched for
  68. * @param params An array of Objects (not Classes) that describe the
  69. * the parameters
  70. *
  71. * @return The desired Method object.
  72. */
  73. public Method getMethod(Class c, String name, Object[] params)
  74. throws Exception
  75. {
  76. if (c == null)
  77. {
  78. throw new Exception (
  79. "Introspector.getMethod(): Class method key was null: " + name );
  80. }
  81. ClassMap classMap = null;
  82. synchronized(classMethodMaps)
  83. {
  84. classMap = (ClassMap)classMethodMaps.get(c);
  85. /*
  86. * if we don't have this, check to see if we have it
  87. * by name. if so, then we have a classloader change
  88. * so dump our caches.
  89. */
  90. if (classMap == null)
  91. {
  92. if ( cachedClassNames.contains( c.getName() ))
  93. {
  94. /*
  95. * we have a map for a class with same name, but not
  96. * this class we are looking at. This implies a
  97. * classloader change, so dump
  98. */
  99. clearCache();
  100. }
  101. classMap = createClassMap(c);
  102. }
  103. }
  104. return classMap.findMethod(name, params);
  105. }
  106. /**
  107. * Creates a class map for specific class and registers it in the
  108. * cache. Also adds the qualified name to the name->class map
  109. * for later Classloader change detection.
  110. */
  111. protected ClassMap createClassMap(Class c)
  112. {
  113. ClassMap classMap = new ClassMap( c );
  114. classMethodMaps.put(c, classMap);
  115. cachedClassNames.add( c.getName() );
  116. return classMap;
  117. }
  118. /**
  119. * Clears the classmap and classname
  120. * caches
  121. */
  122. protected void clearCache()
  123. {
  124. /*
  125. * since we are synchronizing on this
  126. * object, we have to clear it rather than
  127. * just dump it.
  128. */
  129. classMethodMaps.clear();
  130. /*
  131. * for speed, we can just make a new one
  132. * and let the old one be GC'd
  133. */
  134. cachedClassNames = new HashSet();
  135. }
  136. }