Java Code Examples for org.apache.flink.api.java.typeutils.TypeExtractor#getDeclaredField()

The following examples show how to use org.apache.flink.api.java.typeutils.TypeExtractor#getDeclaredField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamSerializer.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
private Object getFieldValue(String fieldName, T input) {
    // TODO: Cache Field Accessor
    Field field = TypeExtractor.getDeclaredField(schema.getTypeInfo().getTypeClass(), fieldName);
    if (field == null) {
        throw new IllegalArgumentException(fieldName + " is not found in " + schema.getTypeInfo());
    }
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }
    try {
        return field.get(input);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example 2
Source File: StreamSerializer.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
private Object getFieldValue(String fieldName, T input) {
    // TODO: Cache Field Accessor
    Field field = TypeExtractor.getDeclaredField(schema.getTypeInfo().getTypeClass(), fieldName);
    if (field == null) {
        throw new IllegalArgumentException(fieldName + " is not found in " + schema.getTypeInfo());
    }
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }
    try {
        return field.get(input);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example 3
Source File: Types.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
/**
 * Returns type information for a POJO (Plain Old Java Object) and allows to specify all fields manually.
 *
 * <p>A POJO class is public and standalone (no non-static inner class). It has a public no-argument
 * constructor. All non-static, non-transient fields in the class (and all superclasses) are either public
 * (and non-final) or have a public getter and a setter method that follows the Java beans naming
 * conventions for getters and setters.
 *
 * <p>A POJO is a fixed-length, null-aware composite type with non-deterministic field order. Every field
 * can be null independent of the field's type.
 *
 * <p>The generic types for all fields of the POJO can be defined in a hierarchy of subclasses.
 *
 * <p>If Flink's type analyzer is unable to extract a POJO field, an
 * {@link org.apache.flink.api.common.functions.InvalidTypesException} is thrown.
 *
 * <p><strong>Note:</strong> In most cases the type information of fields can be determined automatically,
 * we recommend to use {@link Types#POJO(Class)}.
 *
 * @param pojoClass POJO class
 * @param fields map of fields that map a name to type information. The map key is the name of
 *               the field and the value is its type.
 */
public static <T> TypeInformation<T> POJO(Class<T> pojoClass, Map<String, TypeInformation<?>> fields) {
	final List<PojoField> pojoFields = new ArrayList<>(fields.size());
	for (Map.Entry<String, TypeInformation<?>> field : fields.entrySet()) {
		final Field f = TypeExtractor.getDeclaredField(pojoClass, field.getKey());
		if (f == null) {
			throw new InvalidTypesException("Field '" + field.getKey() + "'could not be accessed.");
		}
		pojoFields.add(new PojoField(f, field.getValue()));
	}

	return new PojoTypeInfo<>(pojoClass, pojoFields);
}
 
Example 4
Source File: Types.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Returns type information for a POJO (Plain Old Java Object) and allows to specify all fields manually.
 *
 * <p>A POJO class is public and standalone (no non-static inner class). It has a public no-argument
 * constructor. All non-static, non-transient fields in the class (and all superclasses) are either public
 * (and non-final) or have a public getter and a setter method that follows the Java beans naming
 * conventions for getters and setters.
 *
 * <p>A POJO is a fixed-length, null-aware composite type with non-deterministic field order. Every field
 * can be null independent of the field's type.
 *
 * <p>The generic types for all fields of the POJO can be defined in a hierarchy of subclasses.
 *
 * <p>If Flink's type analyzer is unable to extract a POJO field, an
 * {@link org.apache.flink.api.common.functions.InvalidTypesException} is thrown.
 *
 * <p><strong>Note:</strong> In most cases the type information of fields can be determined automatically,
 * we recommend to use {@link Types#POJO(Class)}.
 *
 * @param pojoClass POJO class
 * @param fields map of fields that map a name to type information. The map key is the name of
 *               the field and the value is its type.
 */
public static <T> TypeInformation<T> POJO(Class<T> pojoClass, Map<String, TypeInformation<?>> fields) {
	final List<PojoField> pojoFields = new ArrayList<>(fields.size());
	for (Map.Entry<String, TypeInformation<?>> field : fields.entrySet()) {
		final Field f = TypeExtractor.getDeclaredField(pojoClass, field.getKey());
		if (f == null) {
			throw new InvalidTypesException("Field '" + field.getKey() + "'could not be accessed.");
		}
		pojoFields.add(new PojoField(f, field.getValue()));
	}

	return new PojoTypeInfo<>(pojoClass, pojoFields);
}
 
Example 5
Source File: Types.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Returns type information for a POJO (Plain Old Java Object) and allows to specify all fields manually.
 *
 * <p>A POJO class is public and standalone (no non-static inner class). It has a public no-argument
 * constructor. All non-static, non-transient fields in the class (and all superclasses) are either public
 * (and non-final) or have a public getter and a setter method that follows the Java beans naming
 * conventions for getters and setters.
 *
 * <p>A POJO is a fixed-length, null-aware composite type with non-deterministic field order. Every field
 * can be null independent of the field's type.
 *
 * <p>The generic types for all fields of the POJO can be defined in a hierarchy of subclasses.
 *
 * <p>If Flink's type analyzer is unable to extract a POJO field, an
 * {@link org.apache.flink.api.common.functions.InvalidTypesException} is thrown.
 *
 * <p><strong>Note:</strong> In most cases the type information of fields can be determined automatically,
 * we recommend to use {@link Types#POJO(Class)}.
 *
 * @param pojoClass POJO class
 * @param fields map of fields that map a name to type information. The map key is the name of
 *               the field and the value is its type.
 */
public static <T> TypeInformation<T> POJO(Class<T> pojoClass, Map<String, TypeInformation<?>> fields) {
	final List<PojoField> pojoFields = new ArrayList<>(fields.size());
	for (Map.Entry<String, TypeInformation<?>> field : fields.entrySet()) {
		final Field f = TypeExtractor.getDeclaredField(pojoClass, field.getKey());
		if (f == null) {
			throw new InvalidTypesException("Field '" + field.getKey() + "'could not be accessed.");
		}
		pojoFields.add(new PojoField(f, field.getValue()));
	}

	return new PojoTypeInfo<>(pojoClass, pojoFields);
}