Search in sources :

Example 36 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project beam by apache.

the class SnsResponse method of.

public static <T> SnsResponse<T> of(@NonNull T element, @Nullable PublishResponse response) {
    final Optional<PublishResponse> publishResponse = Optional.ofNullable(response);
    OptionalInt statusCode = publishResponse.map(r -> OptionalInt.of(r.sdkHttpResponse().statusCode())).orElse(OptionalInt.empty());
    Optional<String> statusText = publishResponse.flatMap(r -> r.sdkHttpResponse().statusText());
    return create(element, statusCode, statusText);
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) PublishResponse(software.amazon.awssdk.services.sns.model.PublishResponse) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) OptionalInt(java.util.OptionalInt) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Serializable(java.io.Serializable) PublishResponse(software.amazon.awssdk.services.sns.model.PublishResponse) OptionalInt(java.util.OptionalInt)

Example 37 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project beam by apache.

the class PipelineOptionsFactory method printHelp.

/**
 * Outputs the set of options available to be set for the passed in {@link PipelineOptions}
 * interface. The output is in a human readable format. The format is:
 *
 * <pre>
 * OptionGroup:
 *     ... option group description ...
 *
 *  --option1={@code <type>} or list of valid enum choices
 *     Default: value (if available, see {@link Default})
 *     ... option description ... (if available, see {@link Description})
 *     Required groups (if available, see {@link Required})
 *  --option2={@code <type>} or list of valid enum choices
 *     Default: value (if available, see {@link Default})
 *     ... option description ... (if available, see {@link Description})
 *     Required groups (if available, see {@link Required})
 * </pre>
 *
 * This method will attempt to format its output to be compatible with a terminal window.
 */
public static void printHelp(PrintStream out, Class<? extends PipelineOptions> iface) {
    checkNotNull(out);
    checkNotNull(iface);
    CACHE.get().validateWellFormed(iface);
    Set<PipelineOptionSpec> properties = PipelineOptionsReflector.getOptionSpecs(iface, true);
    RowSortedTable<Class<?>, String, Method> ifacePropGetterTable = TreeBasedTable.create(ClassNameComparator.INSTANCE, Ordering.natural());
    for (PipelineOptionSpec prop : properties) {
        ifacePropGetterTable.put(prop.getDefiningInterface(), prop.getName(), prop.getGetterMethod());
    }
    for (Map.Entry<Class<?>, Map<String, Method>> ifaceToPropertyMap : ifacePropGetterTable.rowMap().entrySet()) {
        Class<?> currentIface = ifaceToPropertyMap.getKey();
        Map<String, Method> propertyNamesToGetters = ifaceToPropertyMap.getValue();
        SortedSetMultimap<String, String> requiredGroupNameToProperties = getRequiredGroupNamesToProperties(propertyNamesToGetters);
        out.format("%s:%n", currentIface.getName());
        Description ifaceDescription = currentIface.getAnnotation(Description.class);
        if (ifaceDescription != null && ifaceDescription.value() != null) {
            prettyPrintDescription(out, ifaceDescription);
        }
        out.println();
        List<@KeyFor("propertyNamesToGetters") String> lists = Lists.newArrayList(propertyNamesToGetters.keySet());
        lists.sort(String.CASE_INSENSITIVE_ORDER);
        for (String propertyName : lists) {
            Method method = propertyNamesToGetters.get(propertyName);
            String printableType = method.getReturnType().getSimpleName();
            if (method.getReturnType().isEnum()) {
                Object @Nullable [] enumConstants = method.getReturnType().getEnumConstants();
                assert enumConstants != null : "@AssumeAssertion(nullness): checked that it is an enum";
                printableType = Joiner.on(" | ").join(method.getReturnType().getEnumConstants());
            }
            out.format("  --%s=<%s>%n", propertyName, printableType);
            Optional<String> defaultValue = getDefaultValueFromAnnotation(method);
            if (defaultValue.isPresent()) {
                out.format("    Default: %s%n", defaultValue.get());
            }
            @Nullable Description methodDescription = method.getAnnotation(Description.class);
            if (methodDescription != null && methodDescription.value() != null) {
                prettyPrintDescription(out, methodDescription);
            }
            prettyPrintRequiredGroups(out, method.getAnnotation(Validation.Required.class), requiredGroupNameToProperties);
        }
        out.println();
    }
}
Also used : Method(java.lang.reflect.Method) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) Required(org.apache.beam.sdk.options.Validation.Required) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 38 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project beam by apache.

the class AutoValueUtils method getConstructorCreator.

/**
 * Try to find an accessible constructor for creating an AutoValue class. Otherwise return null.
 */
@Nullable
public static SchemaUserTypeCreator getConstructorCreator(Class<?> clazz, Schema schema, FieldValueTypeSupplier fieldValueTypeSupplier) {
    Class<?> generatedClass = getAutoValueGenerated(clazz);
    List<FieldValueTypeInformation> schemaTypes = fieldValueTypeSupplier.get(clazz, schema);
    Optional<Constructor<?>> constructor = Arrays.stream(generatedClass.getDeclaredConstructors()).filter(c -> !Modifier.isPrivate(c.getModifiers())).filter(c -> matchConstructor(c, schemaTypes)).findAny();
    return constructor.map(c -> JavaBeanUtils.getConstructorCreator(generatedClass, c, schema, fieldValueTypeSupplier, new DefaultTypeConversionsFactory())).orElse(null);
}
Also used : ByteBuddy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy) Preconditions.checkNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull) Arrays(java.util.Arrays) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) MethodVariableAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodVariableAccess) Experimental(org.apache.beam.sdk.annotations.Experimental) ArrayAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayAccess) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) TypeCasting(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.TypeCasting) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) Parameter(java.lang.reflect.Parameter) Kind(org.apache.beam.sdk.annotations.Experimental.Kind) SchemaUserTypeCreator(org.apache.beam.sdk.schemas.SchemaUserTypeCreator) Map(java.util.Map) MethodReturn(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodReturn) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) Method(java.lang.reflect.Method) IntegerConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.IntegerConstant) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ClassLoadingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.loading.ClassLoadingStrategy) TypeCreation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.TypeCreation) Duplication(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Duplication) Size(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender.Size) ElementMatchers(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.matcher.ElementMatchers) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) ClassWriter(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.ClassWriter) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) TypeConversion(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversion) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) MethodInvocation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodInvocation) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) InjectPackageStrategy(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) TypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversionsFactory) Optional(java.util.Optional) Removal(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Removal) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) Constructor(java.lang.reflect.Constructor) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 39 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project beam by apache.

the class FromRowUsingCreator method fromValue.

@SuppressWarnings("unchecked")
@Nullable
private <ValueT> ValueT fromValue(FieldType type, ValueT value, Type fieldType, FieldValueTypeInformation fieldValueTypeInformation, Factory<List<FieldValueTypeInformation>> typeFactory) {
    FieldValueTypeInformation elementType = fieldValueTypeInformation.getElementType();
    FieldValueTypeInformation keyType = fieldValueTypeInformation.getMapKeyType();
    FieldValueTypeInformation valueType = fieldValueTypeInformation.getMapValueType();
    if (value == null) {
        return null;
    }
    if (TypeName.ROW.equals(type.getTypeName())) {
        return (ValueT) fromRow((Row) value, (Class) fieldType, typeFactory);
    } else if (TypeName.ARRAY.equals(type.getTypeName())) {
        return (ValueT) fromCollectionValue(type.getCollectionElementType(), (Collection) value, elementType, typeFactory);
    } else if (TypeName.ITERABLE.equals(type.getTypeName())) {
        return (ValueT) fromIterableValue(type.getCollectionElementType(), (Iterable) value, elementType, typeFactory);
    }
    if (TypeName.MAP.equals(type.getTypeName())) {
        return (ValueT) fromMapValue(type.getMapKeyType(), type.getMapValueType(), (Map) value, keyType, valueType, typeFactory);
    } else {
        if (type.isLogicalType(OneOfType.IDENTIFIER)) {
            OneOfType oneOfType = type.getLogicalType(OneOfType.class);
            EnumerationType oneOfEnum = oneOfType.getCaseEnumType();
            OneOfType.Value oneOfValue = (OneOfType.Value) value;
            FieldValueTypeInformation oneOfFieldValueTypeInformation = checkNotNull(fieldValueTypeInformation.getOneOfTypes().get(oneOfEnum.toString(oneOfValue.getCaseType())));
            Object fromValue = fromValue(oneOfType.getFieldType(oneOfValue), oneOfValue.getValue(), oneOfFieldValueTypeInformation.getRawType(), oneOfFieldValueTypeInformation, typeFactory);
            return (ValueT) oneOfType.createValue(oneOfValue.getCaseType(), fromValue);
        } else if (type.getTypeName().isLogicalType()) {
            return (ValueT) type.getLogicalType().toBaseType(value);
        }
        return value;
    }
}
Also used : EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) Row(org.apache.beam.sdk.values.Row) Map(java.util.Map) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 40 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project beam by apache.

the class SchemaTranslation method fieldTypeFromProtoWithoutNullable.

private static FieldType fieldTypeFromProtoWithoutNullable(SchemaApi.FieldType protoFieldType) {
    switch(protoFieldType.getTypeInfoCase()) {
        case ATOMIC_TYPE:
            switch(protoFieldType.getAtomicType()) {
                case BYTE:
                    return FieldType.of(TypeName.BYTE);
                case INT16:
                    return FieldType.of(TypeName.INT16);
                case INT32:
                    return FieldType.of(TypeName.INT32);
                case INT64:
                    return FieldType.of(TypeName.INT64);
                case FLOAT:
                    return FieldType.of(TypeName.FLOAT);
                case DOUBLE:
                    return FieldType.of(TypeName.DOUBLE);
                case STRING:
                    return FieldType.of(TypeName.STRING);
                case BOOLEAN:
                    return FieldType.of(TypeName.BOOLEAN);
                case BYTES:
                    return FieldType.of(TypeName.BYTES);
                case UNSPECIFIED:
                    throw new IllegalArgumentException("Encountered UNSPECIFIED AtomicType");
                default:
                    throw new IllegalArgumentException("Encountered unknown AtomicType: " + protoFieldType.getAtomicType());
            }
        case ROW_TYPE:
            return FieldType.row(schemaFromProto(protoFieldType.getRowType().getSchema()));
        case ARRAY_TYPE:
            return FieldType.array(fieldTypeFromProto(protoFieldType.getArrayType().getElementType()));
        case ITERABLE_TYPE:
            return FieldType.iterable(fieldTypeFromProto(protoFieldType.getIterableType().getElementType()));
        case MAP_TYPE:
            return FieldType.map(fieldTypeFromProto(protoFieldType.getMapType().getKeyType()), fieldTypeFromProto(protoFieldType.getMapType().getValueType()));
        case LOGICAL_TYPE:
            String urn = protoFieldType.getLogicalType().getUrn();
            Class<? extends LogicalType<?, ?>> logicalTypeClass = STANDARD_LOGICAL_TYPES.get(urn);
            if (logicalTypeClass != null) {
                try {
                    return FieldType.logicalType(logicalTypeClass.getConstructor().newInstance());
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException(String.format("Standard logical type '%s' does not have a zero-argument constructor.", urn), e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(String.format("Standard logical type '%s' has a zero-argument constructor, but it is not accessible.", urn), e);
                } catch (ReflectiveOperationException e) {
                    throw new RuntimeException(String.format("Error instantiating logical type '%s' with zero-argument constructor.", urn), e);
                }
            }
            // but not yet in Java. (BEAM-7554)
            if (urn.equals(URN_BEAM_LOGICAL_DATETIME)) {
                return FieldType.DATETIME;
            } else if (urn.equals(URN_BEAM_LOGICAL_DECIMAL)) {
                return FieldType.DECIMAL;
            } else if (urn.equals(URN_BEAM_LOGICAL_JAVASDK)) {
                return FieldType.logicalType((LogicalType) SerializableUtils.deserializeFromByteArray(protoFieldType.getLogicalType().getPayload().toByteArray(), "logicalType"));
            } else {
                @Nullable FieldType argumentType = null;
                @Nullable Object argumentValue = null;
                if (protoFieldType.getLogicalType().hasArgumentType()) {
                    argumentType = fieldTypeFromProto(protoFieldType.getLogicalType().getArgumentType());
                    argumentValue = fieldValueFromProto(argumentType, protoFieldType.getLogicalType().getArgument());
                }
                return FieldType.logicalType(new UnknownLogicalType(urn, protoFieldType.getLogicalType().getPayload().toByteArray(), argumentType, argumentValue, fieldTypeFromProto(protoFieldType.getLogicalType().getRepresentation())));
            }
        default:
            throw new IllegalArgumentException("Unexpected type_info: " + protoFieldType.getTypeInfoCase());
    }
}
Also used : UnknownLogicalType(org.apache.beam.sdk.schemas.logicaltypes.UnknownLogicalType) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) Nullable(org.checkerframework.checker.nullness.qual.Nullable) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType)

Aggregations

Nullable (org.checkerframework.checker.nullness.qual.Nullable)289 ArrayList (java.util.ArrayList)30 TypeElement (javax.lang.model.element.TypeElement)23 Map (java.util.Map)22 IOException (java.io.IOException)21 List (java.util.List)19 ExecutableElement (javax.lang.model.element.ExecutableElement)19 ServerLevel (net.minecraft.server.level.ServerLevel)19 Instant (org.joda.time.Instant)16 HashMap (java.util.HashMap)15 VariableElement (javax.lang.model.element.VariableElement)15 Optional (java.util.Optional)14 Element (javax.lang.model.element.Element)13 TreePath (com.sun.source.util.TreePath)12 BlockPos (net.minecraft.core.BlockPos)12 Method (java.lang.reflect.Method)11 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)11 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)10 VariableTree (com.sun.source.tree.VariableTree)8 ClassTree (com.sun.source.tree.ClassTree)7