有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在最终编译之前使用java注释修改源代码?

我从apt工具页面上读到,可以为generate new derived files (source files, class files, deployment descriptors, etc.)创建注释处理器。我正在寻找这样做的例子

我需要在编译时对所有带注释的字符串进行编码,以便读取类文件时不允许读取静态字符串:

基本代码:

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

应返工为:

String message = new ObfuscatedString(new long[] {0x86DD4DBB5166C13DL, 0x4C79B1CDC313AE09L, 0x1A353051DAF6463BL}).toString();

基于静态^{} method of the TrueLicense framework,处理器可以生成代码来替换带注释的字符串。实际上,此方法生成字符串“new ObfuscatedString([numeric_code]).toString()。 在运行时,ObfuscatedString的toString()方法能够返回数字代码中编码的字符串

您知道如何编写AnnotationProcessor的process()方法来编辑带注释的代码吗

提前感谢,


共 (4) 个答案

  1. # 1 楼答案

    你本来可以的

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

    它可以正常编译,但是在编译之后,你有一个工具可以检查字节码,例如使用ObjectWeb的ASM,改变字符串文字,使其看起来像

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

    为了更容易地识别需要更改的字符串,您可以为它们添加前缀,并且可以确保在代码被混淆后,该前缀确实出现

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

    我打赌spoon可能就是你要找的。但是为什么要混淆静态字符串呢

  3. # 3 楼答案

    Zelix KlassMaster提供了这种能力。如果内存可用,3个开发者以下的公司以前是免费的,但我刚刚查看了他们的购买页面,他们现在似乎向小公司收取小开发者费用。我已经好几年没有使用它了(至少5或7年),但它在混淆字符串和代码方面做得非常好

  4. # 4 楼答案

    当我要生成分发它的版本时,我有一个类,它用OfDiscated字符串调用覆盖所有常量:

    这是一个过程:

    1. 我运行我的ANT,在其他地方复制所有代码
    2. 蚂蚁呼叫OfuscateJavaConstant
    3. 我编译代码

    蚂蚁:

     <java classname="de.schlichtherle.util.ObfuscatedString">
         <arg value="${of.constant}" />
         <classpath>
             <pathelement location="${exit.path}/${buildDir}" />
             <path refid="myclasspath" />
         </classpath>
      </java>
    

    Java Ofuscate代码(混淆):

    public static void main(String[] args) {
        if ( args!=null ){
            String[] ficheros = args[0].split(";");
    
            if ( args!=ficheros )
                for (String ruta:ficheros)
                    ofuscateConstantClass( ruta );
        }
    }
    
    private static void ofuscateConstantClass( String fileName ){
    
        File archivo = null;
        FileReader fr = null;
        BufferedReader br = null;
        List<String> sb  = new ArrayList<String>();
        FileWriter fichero = null;
        PrintWriter pw = null;
    
        try{
            archivo = new File( fileName );
            fr = new FileReader (archivo);
            br = new BufferedReader(fr);
            String linea;
            while( (linea=br.readLine())!=null ){
    
                String noWhite = linea.trim().replaceAll(" +", " ");
    
                if ( noWhite.toLowerCase().startsWith("public static final string") && noWhite.endsWith("\";") ){
    
                    String firstPart = noWhite.substring(0, noWhite.indexOf("\"") );
                    String constant = noWhite.substring(noWhite.indexOf("\"")+1, noWhite.lastIndexOf("\"") );
                    String ofuscatedConstant = obfuscate( constant );
                    String secondPart = noWhite.substring(noWhite.lastIndexOf("\"")+1 );
                    sb.add( firstPart + ofuscatedConstant + secondPart );
                    System.out.println( constant + "-->" + ofuscatedConstant );
                } else
                    sb.add( linea );
            }
    
            fichero = new FileWriter( fileName );
            pw = new PrintWriter(fichero);
            for (String s:sb)
                pw.println( s );
    
        } catch ( Exception e){
    
        } finally {
             try{
                 if( null != fr )
                     fr.close();
                 if( null != pw )
                     pw.close();
                 if( null != fichero )
                     fichero.close();
             }catch (Exception e2){
                e2.printStackTrace();
             }
        }
    
    }
    

    希望有帮助:)