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);
}
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 });
}
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() }));
}
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;
}
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);
}
}
Aggregations