Search in sources :

Example 1 with Condition

use of org.talend.sdk.component.api.configuration.condition.meta.Condition in project component-runtime by Talend.

the class ConditionParameterEnricher method onParameterAnnotation.

@Override
public Map<String, String> onParameterAnnotation(final String parameterName, final Type parameterType, final Annotation annotation) {
    final Condition condition = annotation.annotationType().getAnnotation(Condition.class);
    if (condition != null) {
        final String type = condition.value();
        if (ActiveIfs.class.isInstance(annotation)) {
            return Stream.of(ActiveIfs.class.cast(annotation).value()).map(ai -> onParameterAnnotation(parameterName, parameterType, ai)).collect(HashMap::new, (map, entry) -> {
                final String suffix = "::" + (map.size() / 2);
                map.putAll(entry.entrySet().stream().collect(toMap(e -> e.getKey() + suffix, Map.Entry::getValue)));
            }, HashMap::putAll);
        }
        return Stream.of(annotation.annotationType().getMethods()).filter(m -> m.getDeclaringClass() == annotation.annotationType()).collect(toMap(m -> META_PREFIX + type + "::" + m.getName(), m -> {
            try {
                final Object invoke = m.invoke(annotation);
                if (String[].class.isInstance(invoke)) {
                    return Stream.of(String[].class.cast(invoke)).collect(joining(","));
                }
                return String.valueOf(invoke);
            } catch (final InvocationTargetException | IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
        }));
    }
    return emptyMap();
}
Also used : Condition(org.talend.sdk.component.api.configuration.condition.meta.Condition) Collections.emptyMap(java.util.Collections.emptyMap) ParameterExtensionEnricher(org.talend.sdk.component.spi.parameter.ParameterExtensionEnricher) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) Type(java.lang.reflect.Type) ActiveIfs(org.talend.sdk.component.api.configuration.condition.ActiveIfs) Map(java.util.Map) Condition(org.talend.sdk.component.api.configuration.condition.meta.Condition) Annotation(java.lang.annotation.Annotation) HashMap(java.util.HashMap) Collectors.joining(java.util.stream.Collectors.joining) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashMap(java.util.HashMap)

Aggregations

Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Type (java.lang.reflect.Type)1 Collections.emptyMap (java.util.Collections.emptyMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Stream (java.util.stream.Stream)1 ActiveIfs (org.talend.sdk.component.api.configuration.condition.ActiveIfs)1 Condition (org.talend.sdk.component.api.configuration.condition.meta.Condition)1 ParameterExtensionEnricher (org.talend.sdk.component.spi.parameter.ParameterExtensionEnricher)1