给注解打断点的一种方法

Controllor中有如下的方法,

@HttpFeatures(contentType="application/json")
@Get("send/{appId:[0-9]+}")
@Post("send/{appId:[0-9]+}")
public String sendMail(@Param("data") String dataBase64,@Param("appId") long appId) {
//...
}

想知道这几个注解何时被谁于何处给调用. 又不能在注解所在行直接加断点.  只好退而求其次在Method(java.lang.reflect.Method)的注解相关方法中打断点

<T extends Annotation> T java.lang.reflect.Method.getAnnotation(Class<T> annotationClass)
Annotation[] java.lang.reflect.Method.getDeclaredAnnotations()
Annotation[][] java.lang.reflect.Method.getParameterAnnotations()

但是在断点处,看不到变量的实际值.如下所示:

120844_i9CZ_1175066.png

于是在本地覆盖java.lang.reflect.Method(即在当前工程中创建一个包为java.lang.reflect,在该包下创建一个名为Method的类,内容和jdk源码一样.)并在相关处添加断点. 但压根就不进来,还是找jdk中的Method.看来没办法覆盖java核心类库(如rt.jar)中的类了(因为它们在程序启动时是被顶层(或根)类加载器所加载,一旦完成加载,就不会重复加载(同名类)了.).

那么假如将rt.jar中的Method.class给删除了呢,再在本地覆盖Method,不就可以加载本地的Method了吗. 但证实不可行,因为立即就报错了:

Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/reflect/Method

显然Method被其他核心类所引用.

只好自己编译一个jdk了(且是debug版的jdk,否则调试时还是看不到变量值). 幸好Ubuntu中编译jdk还是挺顺利的.

于是修改项目的JRE为新编译的支持debug的jdk.如下所示:

121205_G7Ip_1175066.png

这时可以清晰的看到变量值了.如下所示:

121312_C8I6_1175066.png

但是有太多注解了,而只想看到net.paoding.rose.web.annotation包下的那些注解.如net.paoding.rose.web.annotation.HttpFeatures.

于是修改Method的方法(当然需要重新编译了),如

public <T extends Annotation> T getAnnotation(Class<T> annotationClass){
    //添加了如下的代码
    if(annotationClass.getName().startsWith("net.paoding.rose.web.annotation"))
        System.out.println(annotationClass.getName());
    //......
}

将断点放在if方法内部, 这样进入断点的注解就是想要探究的注解了.

其他几个方法做同样的处理.

虽然还是有一些干扰项,如net.paoding.rose.web.annotation.Ignored.但还是可以接受的.

很快就知道HttpFeatures注解是被谁在何处给调用的了.

121610_2nup_1175066.png

上图左侧显示,是在ActionEngin的构造方法中98行被调用.该行代码如下所示:

HttpFeatures httpFeatures = method.getAnnotation(HttpFeatures.class);

接下来看Get和Post在何处被调用. 这次是调用的Method另一个方法:

public Annotation[] getDeclaredAnnotations(){  
    //对源代码做了如下的修改:
    //return AnnotationParser.toArray(declaredAnnotations()); //原代码
    Annotation[] annotations = AnnotationParser.toArray(declaredAnnotations());
    for(Annotation anno : annotations)
        if(anno.toString().indexOf("net.paoding.rose.web.annotation")!=-1)
            System.out.println(anno.toString());
        
    return annotations;
}

这时在debug视图中的看到的内容为:

121951_E3Ye_1175066.png

由左侧可知,是被ControllerRef在collectsShotcutMappings方法中所调用. 其相关代码为:

Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
    if (annotation instanceof Delete) {
        restMethods.put(ReqMethod.DELETE, ((Delete) annotation).value());
     } else if (annotation instanceof Get) {
        restMethods.put(ReqMethod.GET, ((Get) annotation).value());
...

接下来看方法参数中的注解(如@Param("data") String dataBase64)何时何处被调用,显然应该在getParameterAnnotations方法中打断点.

同样在getParameterAnnotations添加了如下的代码:

for(int i=0; i<result.length; i++){
    for(int j=0; j<result[i].length; j++){
        if(result[i][j].toString().indexOf("net.paoding.rose.web.annotation")!=-1) 
            System.out.println(result[i][j].toString()); 
    }
}

这时debug视图中的内容为:

122237_pkcz_1175066.png

由左侧可知,是在ParameterNameDiscovererImpl.getParameterNames处调用的,相关代码为:

Annotation[][] parameterAnnotations = method.getParameterAnnotations();
String[] names = new String[parameterTypes.length];
Map<String, Integer> counts = new HashMap<String, Integer>();
for (int i = 0; i < names.length; i++) {
    Annotation[] annotations = parameterAnnotations[i];
    for (Annotation annotation : annotations) {
        String name = null;
        if (annotation instanceof Param) {
            name = ((Param) annotation).value();
......

同理可以修改Class类,在注解相关方法中添加一些代码,断点打在相应位置,可以得到类注解(如@Path("/mail"))的调用信息.

注:

若有Rose的源代码的话,导入到Eclipse中,直接在java类中搜索Get, Post, HttpFeatures即可.

补充:

Ubuntu环境编译OPENJDK(摘自周志明深入理解JAVA虚拟机)

  1. 下载openjdk(http://download.java.net/openjdk/jdk7/)

  2. 安装依赖包
    sudo apt-get install build-essential gawk m4 openjdk-6-jdk libasound2-dev libcups2-dev libxrender-dev xorg-dev xutils-dev x11proto-print-dev binutils libmotif3 libmotif-dev ant

  3. 在解压后的openjdk目录下创建如下的脚本,内容为:

    export LANG=C
    #启动JDK 需修改为当前系统已安装的jdk目录 毕竟jdk中的大部分类还是java写的,编译还得依赖已有的环境
    export ALT_BOOTDIR=${JAVA_HOME}
    export ALLOW_DOWNLOADS=true

    #并行编译的线程数,设置为和CPU内核数量一致即可
    export HOTSPOT_BUILD_JOBS=4
    export ALT_PARALLEL_COMPILE_JOBS=4


    export SKIP_COMPARE_IMAGES=true
    export USE_PRECOMPILED_HEADER=true

    export SKIP_FASTDEBUG_BUILD=false
    export DEBUG_NAME=fastdebug

    BUILD_DEPLOY=false

    BUILD_INSTALL=false

    #编译后的JDK输出目录 ${openjdk_install_path}为当前openjdk目录
    export ALT_OUTPUTDIR=${openjdk_install_path}/build

    unset JAVA_HOME
    unset CLASSPATH

    make 2>&1 | tee $ALT_OUTPUTDIR/build.log

    注: 标红部分表示需修改为自己环境的路径.

     

  4. 执行该脚本,若一切顺利的话,在${openjdk_install_path}下会看到一个目录(build-fastdebug),进入该目录,会有一个j2sdk-image的目录,该目录便是编译好的jdk对应的目录. 若修改了源码(源码的路径为:${openjdk_install_path}/jdk/src/share/classes)的话, 当然需要重新编译(即再次执行上述脚本).



转载于:https://my.oschina.net/zhuguowei/blog/406769

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给某个方法添加注解,可以使用反射来实现。下面是一个示例代码: ```java import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class AnnotationExample { public static void main(String[] args) throws NoSuchMethodException { // 获取要添加注解方法 Method method = MyClass.class.getMethod("myMethod"); // 获取注解类的实例 MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); // 如果方法已经有注解,则输出注解的值 if (annotation != null) { System.out.println("MyAnnotation value: " + annotation.value()); } // 动态添加注解 if (annotation == null) { // 获取注解类 Class<MyAnnotation> annotationClass = MyAnnotation.class; // 创建一个注解实例 MyAnnotation newAnnotation = annotationClass.newInstance(); // 设置注解的值 newAnnotation.setValue("New Value"); // 获取方法注解数组 Annotation[] annotations = method.getDeclaredAnnotations(); // 创建一个新的注解数组,长度比原数组多1 Annotation[] newAnnotations = new Annotation[annotations.length + 1]; // 将原数组的元素复制到新数组中 System.arraycopy(annotations, 0, newAnnotations, 0, annotations.length); // 将新注解添加到新数组的最后一个位置 newAnnotations[newAnnotations.length - 1] = newAnnotation; // 使用反射设置方法注解数组 Method annotationsMethod = Method.class.getDeclaredMethod("declaredAnnotations"); annotationsMethod.setAccessible(true); annotationsMethod.invoke(method)[0] = newAnnotations; } } } // 自定义注解 @interface MyAnnotation { String value() default ""; } // 被注解的类 class MyClass { @MyAnnotation("Old Value") public void myMethod() { System.out.println("MyMethod"); } } ``` 上述代码中,首先通过反射获取要添加注解方法。然后,判断方法是否已经有注解,如果有则输出注解的值。如果方法没有注解,则使用反射动态添加注解。具体实现是通过创建一个新的注解实例,并设置注解的值,然后将新注解添加到方法注解数组中。最后使用反射设置方法注解数组。这样就成功给方法添加了注解

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值