Selector.java
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:3k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. // Swarm library. Copyright (C) 1999-2000 Swarm Development Group.
  2. // This program is free software; you can redistribute it and/or
  3. // modify it under the terms of the GNU General Public License as
  4. // published by the Free Software Foundation; either version 2 of the
  5. // License, or (at your option) any later version.
  6. //
  7. // This program is distributed in the hope that it will be useful, but
  8. // WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10. // General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU General Public License
  13. // along with this program; if not, write to the Free Software
  14. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  15. // USA
  16. // 
  17. // The Swarm Development Group can be reached via our website at:
  18. // http://www.swarm.org/
  19. package swarm;
  20. import swarm.NonUniqueMethodSignatureException;
  21. import swarm.SignatureNotFoundException;
  22. import java.lang.reflect.Method;
  23. public class Selector {
  24.   String signature;
  25.   Class retType;
  26.   Class [] argTypes;
  27.   boolean objcFlag;
  28.   String typeSignature;
  29.   
  30.     public Selector (Class theClass, String methodName, boolean theObjcFlag) throws NonUniqueMethodSignatureException, SignatureNotFoundException {
  31.     
  32.     super ();
  33.     {
  34.       Method[] methods = theClass.getMethods ();
  35.       int matchCount = 0;
  36.       objcFlag = theObjcFlag;
  37.       if (objcFlag)
  38.         {
  39.           methodName = methodName.replace (':', '$');
  40.           if (methodName.endsWith ("$"))
  41.             methodName = methodName.substring (0, methodName.length () - 1);
  42.         }
  43.       
  44.       for (int mi = 0; mi < methods.length; mi++)
  45.         {
  46.           if (methods[mi].getName ().compareTo (methodName) == 0)
  47.             {
  48.               if (matchCount > 0) {
  49.                   int i;
  50.                   Class []margTypes = methods[mi].getParameterTypes ();
  51.                   
  52.                   // Skip over same-named, but different-arity methods
  53.                   // This comes up in ProbeMaps for base classes, e.g. wait
  54.                   if (margTypes.length == argTypes.length)
  55.                       {
  56.                           for (i = 0; i < argTypes.length; i++)
  57.                             if (argTypes[i] != margTypes[i]) {
  58.                                 System.err.println (signature + " arg: " + i + ": " + argTypes[i] + " != " + margTypes[i]);
  59.                                 throw new NonUniqueMethodSignatureException ();
  60.                             }
  61.                         if (retType != methods[mi].getReturnType ())
  62.                             System.err.println (signature + " retType: "+ retType + " != " + methods[mi].getReturnType ());
  63.                       }
  64.               }
  65.               signature = methodName;
  66.               retType = methods[mi].getReturnType ();
  67.               argTypes = methods[mi].getParameterTypes ();
  68.               matchCount++;
  69.             }
  70.         }
  71.       if (matchCount == 0)
  72.         throw new SignatureNotFoundException ();
  73.     }
  74.   }
  75.   public int hashCode () {
  76.     return signature.hashCode ();
  77.   }
  78.   public boolean equals (Object obj) {
  79.     if (obj.getClass () == this.getClass ())
  80.       return ((Selector) obj).signature.equals (signature);
  81.     else
  82.       return false;
  83.   }
  84.   public String getSignature () {
  85.     return signature;
  86.   }
  87. }