Search in sources :

Example 1 with ReflectHelpers

use of org.apache.beam.sdk.util.common.ReflectHelpers in project beam by apache.

the class PipelineOptionsFactory method validateMethodsAreEitherBeanMethodOrKnownMethod.

/**
 * Validates that every non-static or synthetic method is either a known method such as {@link
 * PipelineOptions#as} or a bean property.
 *
 * @param iface The interface to validate.
 * @param klass The proxy class representing the interface.
 */
private static void validateMethodsAreEitherBeanMethodOrKnownMethod(Class<? extends PipelineOptions> iface, Class<? extends PipelineOptions> klass, List<PropertyDescriptor> descriptors) {
    Set<Method> knownMethods = Sets.newHashSet(IGNORED_METHODS);
    // Ignore synthetic methods
    for (Method method : klass.getMethods()) {
        if (Modifier.isStatic(method.getModifiers()) || method.isSynthetic()) {
            knownMethods.add(method);
        }
    }
    // Ignore methods on the base PipelineOptions interface.
    try {
        knownMethods.add(iface.getMethod("as", Class.class));
        knownMethods.add(iface.getMethod("outputRuntimeOptions"));
        knownMethods.add(iface.getMethod("populateDisplayData", DisplayData.Builder.class));
    } catch (NoSuchMethodException | SecurityException e) {
        throw new RuntimeException(e);
    }
    for (PropertyDescriptor descriptor : descriptors) {
        knownMethods.add(descriptor.getReadMethod());
        knownMethods.add(descriptor.getWriteMethod());
    }
    final Set<String> knownMethodsNames = Sets.newHashSet();
    for (Method method : knownMethods) {
        knownMethodsNames.add(method.getName());
    }
    // Verify that no additional methods are on an interface that aren't a bean property.
    // Because methods can have multiple declarations, we do a name-based comparison
    // here to prevent false positives.
    SortedSet<Method> unknownMethods = new TreeSet<>(MethodComparator.INSTANCE);
    unknownMethods.addAll(Sets.filter(Sets.difference(Sets.newHashSet(iface.getMethods()), knownMethods), Predicates.and(input1 -> !input1.isSynthetic(), input -> !knownMethodsNames.contains(input.getName()), input11 -> !Modifier.isStatic(input11.getModifiers()))));
    checkArgument(unknownMethods.isEmpty(), "Methods [%s] on [%s] do not conform to being bean properties.", unknownMethods.stream().map(ReflectHelpers::formatMethod).collect(Collectors.joining(",")), iface.getName());
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) TreeSet(java.util.TreeSet)

Aggregations

AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 TreeSet (java.util.TreeSet)1 ReflectHelpers (org.apache.beam.sdk.util.common.ReflectHelpers)1