All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adgear.anoa.AnoaReflectionUtils Maven / Gradle / Ivy

package com.adgear.anoa;

import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;

import org.apache.avro.specific.SpecificRecord;
import org.apache.thrift.TBase;
import org.apache.thrift.TFieldIdEnum;
import org.apache.thrift.meta_data.FieldMetaData;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Utility class for properly-typed reflection methods.
 */
final public class AnoaReflectionUtils {

  static private Comparator> comparator
      = new Comparator>() {
    @Override
    public int compare(Map.Entry o1,
                       Map.Entry o2) {
      return o1.getKey().getThriftFieldId() - o2.getKey().getThriftFieldId();
    }
  };

  /**
   * @param className Avro SpecificRecord qualified class name
   */
  @SuppressWarnings("unchecked")
  static public Class getAvroClass(String className)
      throws ClassNotFoundException {
    if (className == null) {
      throw new ClassNotFoundException("Class name must not be null.");
    }
    Class recordClass = Class.forName(className);
    if (!SpecificRecord.class.isAssignableFrom(recordClass)) {
      throw new ClassCastException(className + " does not implement SpecificRecord.");
    }
    return (Class) recordClass;
  }

  /**
   * @param className Thrift qualified class name
   */
  @SuppressWarnings("unchecked")
  static public  Class getThriftClass(
      String className) throws ClassNotFoundException {
    if (className == null) {
      throw new ClassNotFoundException("Class name must not be null.");
    }
    Class recordClass = Class.forName(className);
    if (!TBase.class.isAssignableFrom(recordClass)) {
      throw new ClassCastException(className + " does not implement TBase.");
    }
    return (Class) recordClass;
  }

  /**
   * @param thriftClass the Thrift class
   * @param          Field enum type
   * @return An ordered map with field enum and field metadata for this class
   */
  @SuppressWarnings("unchecked")
  static public 
  LinkedHashMap getThriftMetaDataMap(Class> thriftClass) {
    LinkedHashMap result = new LinkedHashMap<>();
    ((Map) FieldMetaData.getStructMetaDataMap(thriftClass)).entrySet().stream()
        .sorted(comparator)
        .forEach(e -> result.put(e.getKey(), e.getValue()));
    return result;
  }

  /**
   * @param className Protobuf qualified class name
   */
  @SuppressWarnings("unchecked")
  static public  Class getProtobufClass(
      String className) throws ClassNotFoundException {
    if (className == null) {
      throw new ClassNotFoundException("Class name must not be null.");
    }
    Class recordClass = Class.forName(className);
    if (!Message.class.isAssignableFrom(recordClass)) {
      throw new ClassCastException(className + " does not implement Message.");
    }
    return (Class) recordClass;
  }

  /**
   * @param recordClass The Protobuf record class
   * @return The corresponding message descriptor
   */
  @SuppressWarnings("unchecked")
  static public Descriptors.Descriptor getProtobufDescriptor(Class recordClass) {
    try {
      return (Descriptors.Descriptor) recordClass.getMethod("getDescriptor").invoke(null);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @param recordClass The Protobuf record class
   * @return The corresponding message builder
   */
  static public MessageLite.Builder getProtobufBuilder(Class recordClass) {
    try {
      return (MessageLite.Builder) recordClass.getMethod("newBuilder").invoke(null);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @param recordClass The Protobuf record class
   * @param          Protobuf record type
   * @return The corresponding message parser
   */
  @SuppressWarnings("unchecked")
  static public  Parser getProtobufParser(Class recordClass) {
    try {
      return (Parser) recordClass.getDeclaredField("PARSER").get(null);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy