Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.qq.tars.protocol.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.qq.tars.common.support.Holder;
import com.qq.tars.common.util.BeanAccessor;
import com.qq.tars.common.util.CommonUtils;
import com.qq.tars.protocol.annotation.Servant;
import com.qq.tars.protocol.tars.annotation.TarsCallback;
import com.qq.tars.protocol.tars.annotation.TarsContext;
import com.qq.tars.protocol.tars.annotation.TarsHolder;
import com.qq.tars.protocol.tars.annotation.TarsMethod;
import com.qq.tars.protocol.tars.annotation.TarsMethodParameter;
import com.qq.tars.protocol.tars.annotation.TarsRouteKey;
import com.qq.tars.protocol.tars.annotation.TarsStruct;
import com.qq.tars.protocol.tars.annotation.TarsStructProperty;
import com.qq.tars.protocol.tars.support.TarsMethodInfo;
import com.qq.tars.protocol.tars.support.TarsMethodParameterInfo;
import com.qq.tars.protocol.tars.support.TarsStructInfo;
import com.qq.tars.protocol.tars.support.TarsStrutPropertyInfo;
public class TarsHelper {
public final static int PACKAGE_MAX_LENGTH = 10 * 1024 * 1024;
public final static int HEAD_SIZE = 4;
public static final short VERSION = 0x01;
public static final short VERSION2 = 0x02;
public static final short VERSION3 = 0x03;
public static final byte NORMAL = 0x00;
public static final byte ONEWAY = 0x01;
public final static int SERVERSUCCESS = 0;
public final static int SERVERDECODEERR = -1;
public final static int SERVERENCODEERR = -2;
public final static int SERVERNOFUNCERR = -3;
public final static int SERVERNOSERVANTERR = -4;
public final static int SERVERRESETGRID = -5;
public final static int SERVERQUEUETIMEOUT = -6;
public final static int ASYNCCALLTIMEOUT = -7;
public final static int PROXYCONNECTERR = -8;
public static final int SERVEROVERLOAD = -9;
public final static int SERVERUNKNOWNERR = -99;
public static final int MESSAGETYPENULL = 0x00;
public static final int MESSAGETYPEHASH = 0x01;
public static final int MESSAGETYPEGRID = 0x02;
public static final int MESSAGETYPEDYED = 0x04;
public static final int MESSAGETYPESAMPLE = 0x08;
public static final int MESSAGETYPEASYNC = 0x10;
public static final int MESSAGETYPELOADED = 0x20;
public static final String STATUS_RESULT_CODE = "STATUS_RESULT_CODE";
public static final String STATUS_RESULT_DESC = "STATUS_RESULT_DESC";
public static final Boolean STAMP_BOOLEAN = Boolean.TRUE;
public static final Byte STAMP_BYTE = Byte.valueOf((byte) 0);
public static final Short STAMP_SHORT = Short.valueOf((short) 0);
public static final Float STAMP_FLOAT = Float.valueOf(0);
public static final Integer STAMP_INT = Integer.valueOf(0);
public static final Long STAMP_LONG = Long.valueOf(0);
public static final Double STAMP_DOUBLE = Double.valueOf(0);
public static final String STAMP_STRING = "";
public static final boolean[] STAMP_BOOLEAN_ARRAY = new boolean[] { true };
public static final byte[] STAMP_BYTE_ARRAY = new byte[] { 0 };
public static final short[] STAMP_SHORT_ARRAY = new short[] { 0 };
public static final int[] STAMP_INT_ARRAY = new int[] { 0 };
public static final long[] STAMP_LONG_ARRAY = new long[] { 0 };
public static final float[] STAMP_FLOAT_ARRAY = new float[] { 0 };
public static final double[] STAMP_DOUBLE_ARRAY = new double[] { 0 };
public static final Map STAMP_MAP = new HashMap();
static {
STAMP_MAP.put("", "");
}
private static Map, TarsStructInfo> tarsStructCache = new HashMap, TarsStructInfo>();
private static Comparator tarsStructFieldsListComparator = new Comparator() {
@Override
public int compare(Field one, Field other) {
TarsStructProperty oneProperty = one.getAnnotation(TarsStructProperty.class);
TarsStructProperty otherProperty = other.getAnnotation(TarsStructProperty.class);
if (oneProperty.order() == otherProperty.order()) {
throw new RuntimeException("Field[" + one.getName() + "] , Field[" + other.getName() + "] order is:" + oneProperty.order());
}
return oneProperty.order() - otherProperty.order();
}
};
private static Map, Object> stampCache = new HashMap, Object>();
public static Object getJavaBaseOrArrayOrJavaBeanStamp(Class> clazz) {
if (clazz == boolean.class || clazz == Boolean.class) {
return STAMP_BOOLEAN;
} else if (clazz == byte.class || clazz == Byte.class) {
return STAMP_BYTE;
} else if (clazz == short.class || clazz == Short.class) {
return STAMP_SHORT;
} else if (clazz == int.class || clazz == Integer.class) {
return STAMP_INT;
} else if (clazz == long.class || clazz == Long.class) {
return STAMP_LONG;
} else if (clazz == float.class || clazz == Float.class) {
return STAMP_FLOAT;
} else if (clazz == double.class || clazz == Double.class) {
return STAMP_DOUBLE;
} else if (clazz == String.class) {
return STAMP_STRING;
} else if (clazz == boolean[].class) {
return STAMP_BOOLEAN_ARRAY;
} else if (clazz == byte[].class) {
return STAMP_BYTE_ARRAY;
} else if (clazz == short[].class) {
return STAMP_SHORT_ARRAY;
} else if (clazz == int[].class) {
return STAMP_INT_ARRAY;
} else if (clazz == long[].class) {
return STAMP_LONG_ARRAY;
} else if (clazz == float[].class) {
return STAMP_FLOAT_ARRAY;
} else if (clazz == double[].class) {
return STAMP_DOUBLE_ARRAY;
}
Object stamp = stampCache.get(clazz);
if (stamp == null) {
if (clazz.isArray()) {
Class> componentType = clazz.getComponentType();
Object[] array = (Object[]) Array.newInstance(componentType, 1);
Object e = getJavaBaseOrArrayOrJavaBeanStamp(componentType);
array[0] = e;
stamp = array;
} else {
stamp = CommonUtils.newInstance(clazz);
}
stampCache.put(clazz, stamp);
}
return stamp;
}
public static String getParameterName(Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (annotation.annotationType() == TarsMethodParameter.class) {
TarsMethodParameter parameter = (TarsMethodParameter) annotation;
return parameter.name();
} else if (annotation.annotationType() == TarsHolder.class) {
TarsHolder tarsHolder = (TarsHolder) annotation;
return tarsHolder.name();
}
}
return null;
}
public static Object getParameterStamp(Type type) {
if (type instanceof Class>) {
Class> clazz = (Class>) type;
if (CommonUtils.isJavaBase(clazz) || clazz.isArray() || isStruct(clazz)) {
return getJavaBaseOrArrayOrJavaBeanStamp((Class>) type);
} else {
return clazz;
// throw new RuntimeException("the class: " + clazz + " not a exact class, please check it.");
}
} else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Class> clazz = (Class>) parameterizedType.getRawType();
if (isStruct(clazz)) {
return getJavaBaseOrArrayOrJavaBeanStamp((Class>) type);
} else if (isMap(clazz)) {
Type[] types = parameterizedType.getActualTypeArguments();
Type keyType = types[0];
Type valueType = types[1];
Object key = getParameterStamp(keyType);
Object value = getParameterStamp(valueType);
Map