Java Code Examples for org.apache.avro.reflect.AvroName

The following examples show how to use org.apache.avro.reflect.AvroName. These examples are extracted from open source projects. 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 Project: beam   Source File: AvroCoder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Extract a field from a class. We need to look at the declared fields so that we can see
 * private fields. We may need to walk up to the parent to get classes from the parent.
 */
private static Field getField(Class<?> originalClazz, String name) {
  Class<?> clazz = originalClazz;
  while (clazz != null) {
    for (Field field : clazz.getDeclaredFields()) {
      AvroName avroName = field.getAnnotation(AvroName.class);
      if (avroName != null && name.equals(avroName.value())) {
        return field;
      } else if (avroName == null && name.equals(field.getName())) {
        return field;
      }
    }
    clazz = clazz.getSuperclass();
  }

  throw new IllegalArgumentException("Unable to get field " + name + " from " + originalClazz);
}
 
Example 2
Source Project: beam   Source File: AvroUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<FieldValueTypeInformation> get(Class<?> clazz) {
  Map<String, FieldValueTypeInformation> types = Maps.newHashMap();
  for (java.lang.reflect.Field f : ReflectUtils.getFields(clazz)) {
    if (!f.isAnnotationPresent(AvroIgnore.class)) {
      FieldValueTypeInformation typeInformation = FieldValueTypeInformation.forField(f);
      AvroName avroname = f.getAnnotation(AvroName.class);
      if (avroname != null) {
        typeInformation = typeInformation.withName(avroname.value());
      }
      types.put(typeInformation.getName(), typeInformation);
    }
  }
  return Lists.newArrayList(types.values());
}
 
Example 3
Source Project: parquet-mr   Source File: AvroRecordConverter.java    License: Apache License 2.0 5 votes vote down vote up
private static Map<String, Class<?>> getFieldsByName(Class<?> recordClass,
                                                     boolean excludeJava) {
  Map<String, Class<?>> fields = new LinkedHashMap<String, Class<?>>();

  if (recordClass != null) {
    Class<?> current = recordClass;
    do {
      if (excludeJava && current.getPackage() != null
          && current.getPackage().getName().startsWith("java.")) {
        break; // skip java built-in classes
      }
      for (Field field : current.getDeclaredFields()) {
        if (field.isAnnotationPresent(AvroIgnore.class) ||
            isTransientOrStatic(field)) {
          continue;
        }
        AvroName altName = field.getAnnotation(AvroName.class);
        Class<?> existing = fields.put(
            altName != null ? altName.value() : field.getName(),
            field.getType());
        if (existing != null) {
          throw new AvroTypeException(
              current + " contains two fields named: " + field.getName());
        }
      }
      current = current.getSuperclass();
    } while (current != null);
  }

  return fields;
}