How to use java annotations to modify source code before final compilation?

前端 未结 4 870
不思量自难忘°
不思量自难忘° 2021-02-20 07:52

I\'ve read from the apt tool page that one can create AnnotationProcessors to generate new derived files (source files, class files, deployment descriptors, etc.). I am looking

4条回答
  •  终归单人心
    2021-02-20 08:43

    You could have

    String message = Obfuscated.decode("a string that should not be readable in class file");
    

    which compiles normally, however after compilation you have a tool which examines the bytecode e.g. using ObjectWeb's ASM, changes the String literal so it looks like

    String message = Obfuscated.decode("\u86DD\u4DBB\u5166\uC13D\u4C79\uB1CD\uC313\uAE09\u1A35\u3051\uDAF6\u463B");
    

    To make it easer to identify strings which need to be changed, you could add a prefix to them, and you could ensure this prefix does appear after the code has been obfuscated.

    String s = "Obfuscate: a string that should not be readable in class file";
    // later
    String message = Obfuscated.decode(s);
    

提交回复
热议问题