}
/*
* Validate the specified method is valid
*/
InstrumentedClass instrumentedClass = ((AtomicImplementationDeclaration) context).getImplementationClass();
try {
/*
* Verify the method has a single parameter of type Instance
*/
String parameterType = instrumentedClass.getMethodParameterType(method,true);
if (parameterType == null) {
error("Invalid substitute value, method "+method+" with a single parameter is not defined in class "+instrumentedClass.getName());
return null;
}
if (!ComponentKind.INSTANCE.isAssignableTo(parameterType)) {
error("Invalid substitute value, method "+method+" with a single Instance parameter is not defined in class "+instrumentedClass.getName());
return null;
}
/*
* Verify the method return type is one of the supported property primitive types
*/
String returnType = instrumentedClass.getMethodReturnType(method, null,true);
if (returnType == null) {
error("Invalid substitute value, method "+method+"does not return a value");
return null;
}
for (Type primitive : PrimitiveType.values()) {
if (primitive.isAssignableFrom(returnType))
return primitive;
}
/*
* As a last resort, we also consider collections of strings
*
* TODO modify CodeReflection to perform this validation and get the type of the collection if possible
* to be able to accept collections of primitive types
*/
if ( returnType.equals(Collection.class.getCanonicalName()) || returnType.equals(Set.class.getCanonicalName())) {
return new CollectionType(PrimitiveType.STRING,true);
}
error("Invalid substitute value, method "+method+"does not return a value of a valid type "+returnType);
return null;
} catch (NoSuchMethodException exc) {
error("Invalid substitute value, method "+method+" with a single parameter is not defined in class "+instrumentedClass.getName());
return null;
}
}