Search in sources :

Example 1 with StateId

use of org.apache.beam.sdk.transforms.DoFn.StateId in project beam by apache.

the class DoFnSignatures method analyzeStateDeclarations.

private static Map<String, DoFnSignature.StateDeclaration> analyzeStateDeclarations(ErrorReporter errors, Class<?> fnClazz) {
    Map<String, DoFnSignature.StateDeclaration> declarations = new HashMap<>();
    for (Field field : declaredFieldsWithAnnotation(DoFn.StateId.class, fnClazz, DoFn.class)) {
        // StateSpec fields may generally be private, but will be accessed via the signature
        field.setAccessible(true);
        String id = field.getAnnotation(DoFn.StateId.class).value();
        if (declarations.containsKey(id)) {
            errors.throwIllegalArgument("Duplicate %s \"%s\", used on both of [%s] and [%s]", DoFn.StateId.class.getSimpleName(), id, field.toString(), declarations.get(id).field().toString());
            continue;
        }
        Class<?> stateSpecRawType = field.getType();
        if (!(TypeDescriptor.of(stateSpecRawType).isSubtypeOf(TypeDescriptor.of(StateSpec.class)))) {
            errors.throwIllegalArgument("%s annotation on non-%s field [%s] that has class %s", DoFn.StateId.class.getSimpleName(), StateSpec.class.getSimpleName(), field.toString(), stateSpecRawType.getName());
            continue;
        }
        if (!Modifier.isFinal(field.getModifiers())) {
            errors.throwIllegalArgument("Non-final field %s annotated with %s. State declarations must be final.", field.toString(), DoFn.StateId.class.getSimpleName());
            continue;
        }
        Type stateSpecType = field.getGenericType();
        // A type descriptor for whatever type the @StateId-annotated class has, which
        // must be some subtype of StateSpec
        TypeDescriptor<? extends StateSpec<?>> stateSpecSubclassTypeDescriptor = (TypeDescriptor) TypeDescriptor.of(stateSpecType);
        // A type descriptor for StateSpec, with the generic type parameters filled
        // in according to the specialization of the subclass (or just straight params)
        TypeDescriptor<StateSpec<?>> stateSpecTypeDescriptor = (TypeDescriptor) stateSpecSubclassTypeDescriptor.getSupertype(StateSpec.class);
        // The type of the state, which may still have free type variables from the
        // context
        Type unresolvedStateType = ((ParameterizedType) stateSpecTypeDescriptor.getType()).getActualTypeArguments()[0];
        // By static typing this is already a well-formed State subclass
        TypeDescriptor<? extends State> stateType = (TypeDescriptor<? extends State>) TypeDescriptor.of(fnClazz).resolveType(unresolvedStateType);
        declarations.put(id, DoFnSignature.StateDeclaration.create(id, field, stateType));
    }
    return ImmutableMap.copyOf(declarations);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StateId(org.apache.beam.sdk.transforms.DoFn.StateId) Field(java.lang.reflect.Field) StateSpec(org.apache.beam.sdk.state.StateSpec) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) DoFn(org.apache.beam.sdk.transforms.DoFn) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) State(org.apache.beam.sdk.state.State) StateDeclaration(org.apache.beam.sdk.transforms.reflect.DoFnSignature.StateDeclaration)

Aggregations

Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 State (org.apache.beam.sdk.state.State)1 StateSpec (org.apache.beam.sdk.state.StateSpec)1 DoFn (org.apache.beam.sdk.transforms.DoFn)1 StateId (org.apache.beam.sdk.transforms.DoFn.StateId)1 StateDeclaration (org.apache.beam.sdk.transforms.reflect.DoFnSignature.StateDeclaration)1 TypeDescriptor (org.apache.beam.sdk.values.TypeDescriptor)1