Search in sources :

Example 26 with Type

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));
            }
        }
    }
}
Also used : DataTable(cucumber.api.DataTable) Utils.mapKeyType(cucumber.runtime.Utils.mapKeyType) Utils.listItemType(cucumber.runtime.Utils.listItemType) Utils.mapValueType(cucumber.runtime.Utils.mapValueType) Type(java.lang.reflect.Type) SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) CucumberException(cucumber.runtime.CucumberException)

Example 27 with Type

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());
}
Also used : DataTable(cucumber.api.DataTable) Type(java.lang.reflect.Type) TableConverter(cucumber.runtime.table.TableConverter)

Example 28 with Type

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);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 29 with Type

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!");
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) DBObject(com.mongodb.DBObject) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 30 with Type

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) HashMap(java.util.HashMap) TypeVariable(java.lang.reflect.TypeVariable) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

Type (java.lang.reflect.Type)6477 ParameterizedType (java.lang.reflect.ParameterizedType)1798 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)711 WildcardType (java.lang.reflect.WildcardType)580 Test (org.junit.Test)513 ArrayList (java.util.ArrayList)429 Method (java.lang.reflect.Method)424 TypeVariable (java.lang.reflect.TypeVariable)350 List (java.util.List)342 Map (java.util.Map)296 Gson (com.google.gson.Gson)230 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)211 Field (java.lang.reflect.Field)170 Annotation (java.lang.annotation.Annotation)161 IOException (java.io.IOException)146 Collection (java.util.Collection)116