|
| 1 | +package io.cucumber.java; |
| 2 | + |
| 3 | +import io.cucumber.core.backend.DataTableTypeTypeDefinition; |
| 4 | +import io.cucumber.core.backend.Lookup; |
| 5 | +import io.cucumber.core.exception.CucumberException; |
| 6 | +import io.cucumber.core.runtime.Invoker; |
| 7 | +import io.cucumber.datatable.DataTable; |
| 8 | +import io.cucumber.datatable.DataTableType; |
| 9 | +import io.cucumber.datatable.TableCellTransformer; |
| 10 | +import io.cucumber.datatable.TableEntryTransformer; |
| 11 | +import io.cucumber.datatable.TableRowTransformer; |
| 12 | +import io.cucumber.datatable.TableTransformer; |
| 13 | + |
| 14 | +import java.lang.reflect.Method; |
| 15 | +import java.lang.reflect.ParameterizedType; |
| 16 | +import java.lang.reflect.Type; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +class JavaDataTableTypeDefinition implements DataTableTypeTypeDefinition { |
| 21 | + |
| 22 | + private final Method method; |
| 23 | + private final Lookup lookup; |
| 24 | + private final DataTableType dataTableType; |
| 25 | + |
| 26 | + JavaDataTableTypeDefinition(Method method, Lookup lookup) { |
| 27 | + this.method = method; |
| 28 | + this.lookup = lookup; |
| 29 | + this.dataTableType = createDataTableType(method); |
| 30 | + } |
| 31 | + |
| 32 | + @SuppressWarnings("unchecked") |
| 33 | + private DataTableType createDataTableType(Method method) { |
| 34 | + Class returnType = requireValidReturnType(method); |
| 35 | + Type parameterType = requireValidParameterType(method); |
| 36 | + |
| 37 | + if (DataTable.class.equals(parameterType)) { |
| 38 | + return new DataTableType( |
| 39 | + returnType, |
| 40 | + (TableTransformer<Object>) this::execute |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + if (List.class.equals(parameterType)) { |
| 45 | + return new DataTableType( |
| 46 | + returnType, |
| 47 | + (TableRowTransformer<Object>) this::execute |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + if (Map.class.equals(parameterType)) { |
| 52 | + return new DataTableType( |
| 53 | + returnType, |
| 54 | + (TableEntryTransformer<Object>) this::execute |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + if (String.class.equals(parameterType)) { |
| 59 | + return new DataTableType( |
| 60 | + returnType, |
| 61 | + (TableCellTransformer<Object>) this::execute |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + throw createInvalidSignatureException(); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + private static CucumberException createInvalidSignatureException() { |
| 70 | + return new CucumberException("" + |
| 71 | + "A @DataTableType annotated method must have one of these signatures:\n" + |
| 72 | + " * public Author author(DataTable table)\n" + |
| 73 | + " * public Author author(List<String> row)\n" + |
| 74 | + " * public Author author(Map<String, String> entry)\n" + |
| 75 | + " * public Author author(String cell)\n" + |
| 76 | + "Note: Author is an example of the class you want to convert the table to" |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + private static Type requireValidParameterType(Method method) { |
| 82 | + Type[] parameterTypes = method.getGenericParameterTypes(); |
| 83 | + if (parameterTypes.length != 1) { |
| 84 | + throw createInvalidSignatureException(); |
| 85 | + } |
| 86 | + |
| 87 | + Type parameterType = parameterTypes[0]; |
| 88 | + if (!(parameterType instanceof ParameterizedType)) { |
| 89 | + return parameterType; |
| 90 | + } |
| 91 | + |
| 92 | + ParameterizedType parameterizedType = (ParameterizedType) parameterType; |
| 93 | + Type[] typeParameters = parameterizedType.getActualTypeArguments(); |
| 94 | + for (Type typeParameter : typeParameters) { |
| 95 | + if (!String.class.equals(typeParameter)) { |
| 96 | + throw createInvalidSignatureException(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return parameterizedType.getRawType(); |
| 101 | + } |
| 102 | + |
| 103 | + private static Class requireValidReturnType(Method method) { |
| 104 | + Class returnType = method.getReturnType(); |
| 105 | + if (Void.class.equals(returnType)) { |
| 106 | + throw createInvalidSignatureException(); |
| 107 | + } |
| 108 | + return returnType; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + @Override |
| 113 | + public DataTableType dataTableType() { |
| 114 | + return dataTableType; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + private Object execute(Object arg) throws Throwable { |
| 119 | + return Invoker.invoke(lookup.getInstance(method.getDeclaringClass()), method, 0, arg); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments