use of java.lang.reflect.Type in project cucumber-jvm by cucumber.
the class TableConverter method convert.
/**
* This method converts a {@link cucumber.api.DataTable} to abother type.
* When a Step Definition is passed a Gherkin Data Table, the runtime will use this method to convert the
* {@link cucumber.api.DataTable} to the declared type before invoking the Step Definition.
* <p/>
* This method uses reflection to inspect the type and delegates to the appropriate {@code toXxx} method.
*
* @param dataTable the table to convert
* @param type the type to convert to
* @param transposed whether the table should be transposed first.
* @return the transformed object.
*/
public <T> T convert(DataTable dataTable, Type type, boolean transposed) {
if (transposed) {
dataTable = dataTable.transpose();
}
if (type == null || (type instanceof Class && ((Class) type).isAssignableFrom(DataTable.class))) {
return (T) dataTable;
}
Type mapKeyType = mapKeyType(type);
if (mapKeyType != null) {
Type mapValueType = mapValueType(type);
return (T) toMap(dataTable, mapKeyType, mapValueType);
}
Type itemType = listItemType(type);
if (itemType == null) {
throw new CucumberException("Not a Map or List type: " + type);
}
Type listItemType = listItemType(itemType);
if (listItemType != null) {
return (T) toLists(dataTable, listItemType);
} else {
SingleValueConverter singleValueConverter = xStream.getSingleValueConverter(itemType);
if (singleValueConverter != null) {
return (T) toList(dataTable, singleValueConverter);
} else {
if (itemType instanceof Class) {
if (Map.class.equals(itemType)) {
// Non-generic map
return (T) toMaps(dataTable, String.class, String.class);
} else {
return (T) toListOfComplexType(dataTable, (Class) itemType);
}
} else {
return (T) toMaps(dataTable, mapKeyType(itemType), mapValueType(itemType));
}
}
}
}
use of java.lang.reflect.Type in project cucumber-jvm by cucumber.
the class StepDefinitionMatch method tableArgument.
private Object tableArgument(Step step, int argIndex, LocalizedXStreams.LocalizedXStream xStream) {
ParameterInfo parameterInfo = getParameterType(argIndex, DataTable.class);
TableConverter tableConverter = new TableConverter(xStream, parameterInfo);
DataTable table = new DataTable(step.getRows(), tableConverter);
Type type = parameterInfo.getType();
return tableConverter.convert(table, type, parameterInfo.isTransposed());
}
use of java.lang.reflect.Type in project hibernate-orm by hibernate.
the class TestUtil method assertAttributeTypeInMetaModelFor.
public static void assertAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedType, String errorString) {
Field field = getFieldFromMetamodelFor(clazz, fieldName);
assertNotNull("Cannot find field '" + fieldName + "' in " + clazz.getName(), field);
ParameterizedType type = (ParameterizedType) field.getGenericType();
Type actualType = type.getActualTypeArguments()[1];
if (expectedType.isArray()) {
expectedType = expectedType.getComponentType();
actualType = getComponentType(actualType);
}
assertEquals("Types do not match: " + buildErrorString(errorString, clazz), expectedType, actualType);
}
use of java.lang.reflect.Type in project morphia by mongodb.
the class MappedField method toClass.
protected Class toClass(final Type t) {
if (t == null) {
return null;
} else if (t instanceof Class) {
return (Class) t;
} else if (t instanceof GenericArrayType) {
final Type type = ((GenericArrayType) t).getGenericComponentType();
Class aClass;
if (type instanceof ParameterizedType) {
aClass = (Class) ((ParameterizedType) type).getRawType();
} else if (type instanceof TypeVariable) {
aClass = ReflectionUtils.getTypeArgument(persistedClass, (TypeVariable<?>) type);
if (aClass == null) {
aClass = Object.class;
}
} else {
aClass = (Class) type;
}
return Array.newInstance(aClass, 0).getClass();
} else if (t instanceof ParameterizedType) {
return (Class) ((ParameterizedType) t).getRawType();
} else if (t instanceof WildcardType) {
return (Class) ((WildcardType) t).getUpperBounds()[0];
}
throw new RuntimeException("Generic TypeVariable not supported!");
}
use of java.lang.reflect.Type in project morphia by mongodb.
the class ReflectionUtils method getTypeArgument.
/**
* Returns the type argument
*
* @param clazz the Class to examine
* @param tv the TypeVariable to look for
* @param <T> the type of the Class
* @return the Class type
*/
public static <T> Class<?> getTypeArgument(final Class<? extends T> clazz, final TypeVariable<? extends GenericDeclaration> tv) {
final Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
Type type = clazz;
// start walking up the inheritance hierarchy until we hit the end
while (type != null && !Object.class.equals(getClass(type))) {
if (type instanceof Class) {
// there is no useful information for us in raw types, so just
// keep going.
type = ((Class) type).getGenericSuperclass();
} else {
final ParameterizedType parameterizedType = (ParameterizedType) type;
final Class<?> rawType = (Class) parameterizedType.getRawType();
final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
final TypeVariable<?>[] typeParameters = rawType.getTypeParameters();
for (int i = 0; i < actualTypeArguments.length; i++) {
if (typeParameters[i].equals(tv)) {
final Class cls = getClass(actualTypeArguments[i]);
if (cls != null) {
return cls;
}
//We don't know that the type we want is the one in the map, if this argument has been
//passed through multiple levels of the hierarchy. Walk back until we run out.
Type typeToTest = resolvedTypes.get(actualTypeArguments[i]);
while (typeToTest != null) {
final Class classToTest = getClass(typeToTest);
if (classToTest != null) {
return classToTest;
}
typeToTest = resolvedTypes.get(typeToTest);
}
}
resolvedTypes.put(typeParameters[i], actualTypeArguments[i]);
}
if (!rawType.equals(Object.class)) {
type = rawType.getGenericSuperclass();
}
}
}
return null;
}
Aggregations