/**
|
* Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
|
* <p>
|
* 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
|
* <p>
|
* http://www.apache.org/licenses/LICENSE-2.0
|
* <p>
|
* 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 tech.aiflowy.common.web.devlog;
|
|
|
import java.lang.reflect.Method;
|
|
|
/**
|
* 参考 https://github.com/apache/dubbo/blob/master/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java
|
*/
|
public final class DevLogUtil {
|
|
/**
|
* void(V).
|
*/
|
public static final char JVM_VOID = 'V';
|
|
/**
|
* boolean(Z).
|
*/
|
public static final char JVM_BOOLEAN = 'Z';
|
|
/**
|
* byte(B).
|
*/
|
public static final char JVM_BYTE = 'B';
|
|
/**
|
* char(C).
|
*/
|
public static final char JVM_CHAR = 'C';
|
|
/**
|
* double(D).
|
*/
|
public static final char JVM_DOUBLE = 'D';
|
|
/**
|
* float(F).
|
*/
|
public static final char JVM_FLOAT = 'F';
|
|
/**
|
* int(I).
|
*/
|
public static final char JVM_INT = 'I';
|
|
/**
|
* long(J).
|
*/
|
public static final char JVM_LONG = 'J';
|
|
/**
|
* short(S).
|
*/
|
public static final char JVM_SHORT = 'S';
|
|
|
/**
|
* get class desc.
|
* boolean[].class => "[Z"
|
* Object.class => "Ljava/lang/Object;"
|
*
|
* @param c class.
|
* @return desc.
|
*/
|
public static String getDesc(Class<?> c) {
|
StringBuilder ret = new StringBuilder();
|
|
while (c.isArray()) {
|
ret.append('[');
|
c = c.getComponentType();
|
}
|
|
if (c.isPrimitive()) {
|
String t = c.getName();
|
if ("void".equals(t)) {
|
ret.append(JVM_VOID);
|
} else if ("boolean".equals(t)) {
|
ret.append(JVM_BOOLEAN);
|
} else if ("byte".equals(t)) {
|
ret.append(JVM_BYTE);
|
} else if ("char".equals(t)) {
|
ret.append(JVM_CHAR);
|
} else if ("double".equals(t)) {
|
ret.append(JVM_DOUBLE);
|
} else if ("float".equals(t)) {
|
ret.append(JVM_FLOAT);
|
} else if ("int".equals(t)) {
|
ret.append(JVM_INT);
|
} else if ("long".equals(t)) {
|
ret.append(JVM_LONG);
|
} else if ("short".equals(t)) {
|
ret.append(JVM_SHORT);
|
}
|
} else {
|
ret.append('L');
|
ret.append(c.getName().replace('.', '/'));
|
ret.append(';');
|
}
|
return ret.toString();
|
}
|
|
|
/**
|
* get method desc.
|
* "(I)I", "()V", "(Ljava/lang/String;Z)V"
|
*
|
* @param m method.
|
* @return desc.
|
*/
|
public static String getMethodDescWithoutName(Method m) {
|
StringBuilder ret = new StringBuilder();
|
ret.append('(');
|
Class<?>[] parameterTypes = m.getParameterTypes();
|
for (int i = 0; i < parameterTypes.length; i++) {
|
ret.append(getDesc(parameterTypes[i]));
|
}
|
ret.append(')').append(getDesc(m.getReturnType()));
|
return ret.toString();
|
}
|
|
}
|