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

问题描述 投票:11回答:5

我已经从apt工具页面中读取了可以为generate new derived files (source files, class files, deployment descriptors, etc.)创建AnnotationProcessors的信息。我正在寻找这样做的例子。

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

基本代码:

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

应该重做为:

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

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

关于如何编写AnnotationProcessor的process()方法以编辑带注释的代码的任何想法?

谢谢,

java annotations apt
5个回答
2
投票

您可能有

ObfuscatedString.obfuscate(String)

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

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

[为了更轻松地识别需要更改的字符串,您可以在它们之前添加一个前缀,并且可以确保在对代码进行混淆之后,此前缀确实出现。

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

2
投票

我敢打赌String s = "Obfuscate: a string that should not be readable in class file"; // later String message = Obfuscated.decode(s); 可能就是您想要的。但是为什么要混淆静态字符串?


1
投票

spoon提供此功能。如果可以使用内存,以前对于3个以下开发人员的公司来说是免费的,但是我只是检查了他们的购买页面,看来他们现在对小型公司收取小开发商费用。几年来(至少5到7年)我没有使用过它,但是它在混淆字符串和一般代码方面做得非常出色。


1
投票

当我要生成要分发的版本时,我有一个用Ofuscated String调用覆盖所有常量的类:

这是过程:

  1. 我运行我的ANT,将所有代码复制到其他位置。
  2. 对OfuscateJavaConstant的ANT调用。
  3. 我编译代码。

[ANT:

Zelix KlassMaster

Java Ofuscate代码(ObfuscatedString):

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

希望有帮助:)


0
投票

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(); } } } 也许可以帮上忙。在发布版本中保护您的日志代码

© www.soinside.com 2019 - 2024. All rights reserved.