javassist:增强型的java反射工具,获取方法参数名

阅读更多

 

java的反射是不能获取方法的参数名的。比如:

public  String concatString(String param1,String param2){
        
return  param1 + param2;
    }

 


想获取"param1",和"param2"这个参数名,貌似是不行的。借助第三方包javaassist就可以获得。

 

整合原作者代码:

 

 

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;

public class Test {
    public static void main(String[] args) {

        testReflectParamName();
        
    }

    /**
     * 反射获取方法参数名称
     */
    public static void testReflectParamName() {
        Class clazz = MyClass.class;
        try {
            ClassPool pool = ClassPool.getDefault();
            CtClass cc = pool.get(clazz.getName());
            CtMethod cm = cc.getDeclaredMethod("concatString");

            // 使用javaassist的反射方法获取方法的参数名
            MethodInfo methodInfo = cm.getMethodInfo();
            CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
            LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
                    .getAttribute(LocalVariableAttribute.tag);
            if (attr == null) {
                // exception
            }
            String[] paramNames = new String[cm.getParameterTypes().length];
            int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
            for (int i = 0; i < paramNames.length; i++)
                paramNames[i] = attr.variableName(i + pos);
            // paramNames即参数名
            for (int i = 0; i < paramNames.length; i++) {
                System.out.println("参数名" + i + ":" + paramNames[i]);
            }

        } catch (NotFoundException e) {
            e.printStackTrace();
        }
    }
}

class MyClass {
    public String concatString(String param1, String param2) {
        return param1 + param2;
    }
}

 

 

反射获取参数注解:

 

 

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Date;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;

public class Test {
    public static void main(String[] args) {

        testReflectParamName();
        // 反射获取方法参数注解
        testReflectMethodParamAnno();
    }

    /**
     * 反射获取方法参数名称
     */
    public static void testReflectParamName() {
        Class clazz = MyClass.class;
        try {
            ClassPool pool = ClassPool.getDefault();
            CtClass cc = pool.get(clazz.getName());
            CtMethod cm = cc.getDeclaredMethod("concatString");

            // 使用javaassist的反射方法获取方法的参数名
            MethodInfo methodInfo = cm.getMethodInfo();
            CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
            LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
                    .getAttribute(LocalVariableAttribute.tag);
            if (attr == null) {
                // exception
            }
            String[] paramNames = new String[cm.getParameterTypes().length];
            int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
            for (int i = 0; i < paramNames.length; i++)
                paramNames[i] = attr.variableName(i + pos);
            // paramNames即参数名
            for (int i = 0; i < paramNames.length; i++) {
                System.out.println("参数名" + i + ":" + paramNames[i]);
            }

        } catch (NotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射获取方法参数注解
     */
    public static void testReflectMethodParamAnno() {
        Class clazz = MyClass.class;
        try {
            // 使用jdk原生的反射方法
            Method m = clazz.getDeclaredMethod("datefomat",
                    new Class[] { Date.class });
            Annotation[][] annotations = m.getParameterAnnotations();
            System.out.println("jdk获取方法参数anno:"+annotations[0][0]);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        try {
            ClassPool pool = ClassPool.getDefault();
            CtClass cc = pool.get(clazz.getName());
            CtMethod cm = cc.getDeclaredMethod("datefomat");

            // 使用javassist的反射方法可以获得参数标注值
            Object[][] annotations = cm.getParameterAnnotations();
            DateFormat myAnno = (DateFormat) annotations[0][0];
            System.out.println("参数注解:"+myAnno.value());

        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class MyClass {
    public String concatString(String param1, String param2) {
        return param1 + param2;
    }

    public String datefomat(@DateFormat("yyyy-MM-dd HH")
    Date date1) {
        return date1.toString();
    }
}

// 注解类
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@interface DateFormat {
    String value() default "yyyy-MM-dd";
}

 

转自:http://www.blogjava.net/Hafeyang/archive/2010/11/13/336114.html

 

 

 

你可能感兴趣的:(javassist,reflect)