use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class JacksonHelper method loadClassToCache.
/**
* load method paramters and return types to cache, will not pass through to next
*
* @param key key
* @param clazz interface name
* @param methodName method name
*/
private void loadClassToCache(String key, Class clazz, String methodName) {
Method jsonMethod = null;
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
jsonMethod = method;
break;
}
}
if (jsonMethod == null) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_METHOD_NOT_FOUND, clazz.getName(), methodName));
}
// parse request types
Type[] parameterTypes = jsonMethod.getGenericParameterTypes();
JavaType[] javaTypes = new JavaType[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
JavaType javaType = mapper.getTypeFactory().constructType(parameterTypes[i]);
javaTypes[i] = javaType;
}
requestClassCache.put(key, javaTypes);
// parse response types
Type resType = jsonMethod.getGenericReturnType();
if (resType == void.class) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_VOID_RETURN, "jackson", clazz.getName()));
}
JavaType resJavaType = mapper.getTypeFactory().constructType(resType);
responseClassCache.put(key, resJavaType);
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ProtobufHelper method loadProtoClassToCache.
/**
* 加载protobuf接口里方法的参数和返回值类型到缓存,不需要传递
*
* @param key 缓存的key
* @param clazz 接口名
* @param methodName 方法名
*/
private void loadProtoClassToCache(String key, Class clazz, String methodName) {
Method pbMethod = null;
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
pbMethod = method;
break;
}
}
if (pbMethod == null) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_METHOD_NOT_FOUND, clazz.getName(), methodName));
}
Class[] parameterTypes = pbMethod.getParameterTypes();
if (parameterTypes == null || parameterTypes.length != 1 || isProtoBufMessageObject(parameterTypes[0])) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_ONLY_ONE_PARAM, "protobuf", clazz.getName()));
}
Class reqClass = parameterTypes[0];
requestClassCache.put(key, reqClass);
Class resClass = pbMethod.getReturnType();
if (resClass == void.class || !isProtoBufMessageClass(resClass)) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_PROTOBUF_RETURN, clazz.getName()));
}
responseClassCache.put(key, resClass);
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ClassUtils method newInstanceWithArgs.
/**
* 实例化一个对象(根据参数自动检测构造方法)
*
* @param clazz 对象类
* @param argTypes 构造函数需要的参数
* @param args 构造函数需要的参数
* @param <T> 对象具体类
* @return 对象实例
* @throws SofaRpcRuntimeException 没有找到方法,或者无法处理,或者初始化方法异常等
*/
public static <T> T newInstanceWithArgs(Class<T> clazz, Class<?>[] argTypes, Object[] args) throws SofaRpcRuntimeException {
if (CommonUtils.isEmpty(argTypes)) {
return newInstance(clazz);
}
try {
if (!(clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers()))) {
Constructor<T> constructor = clazz.getDeclaredConstructor(argTypes);
constructor.setAccessible(true);
return constructor.newInstance(args);
} else {
Constructor<T>[] constructors = (Constructor<T>[]) clazz.getDeclaredConstructors();
if (constructors == null || constructors.length == 0) {
throw new SofaRpcRuntimeException("The " + clazz.getCanonicalName() + " has no constructor with argTypes :" + Arrays.toString(argTypes));
}
Constructor<T> constructor = null;
for (Constructor<T> c : constructors) {
Class[] ps = c.getParameterTypes();
if (ps.length == argTypes.length + 1) {
// 长度多一
boolean allMath = true;
for (int i = 1; i < ps.length; i++) {
// 而且第二个开始的参数类型匹配
if (ps[i] != argTypes[i - 1]) {
allMath = false;
break;
}
}
if (allMath) {
constructor = c;
break;
}
}
}
if (constructor == null) {
throw new SofaRpcRuntimeException("The " + clazz.getCanonicalName() + " has no constructor with argTypes :" + Arrays.toString(argTypes));
} else {
constructor.setAccessible(true);
Object[] newArgs = new Object[args.length + 1];
System.arraycopy(args, 0, newArgs, 1, args.length);
return constructor.newInstance(newArgs);
}
}
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
throw new SofaRpcRuntimeException(e.getMessage(), e);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class HashUtils method messageDigest.
/**
* 换算法? MD5 SHA-1 MurMurHash???
*
* @param value the value
* @return the byte []
*/
public static byte[] messageDigest(String value) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
md5.update(value.getBytes("UTF-8"));
return md5.digest();
} catch (NoSuchAlgorithmException e) {
throw new SofaRpcRuntimeException("No such algorithm named md5", e);
} catch (UnsupportedEncodingException e) {
throw new SofaRpcRuntimeException("Unsupported encoding of" + value, e);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class BeanUtils method copyPropertiesToMap.
/**
* 复制属性到map,可以自定义前缀
*
* @param bean 对象
* @param prefix 放入key的前缀
* @param map 要写入的map
*/
public static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map) {
Class clazz = bean.getClass();
Method[] methods = bean.getClass().getMethods();
for (Method method : methods) {
// 复制属性
Class returnc = method.getReturnType();
if (ReflectUtils.isBeanPropertyReadMethod(method)) {
String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(method);
try {
if (ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnc) == null) {
// 还需要有set方法
continue;
}
} catch (Exception e) {
continue;
}
Object val;
try {
val = method.invoke(bean);
} catch (InvocationTargetException e) {
throw new SofaRpcRuntimeException("Can't access copy " + propertyName, e.getCause());
} catch (IllegalAccessException e) {
throw new SofaRpcRuntimeException("Can't access copy " + propertyName, e);
}
if (val != null) {
// 值不为空,放入缓存
map.put(prefix + propertyName, val);
}
}
}
Field[] fields = bean.getClass().getFields();
for (Field field : fields) {
String fieldName = field.getName();
if (map.containsKey(prefix + fieldName)) {
continue;
}
int m = field.getModifiers();
if (!Modifier.isStatic(m) && !Modifier.isTransient(m)) {
Object val = null;
try {
if (field.isAccessible()) {
val = field.get(bean);
} else {
try {
field.setAccessible(true);
val = field.get(bean);
} finally {
field.setAccessible(false);
}
}
} catch (IllegalAccessException e) {
// LOGGER.warn("Can't access field" + fieldName + "when copy value to context", e);
}
if (val != null) {
map.put(prefix + fieldName, val);
}
}
}
}
Aggregations