Java SQL Type matchMethods(String name, Method[] methods, Class[] spt, int mrs, boolean exact)

Here you can find the source of matchMethods(String name, Method[] methods, Class[] spt, int mrs, boolean exact)

Description

Matches the given method name and it's parameter types to each candidate method in a given list of candidate methods.

License

Apache License

Parameter

Parameter Description
name Name of the method
methods Array of Method objects
spt Source parameter list
mrs Max result sets value
exact Exact match flag

Declaration

private static ArrayList matchMethods(String name, Method[] methods,
        Class[] spt, int mrs, boolean exact) 

Method Source Code

//package com.java2s;
/**********************************************************************
 // @@@ START COPYRIGHT @@@/*from  w  w  w .j av  a2 s  .co m*/
 //
 // (C) Copyright 1997-2014 Hewlett-Packard Development Company, L.P.
 //
 //  Licensed under the Apache License, Version 2.0 (the "License");
 //  you may not use this file except in compliance with the License.
 //  You may obtain a copy of the License at
 //
 //      http://www.apache.org/licenses/LICENSE-2.0
 //
 //  Unless required by applicable law or agreed to in writing, software
 //  distributed under the License is distributed on an "AS IS" BASIS,
 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 //  See the License for the specific language governing permissions and
 //  limitations under the License.
 //
 // @@@ END COPYRIGHT @@@
 **********************************************************************/

import java.lang.reflect.Method;

import java.util.ArrayList;

public class Main {
    /**
     * Matches the given method name and it's parameter types to each candidate 
     * method in a given list of candidate methods. Any matched methods are stored in a 
     * list to be returned to the caller.
     *
     * The criteria for inclusion of a candidate method into the list are:
     *
     * Candidate method name must match the given method name.
     * Candidate method SQL parameter names must match the given methods parameter names.
     *
     * AND
     * 
     * If the user entered an optional signature in CREATE PROCEDURE EXTERNAL NAME
     * and the candidate method does not have any trailing parameters then add this candidate
     * method to the list.
     * 
     * OR
     *
     * If the user specified the parameters in the CREATE PROCEDURE SQL parameters, the 
     * candidate method has trailing parameters and the user specified DYNAMIC RESULT SETS > 0 
     * then add the method to the list.  
     *
     * OR
     *   
     * If the user specified the parameters in the CREATE PROCEDURE SQL parameters, and the 
     * candidate method does not have any trailing parameters then add the method to the list.  
     *
     * 
     * @param  name    Name of the method 
     * @param  methods Array of Method objects
     * @param  spt     Source parameter list
     * @param  mrs     Max result sets value
     * @param  exact   Exact match flag
     * @returns        A list of candidate method objects
     **/
    private static ArrayList matchMethods(String name, Method[] methods,
            Class[] spt, int mrs, boolean exact) {
        ArrayList list = new ArrayList();

        for (int i = 0; i < methods.length; i++) {
            Method m = methods[i];
            Class[] tpt = m.getParameterTypes();

            boolean trailingParams = (tpt.length > spt.length);

            if (name.equals(m.getName()))//Match method names
            {
                if (matchParams(spt, tpt))//Match parameter type names
                {
                    if (exact)//Signature was specified in CREATE PROCEDURE EXTERNAL NAME
                    {
                        if (trailingParams)//Method has trailing params 
                        {
                            //Do nothing - For exact signatures we're not
                            //interested in methods containing trailing params.
                        } else {
                            //We're not concerned with the value of DYNAMIC RESULT SETS
                            //because the signature is an exact match.
                            list.add(m);
                        }
                    } else//Signature was specified in CREATE PROCEDURE SQL parameters
                    {
                        if (trailingParams)//Method has trailing params
                        {
                            if (mrs > 0)//We're interested in methods containing trailing result sets
                            {
                                //Trailing params must all be of type java.sql.ResultSet
                                if (matchTrailingParams(spt.length,
                                        tpt.length, tpt))
                                    list.add(m);
                            } else {
                                //Do nothing - We're not interested in methods that
                                //contain trailing result sets
                            }
                        } else//Method does not have trailing params
                        {
                            if (mrs > 0) {
                                //Whenever DNR > 0 a java.sql.ResultSet must
                                //be present in the actual Java method signature. 
                            } else
                                list.add(m);
                        }
                    }//endIf exact
                }//endIf match params
            }//end method names match
        }//end for loop

        return list;

    }

    /**
     * Compares parameter type names of the source array with that
     * of the target array. If the length of the target array 
     * is less than the source array then matching is not performed. 
     * Matching begins with index 0 of each array for the 
     * length of the source array. If all the names of the source 
     * array match those of the target array then return true 
     * otherwise return false. 
     * @param  spt     Array of Method parameter objects 
     * @param  tpt     Array of Method parameter objects
     * @returns        True if names match otherwise false
     **/
    private static boolean matchParams(Class[] spt, Class[] tpt) {
        if (tpt.length < spt.length) //not enough params in target
            return false;

        for (int i = 0; i < spt.length; i++) {
            if (!(spt[i].getName()).equals(tpt[i].getName()))
                return false;//SQL param name mismatch
        }
        return true;
    }

    /**
     * Compares remaining parameter type names of the target array with the
     * string "[Ljava.sql.ResultSet". If the length of the target array 
     * is less than or equal to the source array then matching is not performed.
     * Matching begins sptLen deep into the target array. 
     * If all the names match then return true otherwise false.
     * @param  sptLen     Length of the source parameter array 
     * @param  tptLen     Length of the target parameter array
     * @param  tpt        The target parameter array
     * @returns           True if trailing params are of type ResultSet 
     *                    otherwise false
     **/

    private static boolean matchTrailingParams(int sptLen, int tptLen,
            Class[] tpt) {
        final String RESULT_SET = "[Ljava.sql.ResultSet;";

        if (tptLen <= sptLen)//No trailing params
            return false;

        for (int i = sptLen; i < tptLen; i++) {
            if (!(tpt[i].getName()).equals(RESULT_SET))
                return false;//Trailing param not type ResultSet
        }
        return true;
    }
}

Related

  1. loadTestDataForEveryType(Connection conn)
  2. mapECLtype2SQLtype(String ecltype)
  3. mapType(Class javaType)
  4. mapXSDTypeName2SQLtype(String xsdtype)
  5. marshalCurlType(String javaType, boolean isAllowNull, boolean generateGenerics)
  6. nativeSQL(String sql, boolean noBackslashEscapes)
  7. newArrayOfType(int typeCode, int size)
  8. newInstance(Class clazz, Object... params)
  9. parse(String txt, int level)