org.unitils.objectvalidation.utils.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unitils-objectvalidation Show documentation
Show all versions of unitils-objectvalidation Show documentation
Unitils module to validate objects.
package org.unitils.objectvalidation.utils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.unitils.objectvalidation.EqualsHashCodeValidator;
import org.unitils.objectvalidation.ObjectValidationRulesCollection;
import org.unitils.util.AnnotationUtils;
/**
* Utility methods for the validation module.
*
* @author Matthieu Mestrez
* @since Oct 10, 2013
*/
public final class Utils {
private Utils() { }
/**
* Will look in the test class to find a field of type 'fieldType' annotated with 'annotation'
* @param classContainingField
* @param annotation
* @param fieldType
* @return the field if it exists
* @throws IllegalStateException if there was an error retrieving the field.
*/
public static List findOneAndOnlyFieldForAnnotation(Class extends Object> classContainingField, Class extends Annotation> annotation, Class fieldType) {
Set objectValidatorFields = AnnotationUtils.getFieldsAnnotatedWith(classContainingField, annotation);
List foundFields = new ArrayList();
for (Field field : objectValidatorFields) {
if (typeOrSubTypeOf(fieldType, field.getType())) {
foundFields.add(field);
}
}
if (objectValidatorFields.size() != foundFields.size()) {
throw new IllegalStateException("Annotation @" + annotation.getName() + " can only be used on a type " + fieldType.getName() + " or its subclasses.");
}
return foundFields;
}
private static boolean typeOrSubTypeOf(Class fieldType, Class> classToCheck) {
return classToCheck.isAssignableFrom(fieldType);
}
private static boolean implementsType(Class fieldType, Class> classToCheck) {
return Arrays.asList(classToCheck.getInterfaces()).contains(fieldType);
}
public static ObjectValidationRulesCollection toRulesCollection(String rulesClassName) {
try {
return toRulesCollection(Class.forName(rulesClassName));
} catch (ClassNotFoundException classNotFoundException) {
throw new IllegalArgumentException("The class " + rulesClassName + " was not found on the classpath.", classNotFoundException);
}
}
public static EqualsHashCodeValidator toEqualsHashCodeValidator(Class> classToCast) {
checkNotNull(classToCast,
"Collection cannot be null");
return toRulesCollection(classToCast).getEqualsHashCodeValidator();
}
public static ObjectValidationRulesCollection toRulesCollection(Class> classToCast) {
checkNotNull(classToCast,
"Collection cannot be null");
checkArgument(!classToCast.isInterface(),
"Collection must be an implementation");
checkArgument(implementsType(ObjectValidationRulesCollection.class, classToCast),
classToCast.getName() + " is does not implement the interface " + ObjectValidationRulesCollection.class.getName());
return (ObjectValidationRulesCollection) instantiate(classToCast);
}
public static Object instantiate(Class> classToInstatiate) {
try {
return classToInstatiate.newInstance();
} catch (InstantiationException instantiationException) {
throw new IllegalArgumentException(classToInstatiate.getName() + " cannot be instantiated.", instantiationException);
} catch (IllegalAccessException illegalAccessException) {
throw new IllegalArgumentException(classToInstatiate.getName() + " cannot be accessed.", illegalAccessException);
}
}
public static T checkNotNull(T reference, Object errorMessage) {
if (reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
}
return reference;
}
public static void checkArgument(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}
}