Example usage for org.apache.thrift.meta_data FieldMetaData getStructMetaDataMap

List of usage examples for org.apache.thrift.meta_data FieldMetaData getStructMetaDataMap

Introduction

In this page you can find the example usage for org.apache.thrift.meta_data FieldMetaData getStructMetaDataMap.

Prototype

public static synchronized Hashtable getStructMetaDataMap(Class sClass) 

Source Link

Document

Returns a map with metadata (i.e.

Usage

From source file:com.linecorp.armeria.server.thrift.ThriftDocServicePlugin.java

License:Apache License

@VisibleForTesting
static StructInfo newStructInfo(Class<? extends TBase<?, ?>> structClass) {
    final String name = structClass.getName();

    final Map<?, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(structClass);
    final List<FieldInfo> fields = metaDataMap.values().stream()
            .map(fieldMetaData -> newFieldInfo(structClass, fieldMetaData)).collect(Collectors.toList());

    return new StructInfo(name, fields);
}

From source file:com.linecorp.armeria.server.thrift.ThriftFunction.java

License:Apache License

private ThriftFunction(String name, Object func, Type type, Class<TBase<TBase<?, ?>, TFieldIdEnum>> resultType)
        throws Exception {

    this.name = name;
    this.func = func;
    this.type = type;

    // Determine the parameter types of the function.
    paramTypes = Collections.unmodifiableList(FieldMetaData.getStructMetaDataMap(newArgs().getClass()).values()
            .stream().map(e -> ThriftUtil.toJavaType(e.valueMetaData)).collect(Collectors.toList()));

    // Determine the success and exception fields of the function.
    TFieldIdEnum successField = null;/*from   w  w w. j  a  v  a2  s .c om*/
    FieldValueMetaData successFieldMetadata = null;

    if (resultType != null) {
        result = resultType.newInstance();

        @SuppressWarnings("unchecked")
        final Map<TFieldIdEnum, FieldMetaData> metaDataMap = (Map<TFieldIdEnum, FieldMetaData>) FieldMetaData
                .getStructMetaDataMap(resultType);

        for (Entry<TFieldIdEnum, FieldMetaData> e : metaDataMap.entrySet()) {
            final TFieldIdEnum key = e.getKey();
            final String fieldName = key.getFieldName();
            if ("success".equals(fieldName)) {
                successField = key;
                successFieldMetadata = e.getValue().valueMetaData;
                continue;
            }

            Class<?> fieldType = resultType.getField(fieldName).getType();
            if (Throwable.class.isAssignableFrom(fieldType)) {
                @SuppressWarnings("unchecked")
                Class<Throwable> exceptionFieldType = (Class<Throwable>) fieldType;
                exceptionFields.put(exceptionFieldType, key);
            }
        }
    } else {
        result = null;
    }

    this.successField = successField;

    if (successFieldMetadata != null) {
        returnType = ThriftUtil.toJavaType(successFieldMetadata);
    } else {
        returnType = Void.class;
    }
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceSpecificationGenerator.java

License:Apache License

private static FunctionInfo newFunctionInfo(String namespace, String name,
        Class<? extends TBase<?, ?>> argsClass, @Nullable Class<? extends TBase<?, ?>> resultClass,
        Class<? extends TException>[] exceptionClasses, String sampleJsonRequest,
        Map<String, String> docStrings) {
    requireNonNull(name, "name");
    final String functionNamespace = ThriftDocString.key(namespace, name);
    final String docString = docStrings.get(functionNamespace);
    requireNonNull(argsClass, "argsClass");
    requireNonNull(exceptionClasses, "exceptionClasses");

    final List<FieldInfo> parameters = FieldMetaData.getStructMetaDataMap(argsClass).values().stream()
            .map(fieldMetaData -> newFieldInfo(fieldMetaData, functionNamespace, docStrings))
            .collect(toImmutableList());

    // Find the 'success' field.
    FieldInfo fieldInfo = null;//from w ww .j  a va2  s.  c  o  m
    if (resultClass != null) { // Function isn't "oneway" function
        final Map<? extends TFieldIdEnum, FieldMetaData> resultMetaData = FieldMetaData
                .getStructMetaDataMap(resultClass);

        for (FieldMetaData fieldMetaData : resultMetaData.values()) {
            if ("success".equals(fieldMetaData.fieldName)) {
                fieldInfo = newFieldInfo(fieldMetaData, functionNamespace, docStrings);
                break;
            }
        }
    }

    final TypeInfo returnType;
    if (fieldInfo == null) {
        returnType = TypeInfo.VOID;
    } else {
        returnType = fieldInfo.typeInfo();
    }

    final List<ExceptionInfo> exceptions = Arrays.stream(exceptionClasses).filter(e -> e != TException.class)
            .map(e -> newExceptionInfo(e, docStrings)).collect(toImmutableList());

    return new FunctionInfo(name, returnType, parameters, exceptions, sampleJsonRequest, docString);
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceSpecificationGenerator.java

License:Apache License

@VisibleForTesting
static StructInfo newStructInfo(StructMetaData structMetaData, Map<String, String> docStrings) {
    final Class<?> structClass = structMetaData.structClass;
    final String name = structClass.getName();

    assert structMetaData.type == TType.STRUCT;
    assert !structMetaData.isBinary();

    final Map<?, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(structMetaData.structClass);
    final List<FieldInfo> fields = metaDataMap.values().stream()
            .map(fieldMetaData -> newFieldInfo(fieldMetaData, name, docStrings)).collect(Collectors.toList());

    return new StructInfo(name, fields, docStrings.get(name));
}

From source file:com.linecorp.armeria.server.thrift.THttpService.java

License:Apache License

private static RpcRequest toRpcRequest(Class<?> serviceType, String method, TBase<?, ?> thriftArgs) {
    requireNonNull(thriftArgs, "thriftArgs");

    @SuppressWarnings("unchecked")
    final TBase<TBase<?, ?>, TFieldIdEnum> castThriftArgs = (TBase<TBase<?, ?>, TFieldIdEnum>) thriftArgs;

    // NB: The map returned by FieldMetaData.getStructMetaDataMap() is an EnumMap,
    //     so the parameter ordering is preserved correctly during iteration.
    final Set<? extends TFieldIdEnum> fields = FieldMetaData.getStructMetaDataMap(castThriftArgs.getClass())
            .keySet();/*w w w  .j  ava2  s.c om*/

    // Handle the case where the number of arguments is 0 or 1.
    final int numFields = fields.size();
    switch (numFields) {
    case 0:
        return RpcRequest.of(serviceType, method);
    case 1:
        return RpcRequest.of(serviceType, method,
                ThriftFieldAccess.get(castThriftArgs, fields.iterator().next()));
    }

    // Handle the case where the number of arguments is greater than 1.
    final List<Object> list = new ArrayList<>(numFields);
    for (TFieldIdEnum field : fields) {
        list.add(ThriftFieldAccess.get(castThriftArgs, field));
    }

    return RpcRequest.of(serviceType, method, list);
}

From source file:com.twitter.common.thrift.text.StructContext.java

License:Apache License

/**
 * Compute a new field name map for the current thrift message
 * we are parsing.//www  .ja  v a 2  s. com
 */
private Map<String, TField> computeFieldNameMap() {
    Map<String, TField> map = new HashMap<String, TField>();

    Class<? extends TBase> clazz = getCurrentThriftMessageClass();

    // Get the metaDataMap for this Thrift class
    Map<? extends TFieldIdEnum, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(clazz);

    for (TFieldIdEnum key : metaDataMap.keySet()) {
        final String fieldName = key.getFieldName();
        final FieldMetaData metaData = metaDataMap.get(key);

        // Workaround a bug in the generated thrift message read()
        // method by mapping the ENUM type to the INT32 type
        // The thrift generated parsing code requires that, when expecting
        // a value of enum, we actually parse a value of type int32. The
        // generated read() method then looks up the enum value in a map.
        byte type = (TType.ENUM == metaData.valueMetaData.type) ? TType.I32 : metaData.valueMetaData.type;

        map.put(fieldName, new TField(fieldName, type, key.getThriftFieldId()));
    }
    return map;
}

From source file:com.twitter.common.thrift.Util.java

License:Apache License

/**
 * Prints a TBase./* ww w . j a va2 s  .  co  m*/
 *
 * @param t The object to print.
 * @param depth The print nesting level.
 * @return The pretty-printed version of the TBase.
 */
private static String printTbase(TBase t, int depth) {
    List<String> fields = Lists.newArrayList();
    for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> entry : FieldMetaData
            .getStructMetaDataMap(t.getClass()).entrySet()) {
        @SuppressWarnings("unchecked")
        Object value = t.getFieldValue(entry.getKey());
        fields.add(tabs(depth) + entry.getValue().fieldName + ": " + printValue(value, depth));
    }

    return Joiner.on("\n").join(fields);
}

From source file:ezbake.deployer.utilities.Utilities.java

License:Apache License

private static String ppTBaseObject(TBase t, int depth) {
    List<String> fields = Lists.newArrayList();
    final String indent = Strings.repeat("  ", depth);
    for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> entry : FieldMetaData
            .getStructMetaDataMap(t.getClass()).entrySet()) {
        fields.add(indent + entry.getValue().fieldName + ": " + ppTBaseField(t, entry, depth));
    }/*from w  w w .ja va 2  s  .  c o m*/
    return Joiner.on("\n").join(fields);
}

From source file:ezbake.thrift.utils.xml2thrift.util.TStructDescriptor.java

License:Apache License

private void build(Class<? extends TBase<?, ?>> tClass) {
    Map<? extends TFieldIdEnum, FieldMetaData> fieldMap = FieldMetaData.getStructMetaDataMap(tClass);
    Field[] arr = new Field[fieldMap.size()];

    isUnion = TUnion.class.isAssignableFrom(tClass);

    int idx = 0;//  w ww .  j a  v a2s  .  com
    for (Entry<? extends TFieldIdEnum, FieldMetaData> e : fieldMap.entrySet()) {
        String fieldName = e.getKey().getFieldName();
        arr[idx++] = new Field(e.getKey(), e.getValue(), fieldName, ThriftUtils.getFieldType(tClass, fieldName),
                e.getValue().valueMetaData);
    }
    // make it immutable since users have access.
    fields = ImmutableList.copyOf(arr);
}

From source file:org.apache.aurora.common.thrift.Util.java

License:Apache License

/**
 * Prints a TBase.//from   ww w .  j  av a  2  s. com
 *
 * @param t The object to print.
 * @param depth The print nesting level.
 * @return The pretty-printed version of the TBase.
 */
private static String printTbase(TBase t, int depth) {
    List<String> fields = Lists.newArrayList();
    for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> entry : FieldMetaData
            .getStructMetaDataMap(t.getClass()).entrySet()) {
        @SuppressWarnings("unchecked")
        boolean fieldSet = t.isSet(entry.getKey());
        String strValue;
        if (fieldSet) {
            @SuppressWarnings("unchecked")
            Object value = t.getFieldValue(entry.getKey());
            strValue = printValue(value, depth);
        } else {
            strValue = "not set";
        }
        fields.add(tabs(depth) + entry.getValue().fieldName + ": " + strValue);
    }

    return Joiner.on("\n").join(fields);
}