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

com.microsoft.thrifty.gen.TypeNames.kt Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * Thrifty
 *
 * Copyright (c) Microsoft Corporation
 *
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
 * WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE,
 * FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
 *
 * See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
 */
package com.microsoft.thrifty.gen

import com.microsoft.thrifty.Adapter
import com.microsoft.thrifty.Obfuscated
import com.microsoft.thrifty.Redacted
import com.microsoft.thrifty.StructBuilder
import com.microsoft.thrifty.TType
import com.microsoft.thrifty.ThriftException
import com.microsoft.thrifty.ThriftField
import com.microsoft.thrifty.protocol.FieldMetadata
import com.microsoft.thrifty.protocol.ListMetadata
import com.microsoft.thrifty.protocol.MapMetadata
import com.microsoft.thrifty.protocol.MessageMetadata
import com.microsoft.thrifty.protocol.Protocol
import com.microsoft.thrifty.protocol.SetMetadata
import com.microsoft.thrifty.service.AsyncClientBase
import com.microsoft.thrifty.service.MethodCall
import com.microsoft.thrifty.service.ServiceMethodCallback
import com.microsoft.thrifty.service.TMessageType
import com.microsoft.thrifty.util.ObfuscationUtil
import com.microsoft.thrifty.util.ProtocolUtil
import com.squareup.javapoet.ClassName
import okio.ByteString

import java.io.IOException
import java.net.ProtocolException
import java.util.Collections
import java.util.NoSuchElementException

/**
 * JavaPoet type names used for code generation.
 */
internal object TypeNames {
    val BOOLEAN = ClassName.BOOLEAN.box()
    val BYTE = ClassName.BYTE.box()
    val SHORT = ClassName.SHORT.box()
    val INTEGER = ClassName.INT.box()
    val LONG = ClassName.LONG.box()
    val DOUBLE = ClassName.DOUBLE.box()
    val VOID = ClassName.VOID // Don't box void, it is only used for methods returning nothing.

    val COLLECTIONS = classNameOf()
    val STRING = classNameOf()
    val LIST = classNameOf>()
    val MAP = classNameOf>()
    val MAP_ENTRY = classNameOf>()
    val SET = classNameOf>()
    val BYTE_STRING = classNameOf()
    val STRING_BUILDER = classNameOf()
    val ILLEGAL_STATE_EXCEPTION = classNameOf()
    val ILLEGAL_ARGUMENT_EXCEPTION = classNameOf()
    val NULL_POINTER_EXCEPTION = classNameOf()

    val ARRAY_LIST = classNameOf>()
    val HASH_SET = classNameOf>()
    val HASH_MAP = classNameOf>()

    val LIST_META = classNameOf()
    val SET_META = classNameOf()
    val MAP_META = classNameOf()

    val PROTOCOL = classNameOf()
    val PROTO_UTIL = classNameOf()
    val PROTOCOL_EXCEPTION = classNameOf()
    val IO_EXCEPTION = classNameOf()
    val EXCEPTION = classNameOf()
    val TTYPE = classNameOf()
    val TMESSAGE_TYPE = classNameOf()

    val THRIFT_EXCEPTION = classNameOf()
    val THRIFT_EXCEPTION_KIND = classNameOf()

    val BUILDER = classNameOf>()
    val ADAPTER = classNameOf>()

    val FIELD_METADATA = classNameOf()
    val MESSAGE_METADATA = classNameOf()

    val OVERRIDE = classNameOf()
    val DEPRECATED = classNameOf()
    val SUPPRESS_WARNINGS = classNameOf()
    val REDACTED = classNameOf()
    val OBFUSCATED = classNameOf()
    val THRIFT_FIELD = classNameOf()

    val NOT_NULL = ClassName.get("android.support.annotation", "NonNull")
    val NULLABLE = ClassName.get("android.support.annotation", "Nullable")

    val SERVICE_CALLBACK = classNameOf>()
    val SERVICE_CLIENT_BASE = classNameOf()
    val SERVICE_CLIENT_LISTENER = classNameOf()
    val SERVICE_METHOD_CALL = classNameOf>()

    val PARCEL = ClassName.get("android.os", "Parcel")
    val PARCELABLE = ClassName.get("android.os", "Parcelable")
    val PARCELABLE_CREATOR = ClassName.get("android.os", "Parcelable", "Creator")

    val OBFUSCATION_UTIL = classNameOf()

    /**
     * Gets the [TType] member name corresponding to the given type-code.
     *
     * @param code the code whose name is needed
     * @return the TType member name as a string
     */
    fun getTypeCodeName(code: Byte): String {
        return when(code) {
            TType.BOOL -> "BOOL"
            TType.BYTE -> "BYTE"
            TType.I16 -> "I16"
            TType.I32 -> "I32"
            TType.I64 -> "I64"
            TType.DOUBLE -> "DOUBLE"
            TType.STRING -> "STRING"
            TType.STRUCT -> "STRUCT"
            TType.LIST -> "LIST"
            TType.SET -> "SET"
            TType.MAP -> "MAP"
            TType.VOID -> "VOID"
            TType.STOP -> "STOP"
            else -> throw NoSuchElementException("not a TType member: $code")
        }
    }
}

internal inline fun  classNameOf(): ClassName = ClassName.get(T::class.java)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy