/** * If {@code coderType} is a subclass of {@code Coder<T>} for a specific type {@code T}, returns * {@code T.class}. */ @SuppressWarnings({"rawtypes", "unchecked"}) public static TypeDescriptor getCodedType(TypeDescriptor coderDescriptor) { ParameterizedType coderType = (ParameterizedType) coderDescriptor.getSupertype(Coder.class).getType(); TypeDescriptor codedType = TypeDescriptor.of(coderType.getActualTypeArguments()[0]); return codedType; } }
/** * Returns the {@link Coder} to use on elements produced by this function, given the {@link Coder} * used for its input elements. * * @throws CannotProvideCoderException if a {@link Coder} cannot be provided * @deprecated This method is to change in an unknown backwards incompatible way once support for * this functionality is refined. */ @Deprecated @Internal public <InputT, OutputT> Coder<OutputT> getOutputCoder( SerializableFunction<InputT, OutputT> fn, Coder<InputT> inputCoder) throws CannotProvideCoderException { ParameterizedType fnType = (ParameterizedType) TypeDescriptor.of(fn.getClass()).getSupertype(SerializableFunction.class).getType(); return getCoder( fn.getClass(), SerializableFunction.class, ImmutableMap.of(fnType.getActualTypeArguments()[0], inputCoder), SerializableFunction.class.getTypeParameters()[1]); }
/** * Like {@link #extractFromTypeParameters(Object, Class, TypeVariableExtractor)}, but takes a * {@link TypeDescriptor} of the instance being analyzed rather than the instance itself. */ @SuppressWarnings("unchecked") public static <T, V> TypeDescriptor<V> extractFromTypeParameters( TypeDescriptor<T> type, Class<? super T> supertype, TypeVariableExtractor<T, V> extractor) { // Get the type signature of the extractor, e.g. // TypeVariableExtractor<SerializableFunction<BarT, String>, BarT> TypeDescriptor<TypeVariableExtractor<T, V>> extractorSupertype = (TypeDescriptor<TypeVariableExtractor<T, V>>) TypeDescriptor.of(extractor.getClass()).getSupertype(TypeVariableExtractor.class); // Get the actual type argument, e.g. SerializableFunction<BarT, String> Type inputT = ((ParameterizedType) extractorSupertype.getType()).getActualTypeArguments()[0]; // Get the actual supertype of the type being analyzed, hopefully with all type parameters // resolved, e.g. SerializableFunction<Integer, String> TypeDescriptor supertypeDescriptor = type.getSupertype(supertype); // Substitute actual supertype into the extractor, e.g. // TypeVariableExtractor<SerializableFunction<Integer, String>, Integer> TypeDescriptor<TypeVariableExtractor<T, V>> extractorT = extractorSupertype.where(inputT, supertypeDescriptor.getType()); // Get output of the extractor. Type outputT = ((ParameterizedType) extractorT.getType()).getActualTypeArguments()[1]; return (TypeDescriptor<V>) TypeDescriptor.of(outputT); }
private static FieldType getMapFieldType(TypeDescriptor typeDescriptor) { TypeDescriptor<Collection<?>> map = typeDescriptor.getSupertype(Map.class); if (map.getType() instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) map.getType(); java.lang.reflect.Type[] params = ptype.getActualTypeArguments(); return FieldType.map( fieldTypeForJavaType(TypeDescriptor.of(params[0])), fieldTypeForJavaType(TypeDescriptor.of(params[1]))); } throw new RuntimeException("Cound not determine array parameter type for field."); } }
@SuppressWarnings("unchecked") private static Implementation getMapType(TypeDescriptor valueType, int index) { if (valueType.isSubtypeOf(TypeDescriptor.of(Map.class))) { TypeDescriptor<Collection<?>> map = valueType.getSupertype(Map.class); if (map.getType() instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) map.getType(); java.lang.reflect.Type[] params = ptype.getActualTypeArguments(); return FixedValue.reference(params[index]); } else { throw new RuntimeException("Map type is not parameterized! " + map); } } return FixedValue.nullValue(); } }
(TypeDescriptor) stateSpecSubclassTypeDescriptor.getSupertype(StateSpec.class);
Type type = TypeDescriptor.of(subClass).getSupertype(baseClass).getType(); if (!(type instanceof ParameterizedType)) { throw new IllegalArgumentException(type + " is not a ParameterizedType");
@SuppressWarnings("unchecked") static Implementation getArrayComponentType(TypeDescriptor valueType) { if (valueType.isArray()) { Type component = valueType.getComponentType().getType(); if (!component.equals(byte.class)) { return FixedValue.reference(component); } } else if (valueType.isSubtypeOf(TypeDescriptor.of(Collection.class))) { TypeDescriptor<Collection<?>> collection = valueType.getSupertype(Collection.class); if (collection.getType() instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) collection.getType(); java.lang.reflect.Type[] params = ptype.getActualTypeArguments(); checkArgument(params.length == 1); return FixedValue.reference(params[0]); } else { throw new RuntimeException("Collection parameter is not parameterized!"); } } return FixedValue.nullValue(); }
(ParameterizedType) candidateOkDescriptor.getSupertype(codedClass).getType(); Type[] typeArguments = parameterizedSupertype.getActualTypeArguments(); List<? extends Coder<?>> typeArgumentCoders = coder.getCoderArguments();
private static FieldType getArrayFieldType(TypeDescriptor typeDescriptor) { if (typeDescriptor.isArray()) { if (typeDescriptor.getComponentType().getType().equals(byte.class)) { return FieldType.BYTES; } else { return FieldType.array(fieldTypeForJavaType(typeDescriptor.getComponentType())); } } if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Collection.class))) { TypeDescriptor<Collection<?>> collection = typeDescriptor.getSupertype(Collection.class); if (collection.getType() instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) collection.getType(); java.lang.reflect.Type[] params = ptype.getActualTypeArguments(); checkArgument(params.length == 1); return FieldType.array(fieldTypeForJavaType(TypeDescriptor.of(params[0]))); } } throw new RuntimeException("Could not determine array parameter type for field."); }
TypeDescriptor<Collection<?>> collection = type.getSupertype(Collection.class); if (collection.getType() instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) collection.getType(); TypeDescriptor<Collection<?>> map = type.getSupertype(Map.class); if (map.getType() instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) map.getType();