Search in sources :

Example 1 with MethodFinder

use of com.axway.ats.core.reflect.MethodFinder 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 MethodFinder

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

the class Test_MethodFinder method findMethodConstructorMethodListNegativeAmbigous.

@Test(expected = AmbiguousMethodException.class)
public void findMethodConstructorMethodListNegativeAmbigous() throws NoSuchMethodException, AmbiguousMethodException {
    MethodFinder methodFinder = new MethodFinder("ambiguous", Arrays.asList(MethodFinderTester.class.getDeclaredMethods()));
    //no arguments
    methodFinder.findMethod("ambiguousMethod", new Class<?>[] { Void.TYPE });
}
Also used : MethodFinder(com.axway.ats.core.reflect.MethodFinder) BaseTest(com.axway.ats.core.BaseTest) Test(org.junit.Test)

Example 3 with MethodFinder

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

the class Test_StringToEnumRule method isCompatibleNegativeArgisArrayParamIsNot.

@Test(expected = NoSuchMethodException.class)
public void isCompatibleNegativeArgisArrayParamIsNot() throws NoSuchMethodException, AmbiguousMethodException {
    List<TypeComparisonRule> typeComparisonRules = new ArrayList<TypeComparisonRule>();
    typeComparisonRules.add(new StringToEnumRule());
    MethodFinder methodFinder = new MethodFinder("methods", Arrays.asList(ActionClassEnumArguments.class.getDeclaredMethods()), typeComparisonRules);
    //String argument
    assertNotNull(methodFinder.findMethod(new Class<?>[] { (new int[] {}).getClass() }));
}
Also used : TypeComparisonRule(com.axway.ats.core.reflect.TypeComparisonRule) ArrayList(java.util.ArrayList) MethodFinder(com.axway.ats.core.reflect.MethodFinder) Test(org.junit.Test) BaseTest(com.axway.ats.agent.core.BaseTest)

Example 4 with MethodFinder

use of com.axway.ats.core.reflect.MethodFinder 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 5 with MethodFinder

use of com.axway.ats.core.reflect.MethodFinder 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

MethodFinder (com.axway.ats.core.reflect.MethodFinder)17 Test (org.junit.Test)13 BaseTest (com.axway.ats.core.BaseTest)10 ArrayList (java.util.ArrayList)5 TypeComparisonRule (com.axway.ats.core.reflect.TypeComparisonRule)4 BaseTest (com.axway.ats.agent.core.BaseTest)3 AmbiguousMethodException (com.axway.ats.core.reflect.AmbiguousMethodException)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 PublicAtsApi (com.axway.ats.common.PublicAtsApi)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 File (java.io.File)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1