我是靠谱客的博主 激情机器猫,这篇文章主要介绍利用Javassist获取Java类中的方法参数名、参数类型、方法访问类型,现在分享给大家,希望可以做个参考。

复制代码
1
http://www.csg.is.titech.ac.jp/~chiba/javassist/
复制代码
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
package com.vl; import java.lang.reflect.Method; import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import javassist.Modifier; import javassist.NotFoundException; import javassist.bytecode.AccessFlag; import javassist.bytecode.CodeAttribute; import javassist.bytecode.LocalVariableAttribute; import javassist.bytecode.MethodInfo; import com.vl.app.PTZ; import com.vl.app.StreamsUtil; import com.vl.sql.UserDAO; public class VLInterface { public static void main(String[] args) throws NotFoundException { ClassPool pool = ClassPool.getDefault(); Class[] cs = new Class[] { PTZ.class, UserDAO.class ,StreamsUtil.class}; String[] prefixStrings = new String[] { "ptz", "user" ,"streamsutil"}; for (int i = 0; i < cs.length; i++) { CtClass ctClass; Class clz = cs[i]; ctClass = pool.get(clz.getName()); Method[] methods = clz.getDeclaredMethods(); for (Method m : methods) { String name = m.getName(); CtMethod cm = ctClass.getDeclaredMethod(name); MethodInfo methodInfo = cm.getMethodInfo(); Class[] parameterTypes = m.getParameterTypes(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute .getAttribute(LocalVariableAttribute.tag); if (attr == null) { // exception } if(cm.getMethodInfo().getAccessFlags()!=AccessFlag.PUBLIC)continue; //System.out.println(); String[] paramNames = new String[cm.getParameterTypes().length]; int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int j = 0; j < paramNames.length; j++) paramNames[j] = attr.variableName(j + pos); // paramNames即参数名 System.out.printf(""%s.%s":rntReturn:%st(Parameters ", prefixStrings[i], name, m.getReturnType().getSimpleName()); for (int i1 = 0; i1 < paramNames.length; i1++) { System.out.printf("%d:[%s %s] ", i1+1, parameterTypes[i1] .getSimpleName(), paramNames[i1]); } // for (Class class1 : parameterTypes) { // System.out.printf("[%s],", class1.getName()); // } System.out.println(")rn"); } } } }

转载于:https://www.cnblogs.com/yangyh/archive/2011/07/12/2103576.html

最后

以上就是激情机器猫最近收集整理的关于利用Javassist获取Java类中的方法参数名、参数类型、方法访问类型的全部内容,更多相关利用Javassist获取Java类中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部