Search in sources :

Example 1 with AmbiguousMethodException

use of com.axway.ats.core.reflect.AmbiguousMethodException in project ats-framework by Axway.

the class ActionMethodContainer method add.

/**
     * Add a new action method
     *
     * @param actionMethod the action method to add
     * @return
     * @throws ActionAlreadyDefinedException if the new action has the exact same arguments of an existing one
     */
public boolean add(ActionMethod actionMethod) throws ActionAlreadyDefinedException {
    // if there is at least one method check for ambiguity
    if (actionMethods.size() > 0) {
        Class<?>[] newMethodParamTypes = actionMethod.getMethod().getParameterTypes();
        //find the action method implementation based on the arguments
        MethodFinder methodFinder = new MethodFinder("methods for action " + actionName, getMethods(), customComparisonRules);
        //get the most specific method which accepts these parameters
        try {
            Method mostSpecificMethod = methodFinder.findMethod(newMethodParamTypes);
            //then we have ambiguity, which should not be allowed
            if (Arrays.equals(newMethodParamTypes, mostSpecificMethod.getParameterTypes())) {
                throw new ActionAlreadyDefinedException(actionName, componentName, mostSpecificMethod);
            }
        } catch (NoSuchMethodException e) {
        //method which accepts there argument does not exist, we can safely add it
        } catch (AmbiguousMethodException e) {
            //unless they are exactly the same, which we don't allow to happen
            throw new RuntimeException("AmbiguousMethodException caught while searching for action methods");
        }
    }
    //add the action method
    return actionMethods.add(actionMethod);
}
Also used : MethodFinder(com.axway.ats.core.reflect.MethodFinder) Method(java.lang.reflect.Method) ActionAlreadyDefinedException(com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException) AmbiguousMethodException(com.axway.ats.core.reflect.AmbiguousMethodException)

Example 2 with AmbiguousMethodException

use of com.axway.ats.core.reflect.AmbiguousMethodException in project ats-framework by Axway.

the class DatabaseProviderFactory method loadDbConnection.

private static DbConnection loadDbConnection(String className, String dbType, String dbHost, String dbName, String dbUser, String dbPass, int dbPort, Map<String, Object> customProperties) {
    Class<?> dbConnectionClass = null;
    // load db connection class
    try {
        dbConnectionClass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new DbException("Unable to get database provider for type '" + dbType + "'", e);
    }
    // load db connection constructor
    Constructor<?> constructor = null;
    try {
        constructor = new MethodFinder(dbConnectionClass).findConstructor(new Class[] { String.class, String.class, String.class, String.class, Map.class });
    } catch (NoSuchMethodException | AmbiguousMethodException e) {
        throw new DbException("Unable to get database provider for type '" + dbType + "'", e);
    }
    // create DbConnection class for this dbType
    DbConnection dbConnection = null;
    try {
        dbConnection = (DbConnection) constructor.newInstance(new Object[] { dbHost, dbName, dbUser, dbPass, customProperties });
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new DbException("Unable to get database provider for type '" + dbType + "'", e);
    }
    return dbConnection;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) MethodFinder(com.axway.ats.core.reflect.MethodFinder) HashMap(java.util.HashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) AmbiguousMethodException(com.axway.ats.core.reflect.AmbiguousMethodException)

Example 3 with AmbiguousMethodException

use of com.axway.ats.core.reflect.AmbiguousMethodException in project ats-framework by Axway.

the class DatabaseProviderFactory method loadCustomDbProvider.

private static DbProvider loadCustomDbProvider(String className, DbConnection dbConnection) {
    Class<?> dbProviderClass = null;
    // load db provider class
    try {
        dbProviderClass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new DbException("Unable to get database provider for type '" + dbConnection.getDbType() + "'", e);
    }
    // load db provider constructor
    Constructor<?> constructorDbProvider = null;
    try {
        constructorDbProvider = new MethodFinder(dbProviderClass).findConstructor(new Class[] { DbConnection.class });
    } catch (NoSuchMethodException | AmbiguousMethodException e) {
        throw new DbException("Unable to get database provider for type '" + dbConnection.getDbType() + "'", e);
    }
    // create DbProvider class for this dbType
    try {
        return (DbProvider) constructorDbProvider.newInstance(new Object[] { dbConnection });
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new DbException("Unable to get database provider for type '" + dbConnection.getDbType() + "'", e);
    }
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) MethodFinder(com.axway.ats.core.reflect.MethodFinder) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider) OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) MssqlDbProvider(com.axway.ats.core.dbaccess.mssql.MssqlDbProvider) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) AmbiguousMethodException(com.axway.ats.core.reflect.AmbiguousMethodException)

Aggregations

AmbiguousMethodException (com.axway.ats.core.reflect.AmbiguousMethodException)3 MethodFinder (com.axway.ats.core.reflect.MethodFinder)3 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ActionAlreadyDefinedException (com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException)1 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)1 MssqlDbProvider (com.axway.ats.core.dbaccess.mssql.MssqlDbProvider)1 MysqlDbProvider (com.axway.ats.core.dbaccess.mysql.MysqlDbProvider)1 OracleDbProvider (com.axway.ats.core.dbaccess.oracle.OracleDbProvider)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1