Search in sources :

Example 1 with Assessor

use of org.talend.sdk.component.api.input.Assessor in project component-runtime by Talend.

the class ModelVisitor method validatePartitionMapper.

private void validatePartitionMapper(final Class<?> type) {
    if (Stream.of(type.getMethods()).filter(m -> getPartitionMapperMethods().anyMatch(m::isAnnotationPresent)).flatMap(m -> getPartitionMapperMethods().filter(m::isAnnotationPresent)).distinct().count() != 3) {
        throw new IllegalArgumentException(type + " partition mapper must have exactly one @Assessor, one @Split and one @Emitter methods");
    }
    // 
    // now validate the 2 methods of the mapper
    // 
    Stream.of(type.getMethods()).filter(m -> m.isAnnotationPresent(Assessor.class)).forEach(m -> {
        if (m.getParameterCount() > 0) {
            throw new IllegalArgumentException(m + " must not have any parameter");
        }
    });
    Stream.of(type.getMethods()).filter(m -> m.isAnnotationPresent(Split.class)).forEach(m -> {
        // we must do that validation
        if (Stream.of(m.getParameters()).filter(p -> !p.isAnnotationPresent(PartitionSize.class) || (p.getType() != long.class && p.getType() != int.class)).count() > 0) {
            throw new IllegalArgumentException(m + " must not have any parameter without @PartitionSize");
        }
        final Type splitReturnType = m.getGenericReturnType();
        if (!ParameterizedType.class.isInstance(splitReturnType)) {
            throw new IllegalArgumentException(m + " must return a Collection<" + type.getName() + ">");
        }
        final ParameterizedType splitPt = ParameterizedType.class.cast(splitReturnType);
        if (!Class.class.isInstance(splitPt.getRawType()) || !Collection.class.isAssignableFrom(Class.class.cast(splitPt.getRawType()))) {
            throw new IllegalArgumentException(m + " must return a List of partition mapper, found: " + splitPt);
        }
        final Type arg = splitPt.getActualTypeArguments().length != 1 ? null : splitPt.getActualTypeArguments()[0];
        if (!Class.class.isInstance(arg) || !type.isAssignableFrom(Class.class.cast(arg))) {
            throw new IllegalArgumentException(m + " must return a Collection<" + type.getName() + "> but found: " + arg);
        }
    });
    Stream.of(type.getMethods()).filter(m -> m.isAnnotationPresent(Emitter.class)).forEach(m -> {
        // already own all the config
        if (m.getParameterCount() > 0) {
            throw new IllegalArgumentException(m + " must not have any parameter");
        }
    });
}
Also used : Emitter(org.talend.sdk.component.api.input.Emitter) PartitionMapper(org.talend.sdk.component.api.input.PartitionMapper) Producer(org.talend.sdk.component.api.input.Producer) AfterGroup(org.talend.sdk.component.api.processor.AfterGroup) BeforeGroup(org.talend.sdk.component.api.processor.BeforeGroup) Collection(java.util.Collection) Assessor(org.talend.sdk.component.api.input.Assessor) Split(org.talend.sdk.component.api.input.Split) OutputEmitter(org.talend.sdk.component.api.processor.OutputEmitter) Processor(org.talend.sdk.component.api.processor.Processor) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ElementListener(org.talend.sdk.component.api.processor.ElementListener) Annotation(java.lang.annotation.Annotation) PartitionSize(org.talend.sdk.component.api.input.PartitionSize) Method(java.lang.reflect.Method) Output(org.talend.sdk.component.api.processor.Output) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) PartitionSize(org.talend.sdk.component.api.input.PartitionSize) Stream(java.util.stream.Stream)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 Assessor (org.talend.sdk.component.api.input.Assessor)1 Emitter (org.talend.sdk.component.api.input.Emitter)1 PartitionMapper (org.talend.sdk.component.api.input.PartitionMapper)1 PartitionSize (org.talend.sdk.component.api.input.PartitionSize)1 Producer (org.talend.sdk.component.api.input.Producer)1 Split (org.talend.sdk.component.api.input.Split)1 AfterGroup (org.talend.sdk.component.api.processor.AfterGroup)1 BeforeGroup (org.talend.sdk.component.api.processor.BeforeGroup)1 ElementListener (org.talend.sdk.component.api.processor.ElementListener)1 Output (org.talend.sdk.component.api.processor.Output)1 OutputEmitter (org.talend.sdk.component.api.processor.OutputEmitter)1 Processor (org.talend.sdk.component.api.processor.Processor)1