@@ -108,23 +108,27 @@ public static String getTypeSignature(Class<?> type)
108108 return array + signature ;
109109 }
110110
111- static String resolveMethodOverload (HashMap <String , Class <?>> classCache , String className , String methodName , Object [] args ) throws ClassNotFoundException
111+ static HashMap <Class <?>, MethodFinder > methodOverloadsForClass = new HashMap <Class <?>, MethodFinder >();
112+ static ArrayList <Tuple <Method , Integer >> candidates = new ArrayList <Tuple <Method , Integer >>();
113+
114+ static String resolveMethodOverload (Class <?> clazz , String methodName , Object [] args ) throws ClassNotFoundException
112115 {
113- String methodSig = null ;
114- Class <?> clazz = classCache .get (className );
115- if (clazz == null )
116- {
117- clazz = Class .forName (className );
118- }
116+ candidates .clear ();
119117 int argLength = (args != null ) ? args .length : 0 ;
120118
121- ArrayList <Tuple <Method , Integer >> candidates = new ArrayList <Tuple <Method , Integer >>();
122-
123119 Class <?> c = clazz ;
124120 int iterationIndex = 0 ;
125121 while (c != null )
126122 {
127- tryFindMatches (methodName , candidates , args , argLength , c .getDeclaredMethods ());
123+ MethodFinder finder = methodOverloadsForClass .get (c );
124+ if (finder == null )
125+ {
126+ finder = new MethodFinder (c );
127+ methodOverloadsForClass .put (c , finder );
128+ }
129+
130+ ArrayList <Method > matchingMethods = finder .getMatchingMethods (methodName );
131+ tryFindMatches (methodName , candidates , args , argLength , matchingMethods );
128132 if (candidates .size () > iterationIndex && candidates .get (iterationIndex ).y == 0 )
129133 {
130134 // direct matching (distance 0) found
@@ -139,28 +143,18 @@ static String resolveMethodOverload(HashMap<String, Class<?>> classCache, String
139143 {
140144 if (candidates .size () > 1 )
141145 Collections .sort (candidates , distanceComparator );
146+
142147 Method method = candidates .get (0 ).x ;
143- methodSig = getMethodSignature (method .getReturnType (), method .getParameterTypes ());
148+ return getMethodSignature (method .getReturnType (), method .getParameterTypes ());
144149 }
145150
146- return methodSig ;
151+ return null ;
147152 }
148153
149- static void tryFindMatches (String methodName , ArrayList <Tuple <Method , Integer >> candidates , Object [] args , int argLength , Method [] methods )
154+ static void tryFindMatches (String methodName , ArrayList <Tuple <Method , Integer >> candidates , Object [] args , int argLength , ArrayList < Method > methods )
150155 {
151156 for (Method method : methods )
152157 {
153- if (!method .getName ().equals (methodName ))
154- {
155- continue ;
156- }
157-
158- int modifiers = method .getModifiers ();
159- if (!Modifier .isPublic (modifiers ) && !Modifier .isProtected (modifiers ))
160- {
161- continue ;
162- }
163-
164158 Class <?>[] params = method .getParameterTypes ();
165159
166160 boolean success = false ;
@@ -620,4 +614,38 @@ else if (primitiveType.equals(boolean.class))
620614
621615 return success ;
622616 }
623- }
617+
618+ static class MethodFinder {
619+ private Method [] declaredMethods ;
620+ private HashMap <String , ArrayList <Method >> matchingMethods = new HashMap <String , ArrayList <Method >>();
621+ public MethodFinder (Class <?> clazz ) {
622+ this .declaredMethods = clazz .getDeclaredMethods ();
623+ }
624+
625+ public ArrayList <Method > getMatchingMethods (String methodName ) {
626+ ArrayList <Method > matches = this .matchingMethods .get (methodName );
627+ if (matches == null ) {
628+ matches = new ArrayList <Method >();
629+ for (Method method : this .declaredMethods )
630+ {
631+ if (!method .getName ().equals (methodName ))
632+ {
633+ continue ;
634+ }
635+
636+ int modifiers = method .getModifiers ();
637+ if (!Modifier .isPublic (modifiers ) && !Modifier .isProtected (modifiers ))
638+ {
639+ continue ;
640+ }
641+
642+ matches .add (method );
643+ }
644+
645+ matchingMethods .put (methodName , matches );
646+ }
647+
648+ return matches ;
649+ }
650+ }
651+ }
0 commit comments