static private Object getEntryPoint()

in src/main/java/org/apache/bsf/util/MethodUtils.java [257:373]


  static private Object getEntryPoint(final Class targetClass,
                                      final String methodName,
                                      final Class[] argTypes,
                                      final boolean isStaticReference)
       throws SecurityException, NoSuchMethodException
  {
    // 15.11.1: OBTAIN STARTING CLASS FOR SEARCH
    Object m=null;

    // 15.11.2 DETERMINE ARGUMENT SIGNATURE
    // (Passed in as argTypes array.)

    // Shortcut: If an exact match exists, return it.
    try {
      if(methodName!=null)
        {
          m=targetClass.getMethod (methodName, argTypes);
          if(isStaticReference &&
             !Modifier.isStatic(entryGetModifiers(m)) )
            {
              throw
                new NoSuchMethodException (callToString (targetClass,
                                                         methodName,
                                                         argTypes,
                                                         isStaticReference)+
                                           " resolved to instance " + m);
            }
          return m;
        } else {
        return targetClass.getConstructor (argTypes);
    }

    } catch (final NoSuchMethodException e) {
      // no-args has no alternatives!
      if(argTypes==null || argTypes.length==0)
      {
        throw
          new NoSuchMethodException (callToString (targetClass,
                                                   methodName,
                                                   argTypes,
                                                   isStaticReference)+
                                     " not found.");
      }
      // Else fall through.
    }

    // Well, _that_ didn't work. Time to search for the Most Specific
    // matching function. NOTE that conflicts are possible!

    // 15.11.2.1 ACCESSIBLE: We apparently need to gather from two
    // sources to be sure we have both instance and static methods.
    Object[] methods;
    if(methodName!=null)
      {
        methods=targetClass.getMethods();
      }
    else
      {
        methods=targetClass.getConstructors();
      }
    if(0==methods.length)
      {
        throw new NoSuchMethodException("No methods!");
      }

    final MoreSpecific best=new MoreSpecific();
    for(int i=0;i<methods.length;++i)
      {
        final Object mi=methods[i];
        if (
            // 15.11.2.1 ACCESSIBLE: Method is public.
            Modifier.isPublic(entryGetModifiers(mi))
            &&
            // 15.11.2.1 APPLICABLE: Right method name (or c'tor)
            (methodName==null || entryGetName(mi).equals(methodName) )
            &&
            // 15.11.2.1 APPLICABLE: Parameters match arguments
            areMethodConvertable(entryGetParameterTypes(mi),argTypes)
             ) {
            // 15.11.2.2 MORE SPECIFIC displace less specific.
              best.addItem(mi);
        }
      }

    // May throw NoSuchMethodException; we pass in info needed to
    // create a useful exception
    m=best.getMostSpecific(targetClass,methodName,argTypes,isStaticReference);

    // 15.11.3 APPROPRIATE: Class invocation can call only static
    // methods. Note that the defined order of evaluation permits a
    // call to be resolved to an inappropriate method and then
    // rejected, rather than finding the best of the appropriate
    // methods.
    //
    // Constructors are never static, so we don't test them.
    if(m==null)
      {
        throw new NoSuchMethodException (callToString(targetClass,
                                                      methodName,
                                                      argTypes,
                                                      isStaticReference)+
                                         " -- no signature match");
      }

    if( methodName!=null &&
        isStaticReference &&
        !Modifier.isStatic(entryGetModifiers(m)) )
      {
        throw new NoSuchMethodException (callToString(targetClass,
                                                      methodName,
                                                      argTypes,
                                                      isStaticReference)+
                                         " resolved to instance: "+m);
      }

    return m;
  }