获取Java方法参数的名称

最近有个需求,需要获取java方法参数的名称,网上查了下,然后自己也实践了下,总结出3点:
1.能不能获取方法参数的名称取决于class文件里是否含有LocalVariableTable。
2.javac编译生成的class文件不含有LocalVariableTable,但是eclipse编译生成的class文件却含有LocalVariableTable。
3.有2种方式可以获得,用Asm和Javassist,Asm的性能略高。
贴代码:

maven依赖:

		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.18.0-GA</version>
		</dependency>
		<dependency>
			<groupId>org.ow2.asm</groupId>
			<artifactId>asm</artifactId>
			<version>4.1</version>
		</dependency>

User类:

/**
 * 
 * @author <a href="mailto:yankai913@gmail.com">yankai</a>
 * @date 2013-7-17
 */
public class User {
	
	String name;
	String address;
	int age;
	
	public User(String name, String address, int age) {
		this.name = name;
		this.address = address;
		this.age = age;
	}
	
	public static User newInstance(String name, String address, int age) {
		return new User(name, address, age);
	}
	
	public User copy(String name, String address, int age) {
		return new User(name, address, age);
	}
}

asm实现:

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
/**
 * @author <a href="mailto:yankai913@gmail.com">yankai</a>
 * @date 2013-7-17
 */
public class AsmTool {
	
	private static boolean sameType(Type[] types, Class<?>[] clazzes) {
		if (types.length != clazzes.length) {
			return false;
		}
		for (int i = 0; i < types.length; i++) {
			if (!Type.getType(clazzes[i]).equals(types[i])) {
				return false;
			}
		}
		return true;
	}
	
	public static String[] getMethodParamNames(final Method m) throws Exception {
		final String[] paramNames = new String[m.getParameterTypes().length];
		String methodClass = m.getDeclaringClass().getName();
		ClassReader cr = new ClassReader(methodClass);
		cr.accept(new ClassVisitor(Opcodes.ASM4) {
			@Override
			public MethodVisitor visitMethod(final int access,
					final String name, final String desc,
					final String signature, final String[] exceptions) {
				Type[] args = Type.getArgumentTypes(desc);
				if (!name.equals(m.getName())
						|| !sameType(args, m.getParameterTypes())) {
					return null;
				}
				return new MethodVisitor(Opcodes.ASM4) {
					@Override
					public void visitLocalVariable(String name, String desc,
							String signature, Label start, Label end, int index) {
						int i = index - 1;
						if (Modifier.isStatic(m.getModifiers())) {
							i = index;
						}
						if (i >= 0 && i < paramNames.length) {
							paramNames[i] = name;
						}
					}

				};
			}
		}, 0);
		return paramNames;
	}
}

asm测试:

import java.lang.reflect.Method;
import java.util.Arrays;
/**
 * 
 * @author <a href="mailto:yankai913@gmail.com">yankai</a>
 * @date 2013-7-17
 */
public class TestAsm {
	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		final Method method = 
				User.class.getMethod("newInstance", new Class<?>[]{String.class, String.class, int.class});
		String[] s = AsmTool.getMethodParamNames(method);
		System.out.println(Arrays.toString(s));
		System.out.println(System.currentTimeMillis() - start);//40
	}
}

javassist实现:

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;
/**
 * 
 * @author <a href="mailto:yankai913@gmail.com">yankai</a>
 * @date 2013-7-17
 */
public class JavassistTool {
	
	// <ClassLoader, ClassPool>
	static final Map<ClassLoader, ClassPool> pool2map = 
			new ConcurrentHashMap<ClassLoader, ClassPool>(); 
	
	public static String[] getMethodParamNames(Method method)
			throws Exception {
		Class<?> clazz = method.getDeclaringClass();
		String methodName = method.getName();
		ClassPool pool = ClassPool.getDefault();
		CtClass cc = null;
		try {
			cc = pool.get(clazz.getName());
		} catch (Exception e) {
			if (e instanceof NotFoundException) {
				pool = pool2map.get(clazz.getClassLoader());
				if (pool == null) {
					pool = new ClassPool(true);
					pool.appendClassPath(new LoaderClassPath(clazz.getClassLoader()));
					pool2map.put(clazz.getClassLoader(), pool);
				}
			}
		}
		cc = pool.get(clazz.getName());
		CtMethod cm = cc.getDeclaredMethod(methodName);
		MethodInfo methodInfo = cm.getMethodInfo();
		CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
		LocalVariableAttribute attr = 
				(LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
				
		if (attr == null) {
			throw new IllegalStateException("LocalVariableAttribute is null");
		}
		String[] paramNames = new String[cm.getParameterTypes().length];
		int offset = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
		for (int i = 0; i < paramNames.length; i++) {
			paramNames[i] = attr.variableName(i + offset);
		}
		return paramNames;
	}
}

javassist测试:

import java.util.Arrays;
/**
 * 
 * @author <a href="mailto:yankai913@gmail.com">yankai</a>
 * @date 2013-7-17
 */
public class TestJavassist {

	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		String[] paramNames = JavassistTool.getMethodParamNames(User.class, "newInstance");
		System.out.println(Arrays.toString(paramNames));
		System.out.println(System.currentTimeMillis() - start);//90
	}
}

上一篇:JVM快速调优手册v1.0之六:JVM参数设置、分析


下一篇:erlang]supervisor(监控树)的重启策略