本文研究的主要是Java方法签名的获取,下面是具体实现实例。
实例代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package com.yunshouhu;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collection;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
/**
* 获取Java的方法签名,参考javah -jni 类路径/javap -s 类路径。
* @author WuJianHua
* @date 2017年9月5日 下午3:25:51
* @url http://blog.csdn.net/earbao
*/
public class ASMUtilsForJavaH {
public static void main(String[] args) throws Exception {
System.out.println(ASMUtilsForJavaH.getDesc(System.class));
System.out.println(ASMUtilsForJavaH.getDesc(String.class));
System.out.println(ASMUtilsForJavaH.getDesc(Integer.class));
System.out.println(ASMUtilsForJavaH.getDesc(int.class));
Method method=ASMUtilsForJavaH.class.getDeclaredMethod("main", String[].class);
System.out.println("javah -jni");
System.out.println(ASMUtilsForJavaH.getDesc(method));
System.out.println(ASMUtilsForJavaH.getType(System.class));
System.out.println(ASMUtilsForJavaH.getType(ASMUtilsForJavaH.class));
}
public static Boolean isAndroid(final String vmName) {
final String lowerVMName = vmName.toLowerCase();
return lowerVMName.contains("dalvik") || lowerVMName.contains("lemur");
}
public static Boolean isAndroid() {
return isAndroid(System.getProperty("java.vm.name"));
}
public static String getDesc(final Method method) {
final StringBuffer buf = new StringBuffer();
buf.append("(");
final Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; ++i) {
buf.append(getDesc(types[i]));
}
buf.append(")");
buf.append(getDesc(method.getReturnType()));
return buf.toString();
}
public static String getDesc(final Class<?> returnType) {
if (returnType.isPrimitive()) {
return getPrimitiveLetter(returnType);
}
if (returnType.isArray()) {
return "[" + getDesc(returnType.getComponentType());
}
return "L" + getType(returnType) + ";";
}
public static String getType(final Class<?> parameterType) {
if (parameterType.isArray()) {
return "[" + getDesc(parameterType.getComponentType());
}
if (!parameterType.isPrimitive()) {
final String clsName = parameterType.getName();
return clsName.replaceAll("\\\\.", "/");
}
return getPrimitiveLetter(parameterType);
}
public static String getPrimitiveLetter(final Class<?> type) {
if (Integer.TYPE.equals(type)) {
return "I";
}
if (Void.TYPE.equals(type)) {
return "V";
}
if (Boolean.TYPE.equals(type)) {
return "Z";
}
if (Character.TYPE.equals(type)) {
return "C";
}
if (byte.TYPE.equals(type)) {
return "B";
}
if (short.TYPE.equals(type)) {
return "S";
}
if (float.TYPE.equals(type)) {
return "F";
}
if (long.TYPE.equals(type)) {
return "J";
}
if (double.TYPE.equals(type)) {
return "D";
}
throw new IllegalStateException("Type: " + type.getCanonicalName() + " is not a primitive type");
}
public static Type getMethodType(final Class<?> clazz, final String methodName) {
try {
final Method method = clazz.getMethod(methodName, (Class<?>[]) new Class[0]);
return method.getGenericReturnType();
}
catch (Exception ex) {
return null;
}
}
public static Type getFieldType(final Class<?> clazz, final String fieldName) {
try {
final Field field = clazz.getField(fieldName);
return field.getGenericType();
}
catch (Exception ex) {
return null;
}
}
public static void parseArray(final Collection collection, final ObjectDeserializer deser,
final DefaultJSONParser parser, final Type type, final Object fieldName) {
final JSONLexer lexer = parser.getLexer();
if (lexer.token() == 8) {
lexer.nextToken(16);
}
parser.accept(14, 14);
int index = 0;
while (true) {
final Object item = deser.deserialze(parser, type, (Object) index);
collection.add(item);
++index;
if (lexer.token() != 16) {
break;
}
lexer.nextToken(14);
}
parser.accept(15, 16);
}
}
|
总结
以上就是本文关于Java方法签名的获取实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://blog.csdn.net/earbao/article/details/77852044
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 31
-
2025-05-25 90
-
2025-05-25 88
-
2025-06-04 80
-
2025-06-04 45
热门评论

