Search code examples
javareflectionbinary-searchcomparatorcomparable

Binary Search Method Array Java


I am trying to efficiently search weather a subclass implements a method that I have the name of in a string called _szMethodName. I can get an array of all the methods that the subclass implements by doing Method[] _arrClassMethodsList = class.getMethods();. I can then compare the name of the method to the stringName of the function that I am looking for to determine weather or not the class implements that particular method. Currently I have this working in a for loop but this gets slow as the subclass grows.

For Loop implementation:

for (Method method : class.getMethods()){
       if(method.getName().equals(_szMethodName)){
          //method exists in subclass
          break;
      }
}

The array of methods from class.getMethods() is sorted alphabetically.(Only in Java >=7). I was hoping that I could leverage this by using a binary-search or some other optimization on the array instead of using the for loop. However, I have not yet been able to figure out how to implement Java's binary search function on the array. I have tried using comparator or and comparable but have not yet had success. My most recent implementation of comparator is below but has errors that I have not yet been able to resolve.

Current attempt using comparator:

Comparator<Method> c = new Comparator <Method>() {
    public int compare(Method method, String string) {
        return method.getName().compareTo(string);
    }
};

Method[] _arrClassMethodsList = class.getMethods();
int index = Arrays.binarySearch(_arrClassMethodsList, _szMethodName, c);

Any help or examples on how to get this working would be greatly appreciated. Thanks!


Solution

  • It would appear that the only real way to do this is to re-implement Binary search pulling out the method name during the search. My final implementation is below. Thanks for all the help everyone.

    public final Method NOT_FOUND = null;
    private Method findMethodInDelegateClassWithParameters (String _szMethodName)
    {
        @SuppressWarnings("rawtypes")
        Class _cDelegateClass = m_oDelegate.getClass();
    
        //Get and sort array for binary search if not done, ensure methods are alphabetical before Java 7 
        if (_arrSortedMethods==null){
            _arrSortedMethods = _cDelegateClass.getMethods();   
            Arrays.sort(_arrSortedMethods, new Comparator<Method>() {
                   public int compare(Method m1, Method m2) {
                       return m1.getName().compareTo(m2.getName());
                   }
            });
        }
    
        return binarySearchForMethodNamed(_arrSortedMethods, _szMethodName);
    }
    
    public Method binarySearchForMethodNamed(Method[] _arrMethods, String _szMethodName) {
        int left = 0;
        int right = _arrMethods.length - 1;
        return binarySearchMethods(_arrMethods, _szMethodName, left, right);
        }
    
    private Method binarySearchMethods(Method[] _arrMethods, String _szMethodName, int left, int right) {
        if (right < left) {
                return NOT_FOUND;
        }
    
        int mid = (left + right) >>> 1;
        String _szArrayMethodName = _arrMethods[mid].getName();
        if (_szMethodName.compareTo(_szArrayMethodName)>0) {
                return binarySearchMethods(_arrMethods, _szMethodName, mid + 1, right);
        } else if (_szMethodName.compareTo(_szArrayMethodName)<0) {
                return binarySearchMethods(_arrMethods, _szMethodName, left, mid - 1);
        } else {
                return _arrMethods[mid];
        }               
    }